Skip to content

Commit ce6ffa9

Browse files
authored
feat: use babel-jest to compile (vuejs#127)
1 parent 7085dda commit ce6ffa9

16 files changed

+1640
-754
lines changed

lib/compilers/babel-compiler.js

Lines changed: 0 additions & 27 deletions
This file was deleted.

lib/compilers/coffee-compiler.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
var ensureRequire = require('../ensure-require.js')
1+
const 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, vueJestConfig, filePath) {
5+
module.exports = function (raw, config, filePath) {
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(vueJestConfig, filePath)
13+
transpile: loadBabelConfig(config, filePath)
1414
})
1515
} catch (err) {
1616
throwError(err)

lib/compilers/typescript-compiler.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
const ensureRequire = require('../ensure-require')
2-
const compileBabel = require('./babel-compiler')
32
const loadBabelConfig = require('../load-babel-config.js')
43
const { loadTypescriptConfig } = require('../load-typescript-config')
4+
const babelJest = require('babel-jest')
5+
const getVueJestConfig = require('../get-vue-jest-config')
56

6-
module.exports = function compileTypescript (scriptContent, vueJestConfig, filePath) {
7+
module.exports = function compileTypescript (scriptContent, filePath, config) {
78
ensureRequire('typescript', ['typescript'])
9+
const vueJestConfig = getVueJestConfig(config)
810
const typescript = require('typescript')
911
const tsConfig = loadTypescriptConfig(vueJestConfig)
1012

@@ -15,14 +17,19 @@ module.exports = function compileTypescript (scriptContent, vueJestConfig, fileP
1517

1618
// handle ES modules in TS source code in case user uses non commonjs module
1719
// output and there is no .babelrc.
18-
let inlineBabelConfig
19-
if (tsConfig.compilerOptions.module !== 'commonjs' && !loadBabelConfig(vueJestConfig, filePath)) {
20+
let inlineBabelConfig = {}
21+
if (tsConfig.compilerOptions.module !== 'commonjs' && !loadBabelConfig(vueJestConfig)) {
2022
inlineBabelConfig = {
2123
plugins: [
2224
require('babel-plugin-transform-es2015-modules-commonjs')
2325
]
2426
}
2527
}
26-
27-
return compileBabel(res.outputText, inputSourceMap, inlineBabelConfig, vueJestConfig)
28+
const transformer = babelJest.createTransformer(Object.assign(
29+
inlineBabelConfig,
30+
{
31+
inputSourceMap
32+
}
33+
))
34+
return transformer.process(res.outputText, filePath, config)
2835
}

lib/load-babel-config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ const cache = require('./cache')
44
const deprecate = require('./deprecate')
55
const path = require('path')
66
const { readFileSync, existsSync } = require('fs')
7+
const getVueJestConfig = require('./get-vue-jest-config')
8+
9+
module.exports = function getBabelConfig (config, filePath) {
10+
const vueJestConfig = getVueJestConfig(config)
711

8-
module.exports = function getBabelConfig (vueJestConfig, filePath) {
912
const find = () => {
1013
const { file, config } = findBabelConfig.sync(filePath || process.cwd())
1114

lib/load-typescript-config.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const tsconfig = require('tsconfig')
22
const cache = require('./cache')
3-
const deprecate = require('./deprecate')
43
const logger = require('./logger')
4+
const getVueJestConfig = require('./get-vue-jest-config')
55

66
const defaultTypescriptConfig = {
77
'compilerOptions': {
@@ -27,7 +27,9 @@ const defaultTypescriptConfig = {
2727
}
2828
}
2929

30-
module.exports.loadTypescriptConfig = function loadTypescriptConfig (vueJestConfig) {
30+
module.exports.loadTypescriptConfig = function loadTypescriptConfig (config) {
31+
const vueJestConfig = getVueJestConfig(config)
32+
3133
const find = () => {
3234
const { path, config } = tsconfig.loadSync(process.cwd())
3335

@@ -43,10 +45,7 @@ module.exports.loadTypescriptConfig = function loadTypescriptConfig (vueJestConf
4345
} else {
4446
let typescriptConfig
4547

46-
if (vueJestConfig.tsConfigFile) {
47-
deprecate.replace('tsConfigFile', 'tsConfig')
48-
typescriptConfig = tsconfig.readFileSync(vueJestConfig.tsConfigFile)
49-
} else if (vueJestConfig.hasOwnProperty('tsConfig')) {
48+
if (vueJestConfig.hasOwnProperty('tsConfig')) {
5049
switch (typeof vueJestConfig.tsConfig) {
5150
case 'string':
5251
// a path to a config file is being passed in; load it

lib/process.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ const vueCompiler = require('vue-template-compiler')
22
const compileTemplate = require('./template-compiler')
33
const generateSourceMap = require('./generate-source-map')
44
const addTemplateMapping = require('./add-template-mapping')
5-
const compileBabel = require('./compilers/babel-compiler')
65
const compileTypescript = require('./compilers/typescript-compiler')
76
const compileCoffeeScript = require('./compilers/coffee-compiler')
87
const processStyle = require('./process-style')
@@ -12,35 +11,35 @@ const path = require('path')
1211
const join = path.join
1312
const logger = require('./logger')
1413
const splitRE = /\r?\n/g
14+
const babelJest = require('babel-jest')
1515

16-
function processScript (scriptPart, vueJestConfig, filePath) {
16+
function processScript (scriptPart, filePath, config) {
1717
if (!scriptPart) {
1818
return { code: '' }
1919
}
2020

2121
if (/^typescript|tsx?$/.test(scriptPart.lang)) {
22-
return compileTypescript(scriptPart.content, vueJestConfig, filePath)
22+
return compileTypescript(scriptPart.content, filePath, config)
2323
}
2424

2525
if (scriptPart.lang === 'coffee' || scriptPart.lang === 'coffeescript') {
26-
return compileCoffeeScript(scriptPart.content, vueJestConfig, filePath)
26+
return compileCoffeeScript(scriptPart.content, config, filePath)
2727
}
2828

29-
return compileBabel(scriptPart.content, undefined, undefined, vueJestConfig, filePath)
29+
return babelJest.process(scriptPart.content, filePath, config)
3030
}
3131

32-
module.exports = function (src, filePath, jestConfig) {
33-
const vueJestConfig = getVueJestConfig(jestConfig)
34-
32+
module.exports = function (src, filePath, config) {
33+
const vueJestConfig = getVueJestConfig(config)
3534
var parts = vueCompiler.parseComponent(src, { pad: true })
3635

3736
if (parts.script && parts.script.src) {
3837
parts.script.content = fs.readFileSync(join(filePath, '..', parts.script.src), 'utf8')
3938
}
4039

41-
const result = processScript(parts.script, vueJestConfig, filePath)
40+
const result = processScript(parts.script, filePath, config)
4241
const script = result.code
43-
const inputMap = result.sourceMap
42+
const inputMap = result.map
4443

4544
let scriptSrc = src
4645
if (parts.script && parts.script.src) {
@@ -89,7 +88,7 @@ module.exports = function (src, filePath, jestConfig) {
8988
.map(ast => {
9089
const styleObj = (/^less|pcss|postcss/.test(ast.lang))
9190
? {}
92-
: processStyle(ast, filePath, jestConfig)
91+
: processStyle(ast, filePath, config)
9392

9493
const moduleName = ast.module === true ? '$style' : ast.module
9594

package.json

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"devDependencies": {
3333
"@vue/test-utils": "^1.0.0-beta.25",
3434
"babel-core": "^6.25.0",
35-
"babel-jest": "^20.0.3",
35+
"babel-jest": "^23.6.0",
3636
"babel-plugin-istanbul": "^4.1.4",
3737
"babel-preset-env": "^1.6.1",
3838
"clear-module": "^2.1.0",
@@ -44,7 +44,7 @@
4444
"eslint-plugin-vue-libs": "^1.2.0",
4545
"hamljs": "^0.6.2",
4646
"jade": "^1.11.0",
47-
"jest": "^20.0.4",
47+
"jest": "^23.6.0",
4848
"node-sass": "^4.7.2",
4949
"pug": "^2.0.0-rc.3",
5050
"stylus": "^0.54.5",
@@ -54,6 +54,7 @@
5454
},
5555
"peerDependencies": {
5656
"babel-core": "^6.25.0 || ^7.0.0-0",
57+
"jest": "^23.x",
5758
"vue": "^2.x",
5859
"vue-template-compiler": "^2.x"
5960
},
@@ -79,10 +80,16 @@
7980
"^.+\\.js$": "<rootDir>/node_modules/babel-jest",
8081
".*\\.(vue)$": "<rootDir>/vue-jest.js"
8182
},
82-
"mapCoverage": true,
8383
"moduleNameMapper": {
8484
"^~?__root/(.*)$": "<rootDir>/$1",
8585
"^~?__test/(.*)$": "<rootDir>/test/$1"
86+
},
87+
"globals": {
88+
"vue-jest": {
89+
"pug": {
90+
"basedir": "test"
91+
}
92+
}
8693
}
8794
},
8895
"repository": {

test/Babel.spec.js

Lines changed: 1 addition & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
import Vue from 'vue'
22
import Basic from './resources/Basic.vue'
33
import BasicSrc from './resources/BasicSrc.vue'
4-
import jestVue from '../vue-jest'
5-
import { resolve } from 'path'
6-
import {
7-
readFileSync,
8-
writeFileSync,
9-
renameSync
10-
} from 'fs'
114
import clearModule from 'clear-module'
125
import cache from '../lib/cache'
136

@@ -25,99 +18,5 @@ test('processes .vue files', () => {
2518
test('processes .vue files using src attributes', () => {
2619
const vm = new Vue(BasicSrc).$mount()
2720
expect(typeof vm.$el).toBe('object')
28-
})
29-
30-
test('skip processing if there is no .babelrc', () => {
31-
const babelRcPath = resolve(__dirname, '../.babelrc')
32-
const babelRcPath2 = resolve(__dirname, '../../.babelrc')
33-
const tempPath = resolve(__dirname, '../.renamed')
34-
const tempPath2 = resolve(__dirname, '../../.renamed')
35-
renameSync(babelRcPath, tempPath)
36-
renameSync(babelRcPath2, tempPath2)
37-
const filePath = resolve(__dirname, './resources/Basic.vue')
38-
const fileString = readFileSync(filePath, { encoding: 'utf8' })
39-
try {
40-
jestVue.process(fileString, filePath)
41-
} catch (err) {
42-
renameSync(tempPath, babelRcPath)
43-
renameSync(tempPath2, babelRcPath2)
44-
throw err
45-
}
46-
renameSync(tempPath, babelRcPath)
47-
renameSync(tempPath2, babelRcPath2)
48-
})
49-
50-
test('logs info when there is no .babelrc', () => {
51-
const babelRcPath = resolve(__dirname, '../.babelrc')
52-
const babelRcPath2 = resolve(__dirname, '../../.babelrc')
53-
const tempPath = resolve(__dirname, '../.renamed')
54-
const tempPath2 = resolve(__dirname, '../../.renamed')
55-
renameSync(babelRcPath, tempPath)
56-
renameSync(babelRcPath2, tempPath2)
57-
const info = jest.spyOn(global.console, 'info')
58-
const filePath = resolve(__dirname, './resources/Basic.vue')
59-
const fileString = readFileSync(filePath, { encoding: 'utf8' })
60-
61-
jestVue.process(fileString, filePath)
62-
try {
63-
expect(info).toHaveBeenCalledWith('\n[vue-jest]: no .babelrc found, skipping babel compilation\n')
64-
} catch (err) {
65-
renameSync(tempPath, babelRcPath)
66-
renameSync(tempPath2, babelRcPath2)
67-
throw err
68-
}
69-
renameSync(tempPath, babelRcPath)
70-
renameSync(tempPath2, babelRcPath2)
71-
jest.resetModules()
72-
})
73-
74-
test('uses babelrc in package.json if none in .babelrc', () => {
75-
const babelRcPath = resolve(__dirname, '../.babelrc')
76-
const tempPath = resolve(__dirname, '../.renamed')
77-
const packagePath = resolve(__dirname, '../package.json')
78-
const packageOriginal = readFileSync(packagePath, { encoding: 'utf8' })
79-
writeFileSync(packagePath, '{ "babel": {"presets": ["env"],"plugins": ["istanbul"]}}')
80-
renameSync(babelRcPath, tempPath)
81-
const filePath = resolve(__dirname, './resources/Basic.vue')
82-
const fileString = readFileSync(filePath, { encoding: 'utf8' })
83-
84-
try {
85-
const output = jestVue.process(fileString, filePath)
86-
expect(output.code).toContain('coverageData.hash')
87-
} catch (err) {
88-
renameSync(tempPath, babelRcPath)
89-
writeFileSync(packagePath, packageOriginal)
90-
jest.resetModules()
91-
throw err
92-
}
93-
renameSync(tempPath, babelRcPath)
94-
writeFileSync(packagePath, packageOriginal)
95-
jest.resetModules()
96-
})
97-
98-
test('processes .vue files using .babelrc if it exists in route', () => {
99-
const babelRcPath = resolve(__dirname, '../.babelrc')
100-
const babelRcOriginal = readFileSync(babelRcPath, { encoding: 'utf8' })
101-
writeFileSync(babelRcPath, '{"presets": ["env"],"plugins": ["istanbul"]}')
102-
const filePath = resolve(__dirname, './resources/Basic.vue')
103-
const fileString = readFileSync(filePath, { encoding: 'utf8' })
104-
105-
const output = jestVue.process(fileString, filePath)
106-
writeFileSync(babelRcPath, babelRcOriginal)
107-
// coverageData.hash is added by babel-plugin-istanbul, added to root .babelrc for this test only
108-
expect(output.code).toContain('coverageData.hash')
109-
})
110-
111-
test('generates inline sourcemap', () => {
112-
const filePath = resolve(__dirname, './resources/Basic.vue')
113-
const fileString = readFileSync(filePath, { encoding: 'utf8' })
114-
const output = jestVue.process(fileString, filePath)
115-
expect(output.code).toMatchSnapshot()
116-
})
117-
118-
test('generates inline sourcemap for .vue files using src attributes', () => {
119-
const filePath = resolve(__dirname, './resources/BasicSrc.vue')
120-
const fileString = readFileSync(filePath, { encoding: 'utf8' })
121-
const output = jestVue.process(fileString, filePath)
122-
expect(output.code).toMatchSnapshot()
21+
vm.toggleClass()
12322
})

0 commit comments

Comments
 (0)