Skip to content

Commit e499e63

Browse files
committed
feat: use TS config to have commonjs modules
The previous logic was trying to figure out if CommonJS modules were used in the TS config and if not, and if no babel configuration provided, then loaded a babel transformer to change the module format. This changes the logic to use a more straightforward method by directly updating the tsconfig to commonjs, which should hopefully have the same results without side-effects.
1 parent c2e2913 commit e499e63

File tree

2 files changed

+4
-20
lines changed

2 files changed

+4
-20
lines changed

packages/vue3-jest/lib/transformers/typescript.js

+1-19
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const ensureRequire = require('../ensure-require')
22
const babelJest = require('babel-jest').default
33
const {
4-
getBabelOptions,
54
getTsJestConfig,
65
stripInlineSourceMap,
76
getCustomTransformer,
@@ -14,7 +13,6 @@ module.exports = {
1413
const typescript = require('typescript')
1514
const vueJestConfig = getVueJestConfig(config)
1615
const tsconfig = getTsJestConfig(config)
17-
const babelOptions = getBabelOptions(filePath)
1816

1917
const res = typescript.transpileModule(scriptContent, {
2018
...tsconfig,
@@ -26,27 +24,11 @@ module.exports = {
2624
const inputSourceMap =
2725
res.sourceMapText !== undefined ? JSON.parse(res.sourceMapText) : ''
2826

29-
// handle ES modules in TS source code in case user uses non commonjs module
30-
// output and there is no presets or plugins defined in package.json or babel config file
31-
let inlineBabelOptions = {}
32-
if (
33-
tsconfig.compilerOptions.module !== typescript.ModuleKind.CommonJS &&
34-
!(babelOptions.presets && babelOptions.presets.length) &&
35-
!(babelOptions.plugins && babelOptions.plugins.length)
36-
) {
37-
inlineBabelOptions = {
38-
plugins: [require('@babel/plugin-transform-modules-commonjs')]
39-
}
40-
}
4127
const customTransformer =
4228
getCustomTransformer(vueJestConfig['transform'], 'js') || {}
4329
const transformer = customTransformer.process
4430
? customTransformer
45-
: babelJest.createTransformer(
46-
Object.assign(inlineBabelOptions, {
47-
inputSourceMap
48-
})
49-
)
31+
: babelJest.createTransformer({ inputSourceMap })
5032

5133
return transformer.process(res.outputText, filePath, config)
5234
}

packages/vue3-jest/lib/utils.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ const getTsJestConfig = function getTsJestConfig(config) {
8282
const configSet = new ConfigSet(config.config)
8383
const tsConfig = configSet.typescript || configSet.parsedTsConfig
8484
// Force es5 to prevent const vue_1 = require('vue') from conflicting
85-
return { compilerOptions: { ...tsConfig.options, target: 'es5' } }
85+
return {
86+
compilerOptions: { ...tsConfig.options, target: 'es5', module: 'commonjs' }
87+
}
8688
}
8789

8890
function isValidTransformer(transformer) {

0 commit comments

Comments
 (0)