Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(unit-jest, unit-mocha): generate passing tests when bare option is used with router enabled (#3544) #5591

Merged
merged 2 commits into from Sep 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 53 additions & 0 deletions packages/@vue/cli-plugin-unit-jest/__tests__/jestGenerator.spec.js
Expand Up @@ -88,3 +88,56 @@ test('TS + bare', async () => {
expect(spec).toMatch(`const wrapper = shallowMount(App)`)
expect(spec).toMatch(`expect(wrapper.text()).toMatch(\`Welcome to Your Vue.js + TypeScript App\`)`)
})

test('bare + router', async () => {
const { files } = await generateWithPlugin([
{
id: 'unit-jest',
apply: require('../generator'),
options: {}
},
{
id: '@vue/cli-service',
apply: () => {},
options: { bare: true }
},
{
id: 'router',
apply: () => {},
options: {}
}
])

const spec = files['tests/unit/example.spec.js']
expect(spec).toMatch(`const wrapper = mount(App,`)
expect(spec).toMatch(`expect(wrapper.text()).toMatch(\`Welcome to Your Vue.js App\`)`)
})

test('TS + bare + router', async () => {
const { files } = await generateWithPlugin([
{
id: 'unit-jest',
apply: require('../generator'),
options: {}
},
{
id: 'typescript',
apply: () => {},
options: {}
},
{
id: '@vue/cli-service',
apply: () => {},
options: { bare: true }
},
{
id: 'router',
apply: () => {},
options: {}
}
])

const spec = files['tests/unit/example.spec.ts']
expect(spec).toMatch(`const wrapper = mount(App,`)
expect(spec).toMatch(`expect(wrapper.text()).toMatch(\`Welcome to Your Vue.js App\`)`)
})
3 changes: 2 additions & 1 deletion packages/@vue/cli-plugin-unit-jest/generator/index.js
Expand Up @@ -3,7 +3,8 @@ module.exports = (api, options, rootOptions, invoking) => {

api.render('./template', {
isVue3,
hasTS: api.hasPlugin('typescript')
hasTS: api.hasPlugin('typescript'),
hasRouter: api.hasPlugin('router')
})

api.extendPackage({
Expand Down
@@ -1,5 +1,9 @@
<%_ if (!hasTS) { _%>
<%_ if (!rootOptions.bare || !hasRouter) { _%>
import { shallowMount } from '@vue/test-utils'
<%_ } else { _%>
import { mount, createLocalVue } from '@vue/test-utils'
<%_ } _%>
<%_ if (!rootOptions.bare) { _%>
import HelloWorld from '@/components/HelloWorld.vue'

Expand All @@ -18,10 +22,28 @@ describe('HelloWorld.vue', () => {
})
<%_ } else { _%>
import App from '@/App.vue'
<%_ if (!hasRouter) { _%>

test('App should work', () => {
const wrapper = shallowMount(App)
expect(wrapper.text()).toMatch(`Welcome to Your Vue.js App`)
})

<%_ } else {_%>
import VueRouter from 'vue-router'
import router from '@/router'

const localVue = createLocalVue()
localVue.use(VueRouter)

test('App should render default route', () => {
const wrapper = mount(App, {
localVue,
router
})
expect(wrapper.text()).toMatch(`Welcome to Your Vue.js App`)
})

<%_ } _%>
<%_ } _%>
<%_ } _%>
@@ -1,5 +1,9 @@
<%_ if (hasTS) { _%>
<%_ if (!rootOptions.bare || !hasRouter) { _%>
import { shallowMount } from '@vue/test-utils'
<%_ } else { _%>
import { mount, createLocalVue } from '@vue/test-utils'
<%_ } _%>
<%_ if (!rootOptions.bare) { _%>
import HelloWorld from '@/components/HelloWorld.vue'

Expand All @@ -18,10 +22,28 @@ describe('HelloWorld.vue', () => {
})
<%_ } else { _%>
import App from '@/App.vue'
<%_ if (!hasRouter) { _%>

test('App should work', () => {
const wrapper = shallowMount(App)
expect(wrapper.text()).toMatch(`Welcome to Your Vue.js + TypeScript App`)
})

<%_ } else {_%>
import VueRouter from 'vue-router'
import router from '@/router'

const localVue = createLocalVue()
localVue.use(VueRouter)

test('App should render default route', () => {
const wrapper = mount(App, {
localVue,
router
})
expect(wrapper.text()).toMatch(`Welcome to Your Vue.js App`)
})

<%_ } _%>
<%_ } _%>
<%_ } _%>
Expand Up @@ -85,3 +85,56 @@ test('TS + bare', async () => {
expect(spec).toMatch(`const wrapper = shallowMount(App)`)
expect(spec).toMatch(`expect(wrapper.text()).to.include(\`Welcome to Your Vue.js + TypeScript App\`)`)
})

test('bare + router', async () => {
const { files } = await generateWithPlugin([
{
id: 'unit-mocha',
apply: require('../generator'),
options: {}
},
{
id: '@vue/cli-service',
apply: () => {},
options: { bare: true }
},
{
id: 'router',
apply: () => {},
options: {}
}
])

const spec = files['tests/unit/example.spec.js']
expect(spec).toMatch(`const wrapper = mount(App,`)
expect(spec).toMatch(`expect(wrapper.text()).to.include(\`Welcome to Your Vue.js App\`)`)
})

test('TS + bare + router', async () => {
const { files } = await generateWithPlugin([
{
id: 'unit-mocha',
apply: require('../generator'),
options: {}
},
{
id: 'typescript',
apply: () => {},
options: {}
},
{
id: '@vue/cli-service',
apply: () => {},
options: { bare: true }
},
{
id: 'router',
apply: () => {},
options: {}
}
])

const spec = files['tests/unit/example.spec.ts']
expect(spec).toMatch(`const wrapper = mount(App,`)
expect(spec).toMatch(`expect(wrapper.text()).to.include(\`Welcome to Your Vue.js App\`)`)
})
3 changes: 2 additions & 1 deletion packages/@vue/cli-plugin-unit-mocha/generator/index.js
Expand Up @@ -3,7 +3,8 @@ module.exports = (api, options, rootOptions, invoking) => {

api.render('./template', {
isVue3,
hasTS: api.hasPlugin('typescript')
hasTS: api.hasPlugin('typescript'),
hasRouter: api.hasPlugin('router')
})

api.extendPackage({
Expand Down
@@ -1,6 +1,10 @@
<%_ if (!hasTS) { _%>
import { expect } from 'chai'
<%_ if (!rootOptions.bare || !hasRouter) { _%>
import { shallowMount } from '@vue/test-utils'
<%_ } else { _%>
import { mount, createLocalVue } from '@vue/test-utils'
<%_ } _%>
<%_ if (!rootOptions.bare) { _%>
import HelloWorld from '@/components/HelloWorld.vue'

Expand All @@ -19,12 +23,32 @@ describe('HelloWorld.vue', () => {
})
<%_ } else { _%>
import App from '@/App.vue'
<%_ if (!hasRouter) { _%>

describe('App', () => {
it('should work', () => {
const wrapper = shallowMount(App)
expect(wrapper.text()).to.include(`Welcome to Your Vue.js App`)
})
})

<%_ } else {_%>
import VueRouter from 'vue-router'
import router from '@/router'

const localVue = createLocalVue()
localVue.use(VueRouter)

describe('App', () => {
it('should render default route', () => {
const wrapper = mount(App, {
localVue,
router
})
expect(wrapper.text()).to.include(`Welcome to Your Vue.js App`)
})
})

<%_ } _%>
<%_ } _%>
<%_ } _%>
@@ -1,6 +1,10 @@
<%_ if (hasTS) { _%>
import { expect } from 'chai'
<%_ if (!rootOptions.bare || !hasRouter) { _%>
import { shallowMount } from '@vue/test-utils'
<%_ } else { _%>
import { mount, createLocalVue } from '@vue/test-utils'
<%_ } _%>
<%_ if (!rootOptions.bare) { _%>
import HelloWorld from '@/components/HelloWorld.vue'

Expand All @@ -19,12 +23,32 @@ describe('HelloWorld.vue', () => {
})
<%_ } else { _%>
import App from '@/App.vue'
<%_ if (!hasRouter) { _%>

describe('App', () => {
it('should work', () => {
const wrapper = shallowMount(App)
expect(wrapper.text()).to.include(`Welcome to Your Vue.js + TypeScript App`)
})
})

<%_ } else {_%>
import VueRouter from 'vue-router'
import router from '@/router'

const localVue = createLocalVue()
localVue.use(VueRouter)

describe('App', () => {
it('should render default route', () => {
const wrapper = mount(App, {
localVue,
router
})
expect(wrapper.text()).to.include(`Welcome to Your Vue.js App`)
})
})

<%_ } _%>
<%_ } _%>
<%_ } _%>