Skip to content

Commit 78b1551

Browse files
committed
fix(ts-config): remove dependency on ts-jest and configure tsconfif with vue-jest
1 parent da9589e commit 78b1551

File tree

6 files changed

+160
-7
lines changed

6 files changed

+160
-7
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"compilerOptions": {
3+
"module": "ES2015",
4+
"isolatedModules": false,
5+
"experimentalDecorators": true,
6+
"noImplicitAny": true,
7+
"noImplicitThis": true,
8+
"strictNullChecks": true,
9+
"removeComments": true,
10+
"emitDecoratorMetadata": true,
11+
"suppressImplicitAnyIndexErrors": true,
12+
"allowSyntheticDefaultImports": true,
13+
"sourceMap": true
14+
}
15+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "babel-in-package",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"license": "MIT",
6+
"private": true,
7+
"scripts": {
8+
"test": "jest --no-cache test.js"
9+
},
10+
"dependencies": {
11+
"vue": "^2.5.21",
12+
"vue-template-compiler": "^2.5.21"
13+
},
14+
"devDependencies": {
15+
"@babel/core": "^7.2.2",
16+
"@babel/preset-env": "^7.2.3",
17+
"@vue/test-utils": "^1.0.0-beta.28",
18+
"jest": "^24.0.0",
19+
"vue-jest": "file:../../../"
20+
},
21+
"jest": {
22+
"moduleFileExtensions": [
23+
"js",
24+
"json",
25+
"vue"
26+
],
27+
"transform": {
28+
"^.+\\.js$": "babel-jest",
29+
"^.+\\.vue$": "vue-jest"
30+
}
31+
},
32+
"babel": {
33+
"presets": [
34+
"@babel/env"
35+
]
36+
}
37+
}

e2e/__projects__/tsconfig/test.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { getTSConfig } from 'vue-jest/lib/utils'
2+
import ts from 'typescript'
3+
4+
describe('tsconfig options', () => {
5+
test('should extend the tsconfig when extends options is used', () => {
6+
const { compilerOptions } = getTSConfig()
7+
expect(compilerOptions.strictNullChecks).toBeTruthy()
8+
expect(compilerOptions.module).toBe(ts.ModuleKind.ES2015)
9+
})
10+
11+
test('should read tsconfig from root when not assigned', () => {
12+
const { compilerOptions } = getTSConfig()
13+
expect(compilerOptions.strictNullChecks).toBeTruthy()
14+
expect(compilerOptions.module).toBe(ts.ModuleKind.ES2015)
15+
})
16+
17+
test('should read tsconfig from ts-jest when used in jest config', () => {
18+
const { compilerOptions } = getTSConfig({
19+
globals: {
20+
'ts-jest': {
21+
tsConfig: './tsconfig.json'
22+
}
23+
}
24+
})
25+
expect(compilerOptions.outDir).toBe('$$ts-jest$$')
26+
})
27+
28+
test('should read from tsConfig object when specified in vue-jest config', () => {
29+
const { compilerOptions } = getTSConfig({
30+
globals: {
31+
'vue-jest': {
32+
tsConfig: {
33+
compilerOptions: {
34+
target: 'ES2016'
35+
}
36+
}
37+
}
38+
}
39+
})
40+
expect(compilerOptions.target).toBe(ts.ScriptTarget[ts.ScriptTarget.ES2016])
41+
})
42+
43+
test('should read user configured file path when specified in vue-jest config', () => {
44+
const { compilerOptions } = getTSConfig({
45+
globals: {
46+
'vue-jest': {
47+
tsConfig: './config/base-tsconfig.json'
48+
}
49+
}
50+
})
51+
expect(compilerOptions.module).toBe(ts.ModuleKind.ES2015)
52+
expect(compilerOptions.moduleResolution).toBeUndefined()
53+
})
54+
})
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "./config/base-tsconfig.json",
3+
"compilerOptions": {
4+
"target": "ES2017",
5+
"lib": ["dom", "es6"],
6+
"moduleResolution": "node",
7+
"types": ["vue-typescript-import-dts", "node"]
8+
}
9+
}

lib/typescript-transformer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const ensureRequire = require('./ensure-require')
22
const babelJest = require('babel-jest')
33
const getBabelOptions = require('./utils').getBabelOptions
4-
const getTsJestConfig = require('./utils').getTsJestConfig
4+
const getTSConfig = require('./utils').getTSConfig
55
const stripInlineSourceMap = require('./utils').stripInlineSourceMap
66
const getCustomTransformer = require('./utils').getCustomTransformer
77
const getVueJestConfig = require('./utils').getVueJestConfig
@@ -11,7 +11,7 @@ module.exports = {
1111
ensureRequire('typescript', ['typescript'])
1212
const typescript = require('typescript')
1313
const vueJestConfig = getVueJestConfig(config)
14-
const tsconfig = getTsJestConfig(config)
14+
const tsconfig = getTSConfig(config)
1515
const babelOptions = getBabelOptions(filePath)
1616

1717
const res = typescript.transpileModule(scriptContent, tsconfig)

lib/utils.js

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const loadPartialConfig = require('@babel/core').loadPartialConfig
22
const createTransformer = require('ts-jest').createTransformer
33
const chalk = require('chalk')
4+
const fs = require('fs')
5+
const ts = require('typescript')
46
const path = require('path')
57

68
const fetchTransformer = function fetchTransformer(key, obj) {
@@ -62,10 +64,46 @@ const getBabelOptions = function loadBabelOptions(filename, options = {}) {
6264
return loadPartialConfig(opts).options
6365
}
6466

65-
const getTsJestConfig = function getTsJestConfig(config) {
66-
const tr = createTransformer()
67-
const { typescript } = tr.configsFor(config)
68-
return { compilerOptions: typescript.options }
67+
const getTSConfig = function getTSConfig(config = {}) {
68+
const vueTsConfig = getVueJestConfig(config)
69+
if (typeof vueTsConfig.tsConfig === 'string') {
70+
return parseTSConfigFile(vueTsConfig.tsConfig)
71+
} else if (typeof vueTsConfig.tsConfig === 'object') {
72+
return vueTsConfig.tsConfig
73+
} else if (config.globals && config.globals['ts-jest']) {
74+
const tr = createTransformer()
75+
const {
76+
typescript: { options: compilerOptions }
77+
} = tr.configsFor(config)
78+
return { compilerOptions }
79+
} else {
80+
// auto pick tsconfig from the root folder
81+
return parseTSConfigFile()
82+
}
83+
}
84+
85+
const parseTSConfigFile = function parseTSConfigFile(
86+
pathToConfig = path.resolve(process.cwd(), './tsconfig.json')
87+
) {
88+
if (!fs.existsSync(pathToConfig)) {
89+
return {
90+
compilerOptions: {}
91+
}
92+
}
93+
const configJson = ts.parseConfigFileTextToJson(
94+
pathToConfig,
95+
ts.sys.readFile(pathToConfig)
96+
)
97+
const { options: compilerOptions } = ts.parseJsonConfigFileContent(
98+
configJson.config,
99+
ts.sys,
100+
ts.getDirectoryPath(pathToConfig),
101+
{},
102+
pathToConfig
103+
)
104+
return {
105+
compilerOptions
106+
}
69107
}
70108

71109
function isValidTransformer(transformer) {
@@ -119,7 +157,7 @@ module.exports = {
119157
throwError,
120158
logResultErrors,
121159
getCustomTransformer,
122-
getTsJestConfig,
160+
getTSConfig,
123161
getBabelOptions,
124162
getVueJestConfig,
125163
transformContent,

0 commit comments

Comments
 (0)