Skip to content

Commit f0d66a8

Browse files
committed
feat: allow specifying the babelrc file as a jest global
Closes vuejs#66
1 parent 596e035 commit f0d66a8

7 files changed

+57
-19
lines changed

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,18 @@ To define a tsconfig file that vue-jest will use when transpiling typescript, yo
6969
}
7070
```
7171

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+
7284
### Supported template languages
7385

7486
- **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) {
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

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ module.exports = function compileTypescript (scriptContent, vueJestConfig) {
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/process.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ function processScript (scriptPart, 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) {

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)

0 commit comments

Comments
 (0)