From 9624a5d8955c48ce714d090347bbfa02b5dec34a Mon Sep 17 00:00:00 2001 From: Sergey Skrynnikov Date: Wed, 17 Jun 2020 15:31:59 +0300 Subject: [PATCH] fix(unit-jest, unit-mocha): generate passing tests when `bare` option is used with router enabled (#3544) --- .../__tests__/jestGenerator.spec.js | 53 +++++++++++++++++++ .../cli-plugin-unit-jest/generator/index.js | 3 +- .../template/tests/unit/example.spec.js | 22 ++++++++ .../template/tests/unit/example.spec.ts | 22 ++++++++ .../__tests__/mochaGenerator.spec.js | 53 +++++++++++++++++++ .../cli-plugin-unit-mocha/generator/index.js | 3 +- .../template/tests/unit/example.spec.js | 24 +++++++++ .../template/tests/unit/example.spec.ts | 24 +++++++++ 8 files changed, 202 insertions(+), 2 deletions(-) diff --git a/packages/@vue/cli-plugin-unit-jest/__tests__/jestGenerator.spec.js b/packages/@vue/cli-plugin-unit-jest/__tests__/jestGenerator.spec.js index dcc581a68f..0166aae893 100644 --- a/packages/@vue/cli-plugin-unit-jest/__tests__/jestGenerator.spec.js +++ b/packages/@vue/cli-plugin-unit-jest/__tests__/jestGenerator.spec.js @@ -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\`)`) +}) diff --git a/packages/@vue/cli-plugin-unit-jest/generator/index.js b/packages/@vue/cli-plugin-unit-jest/generator/index.js index be9dfd94ab..0f32bd05c6 100644 --- a/packages/@vue/cli-plugin-unit-jest/generator/index.js +++ b/packages/@vue/cli-plugin-unit-jest/generator/index.js @@ -1,6 +1,7 @@ module.exports = (api, _, __, invoking) => { api.render('./template', { - hasTS: api.hasPlugin('typescript') + hasTS: api.hasPlugin('typescript'), + hasRouter: api.hasPlugin('router') }) api.extendPackage({ diff --git a/packages/@vue/cli-plugin-unit-jest/generator/template/tests/unit/example.spec.js b/packages/@vue/cli-plugin-unit-jest/generator/template/tests/unit/example.spec.js index aa88537261..40e5b97dbe 100644 --- a/packages/@vue/cli-plugin-unit-jest/generator/template/tests/unit/example.spec.js +++ b/packages/@vue/cli-plugin-unit-jest/generator/template/tests/unit/example.spec.js @@ -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' @@ -14,10 +18,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`) +}) + +<%_ } _%> <%_ } _%> <%_ } _%> diff --git a/packages/@vue/cli-plugin-unit-jest/generator/template/tests/unit/example.spec.ts b/packages/@vue/cli-plugin-unit-jest/generator/template/tests/unit/example.spec.ts index 5d56d9ae54..cef80d7171 100644 --- a/packages/@vue/cli-plugin-unit-jest/generator/template/tests/unit/example.spec.ts +++ b/packages/@vue/cli-plugin-unit-jest/generator/template/tests/unit/example.spec.ts @@ -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' @@ -14,10 +18,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`) +}) + +<%_ } _%> <%_ } _%> <%_ } _%> diff --git a/packages/@vue/cli-plugin-unit-mocha/__tests__/mochaGenerator.spec.js b/packages/@vue/cli-plugin-unit-mocha/__tests__/mochaGenerator.spec.js index 572de94703..6c6368c4d1 100644 --- a/packages/@vue/cli-plugin-unit-mocha/__tests__/mochaGenerator.spec.js +++ b/packages/@vue/cli-plugin-unit-mocha/__tests__/mochaGenerator.spec.js @@ -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\`)`) +}) diff --git a/packages/@vue/cli-plugin-unit-mocha/generator/index.js b/packages/@vue/cli-plugin-unit-mocha/generator/index.js index 88fb63dfbf..2c4a83be0b 100644 --- a/packages/@vue/cli-plugin-unit-mocha/generator/index.js +++ b/packages/@vue/cli-plugin-unit-mocha/generator/index.js @@ -1,6 +1,7 @@ module.exports = (api, _, __, invoking) => { api.render('./template', { - hasTS: api.hasPlugin('typescript') + hasTS: api.hasPlugin('typescript'), + hasRouter: api.hasPlugin('router') }) api.extendPackage({ diff --git a/packages/@vue/cli-plugin-unit-mocha/generator/template/tests/unit/example.spec.js b/packages/@vue/cli-plugin-unit-mocha/generator/template/tests/unit/example.spec.js index 9231226366..e270fa6e20 100644 --- a/packages/@vue/cli-plugin-unit-mocha/generator/template/tests/unit/example.spec.js +++ b/packages/@vue/cli-plugin-unit-mocha/generator/template/tests/unit/example.spec.js @@ -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' @@ -15,6 +19,7 @@ describe('HelloWorld.vue', () => { }) <%_ } else { _%> import App from '@/App.vue' +<%_ if (!hasRouter) { _%> describe('App', () => { it('should work', () => { @@ -22,5 +27,24 @@ describe('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`) + }) +}) + +<%_ } _%> <%_ } _%> <%_ } _%> diff --git a/packages/@vue/cli-plugin-unit-mocha/generator/template/tests/unit/example.spec.ts b/packages/@vue/cli-plugin-unit-mocha/generator/template/tests/unit/example.spec.ts index 16bb292f8d..9db17d20ae 100644 --- a/packages/@vue/cli-plugin-unit-mocha/generator/template/tests/unit/example.spec.ts +++ b/packages/@vue/cli-plugin-unit-mocha/generator/template/tests/unit/example.spec.ts @@ -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' @@ -15,6 +19,7 @@ describe('HelloWorld.vue', () => { }) <%_ } else { _%> import App from '@/App.vue' +<%_ if (!hasRouter) { _%> describe('App', () => { it('should work', () => { @@ -22,5 +27,24 @@ describe('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`) + }) +}) + +<%_ } _%> <%_ } _%> <%_ } _%>