Skip to content

Commit

Permalink
fix(unit-jest, unit-mocha): generate passing tests when bare option…
Browse files Browse the repository at this point in the history
… is used with router enabled (#5591)

Fixes #3544
  • Loading branch information
IwalkAlone committed Sep 7, 2020
1 parent f42888d commit d6e493d
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 2 deletions.
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`)
})
})

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

0 comments on commit d6e493d

Please sign in to comment.