Skip to content

Commit

Permalink
feat: jest v27 support for vue2-jest (#368)
Browse files Browse the repository at this point in the history
  • Loading branch information
cexbrayat committed Aug 1, 2021
1 parent edf2633 commit 3a0f4fe
Show file tree
Hide file tree
Showing 14 changed files with 87 additions and 1,187 deletions.
7 changes: 4 additions & 3 deletions e2e/2.x/babel-in-package/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
"@babel/preset-env": "^7.9.0",
"@vue/test-utils": "^1.1.0",
"coffeescript": "^2.3.2",
"jest": "26.x",
"ts-jest": "^26.3.0",
"jest": "27.x",
"ts-jest": "^27.0.4",
"typescript": "^3.2.2",
"vue2-jest": "~26.0.0"
"vue2-jest": "~27.0.0-alpha.1"
},
"jest": {
"testEnvironment": "jsdom",
"moduleFileExtensions": [
"js",
"json",
Expand Down
7 changes: 4 additions & 3 deletions e2e/2.x/basic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@
"babel-plugin-transform-vue-jsx": "^3.7.0",
"coffeescript": "^2.3.2",
"jade": "^1.11.0",
"jest": "26.x",
"jest": "27.x",
"pug": "^3.0.1",
"sass": "^1.23.7",
"ts-jest": "^26.3.0",
"ts-jest": "^27.0.4",
"typescript": "^3.2.2",
"vue2-jest": "~26.0.0"
"vue2-jest": "~27.0.0-alpha.1"
},
"jest": {
"testEnvironment": "jsdom",
"moduleFileExtensions": [
"js",
"json",
Expand Down
11 changes: 9 additions & 2 deletions e2e/2.x/basic/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,12 @@ test('handles named exports', () => {
test('generates source maps for .vue files', () => {
const filePath = resolve(__dirname, './components/Basic.vue')
const fileString = readFileSync(filePath, { encoding: 'utf8' })
const config = {
moduleFileExtensions: ['js', 'vue']
}

const { map } = jestVue.process(fileString, filePath, {
moduleFileExtensions: ['js', 'vue']
config
})

expect(map).toMatchSnapshot()
Expand All @@ -50,8 +53,12 @@ test('generates source maps using src attributes', () => {
const filePath = resolve(__dirname, './components/SourceMapsSrc.vue')
const fileString = readFileSync(filePath, { encoding: 'utf8' })

const { map } = jestVue.process(fileString, filePath, {
const config = {
moduleFileExtensions: ['js', 'vue']
}

const { map } = jestVue.process(fileString, filePath, {
config
})

expect(map).toMatchSnapshot()
Expand Down
2 changes: 1 addition & 1 deletion e2e/2.x/custom-transformers/babel-transformer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { createTransformer } = require('babel-jest')
const { createTransformer } = require('babel-jest').default
module.exports = createTransformer({
presets: ['@babel/preset-env']
})
7 changes: 4 additions & 3 deletions e2e/2.x/custom-transformers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
"@babel/core": "^7.9.0",
"@babel/preset-env": "^7.9.0",
"@vue/test-utils": "^1.1.0",
"babel-jest": "^26.6.3",
"jest": "26.x",
"babel-jest": "^27.0.6",
"jest": "27.x",
"postcss": "^7.0.13",
"postcss-color-function": "^4.0.1",
"sass": "^1.23.7",
"vue2-jest": "~26.0.0"
"vue2-jest": "~27.0.0-alpha.1"
},
"jest": {
"testEnvironment": "jsdom",
"moduleFileExtensions": [
"js",
"json",
Expand Down
5 changes: 3 additions & 2 deletions e2e/2.x/style/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
"@babel/core": "^7.9.0",
"@babel/preset-env": "^7.9.0",
"@vue/test-utils": "^1.1.0",
"jest": "26.x",
"jest": "27.x",
"less": "^3.9.0",
"postcss": "^7.0.13",
"sass": "^1.23.7",
"stylus": "^0.54.5",
"vue2-jest": "~26.0.0"
"vue2-jest": "~27.0.0-alpha.1"
},
"jest": {
"testEnvironment": "jsdom",
"moduleFileExtensions": [
"js",
"json",
Expand Down
9 changes: 4 additions & 5 deletions packages/vue2-jest/lib/index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
const crypto = require('crypto')
const babelJest = require('babel-jest')

const babelJest = require('babel-jest').default
module.exports = {
process: require('./process'),
getCacheKey: function getCacheKey(
fileData,
filename,
configString,
{ config, instrument, rootDir }
{ config, configString, instrument, rootDir }
) {
return crypto
.createHash('md5')
.update(
babelJest.getCacheKey(fileData, filename, configString, {
babelJest.getCacheKey(fileData, filename, {
config,
configString,
instrument,
rootDir
}),
Expand Down
4 changes: 2 additions & 2 deletions packages/vue2-jest/lib/process-style.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ module.exports = function processStyle(stylePart, filePath, config = {}) {
const cssFilePath = applyModuleNameMapper(
stylePart.src,
filePath,
config,
config.config,
stylePart.lang
)
stylePart.content = loadSrc(cssFilePath, filePath)
Expand Down Expand Up @@ -86,7 +86,7 @@ module.exports = function processStyle(stylePart, filePath, config = {}) {
const preprocessOptions = getPreprocessOptions(
stylePart.lang,
filePath,
config
config.config
)
const result = compileStyle({
source: content,
Expand Down
2 changes: 1 addition & 1 deletion packages/vue2-jest/lib/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const logResultErrors = require('./utils').logResultErrors
const stripInlineSourceMap = require('./utils').stripInlineSourceMap
const getCustomTransformer = require('./utils').getCustomTransformer
const loadSrc = require('./utils').loadSrc
const babelTransformer = require('babel-jest')
const babelTransformer = require('babel-jest').default
const compilerUtils = require('@vue/component-compiler-utils')
const generateCode = require('./generate-code')

Expand Down
2 changes: 1 addition & 1 deletion packages/vue2-jest/lib/transformers/coffee.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const throwError = require('../utils').throwError
const getBabelOptions = require('../utils').getBabelOptions

module.exports = {
process(src, filename, config) {
process(src, filename) {
ensureRequire('coffee', ['coffeescript'])
const coffee = require('coffeescript')
const babelOptions = getBabelOptions(filename)
Expand Down
26 changes: 6 additions & 20 deletions packages/vue2-jest/lib/transformers/typescript.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const ensureRequire = require('../ensure-require')
const babelJest = require('babel-jest')
const babelJest = require('babel-jest').default
const {
getBabelOptions,
getTsJestConfig,
stripInlineSourceMap,
getCustomTransformer,
Expand All @@ -14,35 +13,22 @@ module.exports = {
const typescript = require('typescript')
const vueJestConfig = getVueJestConfig(config)
const tsconfig = getTsJestConfig(config)
const babelOptions = getBabelOptions(filePath)

const res = typescript.transpileModule(scriptContent, tsconfig)
const res = typescript.transpileModule(scriptContent, {
...tsconfig,
fileName: filePath
})

res.outputText = stripInlineSourceMap(res.outputText)

const inputSourceMap =
res.sourceMapText !== undefined ? JSON.parse(res.sourceMapText) : ''

// handle ES modules in TS source code in case user uses non commonjs module
// output and there is no .babelrc.
let inlineBabelOptions = {}
if (
tsconfig.compilerOptions.module !== typescript.ModuleKind.CommonJS &&
!babelOptions
) {
inlineBabelOptions = {
plugins: [require('@babel/plugin-transform-modules-commonjs')]
}
}
const customTransformer =
getCustomTransformer(vueJestConfig['transform'], 'js') || {}
const transformer = customTransformer.process
? customTransformer
: babelJest.createTransformer(
Object.assign(inlineBabelOptions, {
inputSourceMap
})
)
: babelJest.createTransformer({ inputSourceMap })

return transformer.process(res.outputText, filePath, config)
}
Expand Down
15 changes: 10 additions & 5 deletions packages/vue2-jest/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ const transformContent = function transformContent(

const getVueJestConfig = function getVueJestConfig(jestConfig) {
return (
(jestConfig && jestConfig.globals && jestConfig.globals['vue-jest']) || {}
(jestConfig &&
jestConfig.config &&
jestConfig.config.globals &&
jestConfig.config.globals['vue-jest']) ||
{}
)
}
const getBabelOptions = function loadBabelOptions(filename, options = {}) {
Expand All @@ -64,11 +68,12 @@ const getBabelOptions = function loadBabelOptions(filename, options = {}) {
}

const getTsJestConfig = function getTsJestConfig(config) {
const createTransformer = require('ts-jest').createTransformer
const tr = createTransformer()
const configSet = tr.configsFor(config)
const { ConfigSet } = require('ts-jest/dist/config/config-set')
const configSet = new ConfigSet(config.config)
var tsConfig = configSet.typescript || configSet.parsedTsConfig
return { compilerOptions: tsConfig.options }
return {
compilerOptions: { ...tsConfig.options, target: 'es5', module: 'commonjs' }
}
}

function isValidTransformer(transformer) {
Expand Down
14 changes: 7 additions & 7 deletions packages/vue2-jest/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vue2-jest",
"version": "26.0.0",
"version": "27.0.0-alpha.1",
"description": "Jest transformer for Vue 2",
"main": "lib/index.js",
"files": [
Expand Down Expand Up @@ -28,20 +28,20 @@
"@babel/preset-env": "^7.9.0",
"@vue/test-utils": "^1.1.0",
"babel-core": "^7.0.0-bridge.0",
"babel-jest": "^23.6.0",
"babel-jest": "^27.0.6",
"conventional-changelog": "^1.1.5",
"jest": "^26.0.0",
"jest": "^27.0.6",
"semantic-release": "^15.13.2",
"ts-jest": "^26.3.0",
"ts-jest": "^27.0.4",
"typescript": "^3.2.2",
"vue": "^2.4.2",
"vue-template-compiler": "^2.4.2"
},
"peerDependencies": {
"@babel/core": "7.x",
"babel-jest": ">= 24 < 27",
"jest": "26.x",
"ts-jest": ">= 24 < 27 ",
"babel-jest": ">= 27 < 28",
"jest": "27.x",
"ts-jest": ">= 27 < 28",
"vue": "^2.x",
"vue-template-compiler": "^2.x"
},
Expand Down

0 comments on commit 3a0f4fe

Please sign in to comment.