Skip to content

Commit 65d5d36

Browse files
committed
fix: transform es modules syntax for Jest when not using Babel
close #1524
1 parent dd39866 commit 65d5d36

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

packages/@vue/cli-plugin-unit-jest/__tests__/jestPlugin.spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,13 @@ test('should respect jest.config.js testMatch config', async () => {
5757
expect(result.stdout).toMatch('testMatch: custom-test-directory/my.spec.js')
5858
expect(result.stdout).toMatch('No tests found')
5959
})
60+
61+
test('should work without Babel', async () => {
62+
const project = await create('jest-without-babel', {
63+
plugins: {
64+
'@vue/cli-plugin-unit-jest': {}
65+
},
66+
useConfigFiles: true
67+
})
68+
await project.run(`vue-cli-service test:unit`)
69+
})

packages/@vue/cli-plugin-unit-jest/generator/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,21 @@ module.exports = api => {
3636
}
3737

3838
if (!api.hasPlugin('typescript')) {
39+
jestConfig.transform['^.+\\.jsx?$'] = 'babel-jest'
3940
if (api.hasPlugin('babel')) {
40-
jestConfig.transform['^.+\\.jsx?$'] = 'babel-jest'
4141
api.extendPackage({
4242
devDependencies: {
4343
'babel-jest': '^23.0.1',
4444
// this is for now necessary to force babel-jest and vue-jest to use babel 7
4545
'babel-core': '7.0.0-bridge.0'
4646
}
4747
})
48+
} else {
49+
api.extendPackage({
50+
babel: {
51+
plugins: ['transform-es2015-modules-commonjs']
52+
}
53+
})
4854
}
4955
} else {
5056
jestConfig.moduleFileExtensions.unshift('ts', 'tsx')

packages/@vue/cli-plugin-unit-jest/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@
2323
},
2424
"dependencies": {
2525
"@vue/cli-shared-utils": "^3.0.0-beta.16",
26+
"babel-jest": "^23.0.1",
27+
"babel-plugin-transform-es2015-modules-commonjs": "^6.26.2",
2628
"jest": "^23.1.0",
2729
"jest-serializer-vue": "^1.0.0",
2830
"jest-transform-stub": "^1.0.0",
2931
"vue-jest": "^2.6.0"
3032
},
3133
"devDependencies": {
3234
"@vue/test-utils": "^1.0.0-beta.16",
33-
"babel-jest": "^23.0.1",
3435
"ts-jest": "^22.4.6"
3536
}
3637
}

0 commit comments

Comments
 (0)