From ea2648e62b447acae5337166de1764298cd774b8 Mon Sep 17 00:00:00 2001 From: Evan You Date: Mon, 15 Jan 2018 15:16:08 -0500 Subject: [PATCH] feat: make jest plugin work with TypeScript --- .../generator/template/tsconfig.json | 3 +++ .../@vue/cli-plugin-unit-jest/generator/index.js | 3 ++- packages/@vue/cli-plugin-unit-jest/index.js | 15 ++++++++++++++- packages/@vue/cli/lib/promptModules/unit.js | 5 ++--- 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/packages/@vue/cli-plugin-typescript/generator/template/tsconfig.json b/packages/@vue/cli-plugin-typescript/generator/template/tsconfig.json index 93ed51cf7..cb4cd62e8 100644 --- a/packages/@vue/cli-plugin-typescript/generator/template/tsconfig.json +++ b/packages/@vue/cli-plugin-typescript/generator/template/tsconfig.json @@ -8,6 +8,9 @@ "experimentalDecorators": true, "emitDecoratorMetadata": true, <%_ } _%> + <%_ if (hasJest) { _%> + "allowSyntheticDefaultImports": true, + <%_ } _%> "sourceMap": true, "baseUrl": ".", "paths": { diff --git a/packages/@vue/cli-plugin-unit-jest/generator/index.js b/packages/@vue/cli-plugin-unit-jest/generator/index.js index 1712dc331..5dc32af45 100644 --- a/packages/@vue/cli-plugin-unit-jest/generator/index.js +++ b/packages/@vue/cli-plugin-unit-jest/generator/index.js @@ -11,6 +11,8 @@ module.exports = api => { const jestConfig = { 'moduleFileExtensions': [ + 'js', + 'jsx', 'json', // tell Jest to handle *.vue files 'vue' @@ -31,7 +33,6 @@ module.exports = api => { } if (!api.hasPlugin('typescript')) { - jestConfig.moduleFileExtensions.unshift('js', 'jsx') jestConfig.transform['^.+\\.jsx?$'] = 'babel-jest' api.extendPackage({ devDependencies: { diff --git a/packages/@vue/cli-plugin-unit-jest/index.js b/packages/@vue/cli-plugin-unit-jest/index.js index 15e133782..b8f1cc346 100644 --- a/packages/@vue/cli-plugin-unit-jest/index.js +++ b/packages/@vue/cli-plugin-unit-jest/index.js @@ -18,8 +18,21 @@ module.exports = api => { const execa = require('execa') const jestBinPath = require.resolve('jest/bin/jest') + let testMatch = [] + if (!args._.length && api.hasPlugin('typescript')) { + testMatch = [`--testMatch`, `/**/*.spec.(ts|tsx|js)`] + } + + const argv = [ + ...rawArgv, + ...testMatch + ] + return new Promise((resolve, reject) => { - const child = execa(jestBinPath, rawArgv, { stdio: 'inherit' }) + const child = execa(jestBinPath, argv, { + cwd: api.resolve('.'), + stdio: 'inherit' + }) child.on('error', reject) child.on('exit', code => { if (code !== 0) { diff --git a/packages/@vue/cli/lib/promptModules/unit.js b/packages/@vue/cli/lib/promptModules/unit.js index 177926b84..f0f37af07 100644 --- a/packages/@vue/cli/lib/promptModules/unit.js +++ b/packages/@vue/cli/lib/promptModules/unit.js @@ -18,10 +18,9 @@ module.exports = cli => { short: 'Mocha' }, { - name: 'Jest (WIP)', + name: 'Jest', value: 'jest', - short: 'Jest', - disabled: true + short: 'Jest' } ] })