Skip to content

Commit fc3164d

Browse files
dchermaneddyerburgh
authored andcommitted
feat: add config options for Babel and Typescript (#87)
1 parent f150076 commit fc3164d

9 files changed

+110
-33
lines changed

README.md

+24
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,30 @@ vue-jest compiles the script and template of SFCs into a JavaScript file that Je
5757
- **typescript** (`lang="ts"`, `lang="typescript"`)
5858
- **coffeescript** (`lang="coffee"`, `lang="coffeescript"`)
5959

60+
To define a tsconfig file that vue-jest will use when transpiling typescript, you can specify it in the jest globals
61+
62+
```json
63+
{
64+
"jest": {
65+
"vue-jest": {
66+
"tsConfigFile": "tsconfig.jest.json"
67+
}
68+
}
69+
}
70+
```
71+
72+
To define a babelrc file that vue-jest will use when transpiling javascript, you can specify it in the jest globals
73+
74+
```json
75+
{
76+
"jest": {
77+
"vue-jest": {
78+
"babelRcFile": "jest.babelrc"
79+
}
80+
}
81+
}
82+
```
83+
6084
### Supported template languages
6185

6286
- **pug** (`lang="pug"`)

lib/compilers/babel-compiler.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
const babel = require('babel-core')
22
const loadBabelConfig = require('../load-babel-config.js')
33

4-
module.exports = function compileBabel (scriptContent, inputSourceMap, inlineConfig) {
5-
const babelConfig = inlineConfig || loadBabelConfig()
4+
module.exports = function compileBabel (scriptContent, inputSourceMap, inlineConfig, vueJestConfig) {
5+
const babelConfig = inlineConfig || loadBabelConfig(vueJestConfig)
66

77
if (!babelConfig) {
88
return {

lib/compilers/coffee-compiler.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ var ensureRequire = require('../ensure-require.js')
22
const throwError = require('../throw-error')
33
const loadBabelConfig = require('../load-babel-config.js')
44

5-
module.exports = function (raw, cb, compiler) {
5+
module.exports = function (raw, vueJestConfig) {
66
ensureRequire('coffee', ['coffeescript'])
77
var coffee = require('coffeescript')
88
var compiled
99
try {
1010
compiled = coffee.compile(raw, {
1111
bare: true,
1212
sourceMap: true,
13-
transpile: loadBabelConfig()
13+
transpile: loadBabelConfig(vueJestConfig)
1414
})
1515
} catch (err) {
1616
throwError(err)

lib/compilers/typescript-compiler.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ const compileBabel = require('./babel-compiler')
33
const loadBabelConfig = require('../load-babel-config.js')
44
const { loadTypescriptConfig } = require('../load-typescript-config')
55

6-
module.exports = function compileTypescript (scriptContent) {
6+
module.exports = function compileTypescript (scriptContent, vueJestConfig) {
77
ensureRequire('typescript', ['typescript'])
88
const typescript = require('typescript')
9-
const tsConfig = loadTypescriptConfig()
9+
const tsConfig = loadTypescriptConfig(vueJestConfig)
1010

1111
const res = typescript.transpileModule(scriptContent, tsConfig)
1212
const inputSourceMap = (res.sourceMapText !== undefined)
@@ -16,13 +16,13 @@ module.exports = function compileTypescript (scriptContent) {
1616
// handle ES modules in TS source code in case user uses non commonjs module
1717
// output and there is no .babelrc.
1818
let inlineBabelConfig
19-
if (tsConfig.compilerOptions.module !== 'commonjs' && !loadBabelConfig()) {
19+
if (tsConfig.compilerOptions.module !== 'commonjs' && !loadBabelConfig(vueJestConfig)) {
2020
inlineBabelConfig = {
2121
plugins: [
2222
require('babel-plugin-transform-es2015-modules-commonjs')
2323
]
2424
}
2525
}
2626

27-
return compileBabel(res.outputText, inputSourceMap, inlineBabelConfig)
27+
return compileBabel(res.outputText, inputSourceMap, inlineBabelConfig, vueJestConfig)
2828
}

lib/load-babel-config.js

+19-8
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,32 @@
11
const findBabelConfig = require('find-babel-config')
22
const logger = require('./logger')
33
const cache = require('./cache')
4+
const { readFileSync } = require('fs')
45

5-
module.exports = function getBabelConfig () {
6+
module.exports = function getBabelConfig (vueJestConfig) {
67
const cachedConfig = cache.get('babel-config')
78
if (cachedConfig) {
89
return cachedConfig
910
} else if (cachedConfig === false) {
1011
return
1112
} else {
12-
const { file, config } = findBabelConfig.sync(process.cwd(), 0)
13-
if (!file) {
14-
logger.info('no .babelrc found, skipping babel compilation')
15-
cache.set('babel-config', false)
16-
return
13+
let babelConfig
14+
15+
if (vueJestConfig.babelRcFile) {
16+
babelConfig = JSON.parse(readFileSync(vueJestConfig.babelRcFile))
17+
} else {
18+
const { file, config } = findBabelConfig.sync(process.cwd(), 0)
19+
20+
if (!file) {
21+
logger.info('no .babelrc found, skipping babel compilation')
22+
cache.set('babel-config', false)
23+
return
24+
}
25+
26+
babelConfig = config
1727
}
18-
cache.set('babel-config', config)
19-
return config
28+
29+
cache.set('babel-config', babelConfig)
30+
return babelConfig
2031
}
2132
}

lib/load-typescript-config.js

+12-5
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,25 @@ const defaultTypescriptConfig = {
2626
}
2727
}
2828

29-
module.exports.loadTypescriptConfig = function loadTypescriptConfig () {
29+
module.exports.loadTypescriptConfig = function loadTypescriptConfig (vueJestConfig) {
3030
const cachedConfig = cache.get('typescript-config')
3131
if (cachedConfig) {
3232
return cachedConfig
3333
} else {
34-
const { path, config } = tsconfig.loadSync(process.cwd())
34+
let typescriptConfig
3535

36-
if (!path) {
37-
logger.info('no tsconfig.json found, defaulting to default typescript options')
36+
if (vueJestConfig.tsConfigFile) {
37+
typescriptConfig = tsconfig.readFileSync(vueJestConfig.tsConfigFile)
38+
} else {
39+
const { path, config } = tsconfig.loadSync(process.cwd())
40+
41+
if (!path) {
42+
logger.info('no tsconfig.json found, defaulting to default typescript options')
43+
}
44+
45+
typescriptConfig = path ? config : defaultTypescriptConfig
3846
}
3947

40-
const typescriptConfig = path ? config : defaultTypescriptConfig
4148
cache.set('typescript-config', typescriptConfig)
4249
return typescriptConfig
4350
}

lib/process.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,20 @@ const join = path.join
1414
const logger = require('./logger')
1515
const splitRE = /\r?\n/g
1616

17-
function processScript (scriptPart) {
17+
function processScript (scriptPart, vueJestConfig) {
1818
if (!scriptPart) {
1919
return { code: '' }
2020
}
2121

2222
if (/^typescript|tsx?$/.test(scriptPart.lang)) {
23-
return compileTypescript(scriptPart.content)
23+
return compileTypescript(scriptPart.content, vueJestConfig)
2424
}
2525

2626
if (scriptPart.lang === 'coffee' || scriptPart.lang === 'coffeescript') {
27-
return compileCoffeeScript(scriptPart.content)
27+
return compileCoffeeScript(scriptPart.content, vueJestConfig)
2828
}
2929

30-
return compileBabel(scriptPart.content)
30+
return compileBabel(scriptPart.content, undefined, undefined, vueJestConfig)
3131
}
3232

3333
function changePartsIfFunctional (parts) {
@@ -51,7 +51,7 @@ module.exports = function (src, filePath, jestConfig) {
5151
parts.script.content = fs.readFileSync(join(filePath, '..', parts.script.src), 'utf8')
5252
}
5353

54-
const result = processScript(parts.script)
54+
const result = processScript(parts.script, vueJestConfig)
5555
const script = result.code
5656
const inputMap = result.sourceMap
5757

test/load-babel-config.spec.js

+18-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import {
44
createReadStream,
55
createWriteStream,
66
readFileSync,
7-
renameSync
7+
renameSync,
8+
writeFileSync,
9+
unlinkSync
810
} from 'fs'
911
import clearModule from 'clear-module'
1012
import cache from '../lib/cache'
@@ -19,7 +21,7 @@ describe('load-babel-config.js', () => {
1921
const babelRcPath = resolve(__dirname, '../.babelrc')
2022
const tempPath = resolve(__dirname, '../.renamed')
2123
renameSync(babelRcPath, tempPath)
22-
const babelConfig = loadBabelConfig()
24+
const babelConfig = loadBabelConfig({})
2325
try {
2426
expect(babelConfig).toBe(undefined)
2527
} catch (err) {
@@ -31,12 +33,25 @@ describe('load-babel-config.js', () => {
3133
expect(babelConfigCached).toBe(undefined)
3234
})
3335

36+
it('reads babelrc from jest globals if exists', () => {
37+
const jestGlobalBabelPath = resolve(__dirname, '../jest.babelrc')
38+
writeFileSync(jestGlobalBabelPath, JSON.stringify({
39+
plugins: ['foo']
40+
}))
41+
const jestGlobalBabelConfig = JSON.parse(readFileSync(jestGlobalBabelPath, { encoding: 'utf8' }))
42+
const babelConfig = loadBabelConfig({
43+
babelRcFile: 'jest.babelrc'
44+
})
45+
expect(babelConfig).toEqual(jestGlobalBabelConfig)
46+
unlinkSync(jestGlobalBabelPath)
47+
})
48+
3449
it('reads default babel if there is .babelrc', () => {
3550
const babelRcPath = resolve(__dirname, '../.babelrc')
3651
const babelRcCopiedPath = resolve(__dirname, '../.babelrc_cp')
3752
createReadStream(babelRcPath).pipe(createWriteStream(babelRcCopiedPath))
3853
const babelRcOriginal = JSON.parse(readFileSync(babelRcPath, { encoding: 'utf8' }))
39-
const babelConfig = loadBabelConfig()
54+
const babelConfig = loadBabelConfig({})
4055
expect(babelConfig).toEqual(babelRcOriginal)
4156
const tempPath = resolve(__dirname, '../.renamed')
4257
renameSync(babelRcCopiedPath, tempPath)

test/load-typescript-config.spec.js

+24-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import {
44
createReadStream,
55
createWriteStream,
66
readFileSync,
7-
renameSync
7+
writeFileSync,
8+
renameSync,
9+
unlinkSync
810
} from 'fs'
911
import clearModule from 'clear-module'
1012
import cache from '../lib/cache'
@@ -20,7 +22,7 @@ describe('load-typescript-config.js', () => {
2022
const tempPath = resolve(__dirname, '../.renamed')
2123
renameSync(tsconfigPath, tempPath)
2224

23-
const tsConfig = loadTypescriptConfig()
25+
const tsConfig = loadTypescriptConfig({})
2426

2527
try {
2628
expect(tsConfig).toEqual(defaultConfig)
@@ -34,16 +36,34 @@ describe('load-typescript-config.js', () => {
3436
expect(tsconfigCachedConfig).toEqual(defaultConfig)
3537
})
3638

39+
it('returns the tsconfig specified in jest globals', () => {
40+
const jestGlobalTsConfigPath = resolve(__dirname, '../tsconfig.jest.json')
41+
42+
writeFileSync(jestGlobalTsConfigPath, JSON.stringify({
43+
allowJs: false
44+
}))
45+
46+
const jestGlobalTsConfig = JSON.parse(readFileSync(jestGlobalTsConfigPath, { encoding: 'utf8' }))
47+
48+
const tsconfig = loadTypescriptConfig({
49+
tsConfigFile: jestGlobalTsConfigPath
50+
})
51+
52+
expect(tsconfig).toEqual(jestGlobalTsConfig)
53+
54+
unlinkSync(jestGlobalTsConfigPath)
55+
})
56+
3757
it('reads default tsconfig if there is tsconfig.json', () => {
3858
const tsconfigPath = resolve(__dirname, '../tsconfig.json')
3959
const tsconfigCopiedPath = resolve(__dirname, '../.tsconfig.json_cp')
4060
createReadStream(tsconfigPath).pipe(createWriteStream(tsconfigCopiedPath))
4161
const tsconfigOriginal = JSON.parse(readFileSync(tsconfigPath, { encoding: 'utf8' }))
42-
const tsconfig = loadTypescriptConfig()
62+
const tsconfig = loadTypescriptConfig({})
4363
expect(tsconfig).toEqual(tsconfigOriginal)
4464
const tempPath = resolve(__dirname, '../.renamed')
4565
renameSync(tsconfigCopiedPath, tempPath)
46-
const tsconfigCached = loadTypescriptConfig()
66+
const tsconfigCached = loadTypescriptConfig({})
4767
try {
4868
expect(tsconfig).not.toBe(tsconfigCached)
4969
expect(tsconfig).toEqual(tsconfigCached)

0 commit comments

Comments
 (0)