Skip to content

Commit ba46b9f

Browse files
committed
feat(babel): use root .babelrc if exists
1 parent 05e39c0 commit ba46b9f

File tree

5 files changed

+382
-220
lines changed

5 files changed

+382
-220
lines changed

jest-vue.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
11
const vueCompiler = require('vue-template-compiler')
2-
const babel = require('babel-core')
32
const compileTemplate = require('./lib/template-compiler')
43
const generateSourceMap = require('./lib/generate-source-map')
54
const addTemplateMapping = require('./lib/add-template-mapping')
65
const convertSourceMap = require('convert-source-map')
76
const typescript = require('typescript')
7+
const compileBabel = require('./lib/compilers/babel-compiler')
88

99
const splitRE = /\r?\n/g
1010

11-
function compileBabel (scriptContent) {
12-
return babel.transform(scriptContent, {
13-
sourceMaps: true,
14-
presets: ['es2015'],
15-
plugins: ['transform-runtime']
16-
})
17-
}
18-
1911
function compileTypescript (scriptContent) {
2012
return {
2113
code: typescript.transpile(scriptContent)

lib/compilers/babel-compiler.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const babel = require('babel-core')
2+
const path = require('path')
3+
const fs = require('fs')
4+
5+
var defaultBabelOptions = {
6+
presets: ['es2015'],
7+
plugins: ['transform-runtime']
8+
}
9+
10+
function getBabelRc (path) {
11+
var rc
12+
try {
13+
rc = JSON.parse(fs.readFileSync(path, 'utf-8'))
14+
} catch (e) {
15+
throw new Error('[vue-jest] Your .babelrc seems to be incorrectly formatted.')
16+
}
17+
return rc
18+
}
19+
20+
module.exports = function compileBabel (scriptContent) {
21+
const babelRcPath = path.resolve(process.cwd(), '.babelrc')
22+
const babelRcExists = fs.existsSync(babelRcPath);
23+
const baseBabelOptions = babelRcExists ? getBabelRc(babelRcPath) : defaultBabelOptions
24+
const babelOptions = Object.assign({sourceMaps: true}, baseBabelOptions)
25+
26+
return babel.transform(scriptContent, babelOptions)
27+
}

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
"devDependencies": {
1717
"babel-core": "^6.25.0",
1818
"babel-jest": "^20.0.3",
19+
"babel-plugin-external-helpers": "^6.22.0",
20+
"babel-plugin-istanbul": "^4.1.4",
1921
"babel-plugin-transform-runtime": "^6.23.0",
2022
"babel-preset-env": "^1.6.0",
2123
"babel-preset-es2015": "^6.24.1",

test/Basic.spec.js renamed to test/Babel.spec.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
11
import Vue from 'vue'
2-
import Hello from './resources/Basic.vue'
2+
import Basic from './resources/Basic.vue'
33
import jestVue from '../jest-vue'
44
import { resolve } from 'path'
5-
import { readFileSync } from 'fs'
5+
import { readFileSync, writeFileSync } from 'fs'
66

77
test('processes .vue files', () => {
8-
const vm = new Vue(Hello).$mount()
8+
const vm = new Vue(Basic).$mount()
99
expect(typeof vm.$el).toBe('object')
1010
})
1111

12+
test('processes .vue files using .babelrc if it exists in route', () => {
13+
const babelRcPath = resolve(__dirname, '../.babelrc')
14+
const babelRcOriginal = readFileSync(babelRcPath, { encoding: 'utf8' })
15+
writeFileSync(babelRcPath, '{"presets": ["es2015", "stage-2"],"plugins": ["istanbul"]}')
16+
const filePath = resolve(__dirname, './resources/Basic.vue')
17+
const fileString = readFileSync(filePath, { encoding: 'utf8' })
18+
19+
const output = jestVue.process(fileString, filePath)
20+
writeFileSync(babelRcPath, babelRcOriginal)
21+
// coverageData.hash is added by babel-plugin-istanbul, added to root .babelrc for this test only
22+
expect(output).toContain('coverageData.hash')
23+
})
24+
1225
test('generates inline sourcemap', () => {
1326
const expectedMap = '//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIkJhc2ljLnZ1ZSJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7QUFBQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7OztBQUdBO0FBQ0E7QUFDQT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AgICAgICAgICAgICB0aGlzLmlzQ3JhenkgPSAhdGhpcy5pc0NyYXp5O1xuICAgICAgICAgICAgfSxcbiAgICAgICAgfSxcbiAgICB9O1xuPC9zY3JpcHQ+XG4iXX0='
1427
const filePath = resolve(__dirname, './resources/Basic.vue')

0 commit comments

Comments
 (0)