Files
vue-cli/packages/@vue/cli-plugin-unit-jest/generator/index.js
T
Haoqun Jiang ed50ceb255 refactor: rename jest presets (#4609)
* refactor: rename jest presets

Follow up of #4597.
Renamed `preset` folder to `presets`.
Added a default preset and it is accessible in the short form of
`@vue/cli-plugin-unit-jest`.

This change is inspired by ts-jest presets:
https://kulshekhar.github.io/ts-jest/user/config/#jest-preset

* fix: update import path

* fix: fix typo in file path
2019-09-25 00:02:39 +08:00

72 lines
1.6 KiB
JavaScript

module.exports = (api, _, __, invoking) => {
api.render('./template', {
hasTS: api.hasPlugin('typescript')
})
api.extendPackage({
scripts: {
'test:unit': 'vue-cli-service test:unit'
},
devDependencies: {
'@vue/test-utils': '1.0.0-beta.29'
},
jest: {
preset: api.hasPlugin('babel')
? '@vue/cli-plugin-unit-jest'
: '@vue/cli-plugin-unit-jest/presets/no-babel'
}
})
if (api.hasPlugin('eslint')) {
api.extendPackage({
eslintConfig: {
overrides: [
{
files: [
'**/__tests__/*.{j,t}s?(x)',
'**/tests/unit/**/*.spec.{j,t}s?(x)'
],
env: {
jest: true
}
}
]
}
})
}
if (api.hasPlugin('typescript')) {
applyTS(api, invoking)
}
}
const applyTS = (module.exports.applyTS = (api, invoking) => {
api.extendPackage({
jest: {
preset: api.hasPlugin('babel')
? '@vue/cli-plugin-unit-jest/presets/typescript-and-babel'
: '@vue/cli-plugin-unit-jest/presets/typescript'
},
devDependencies: {
'@types/jest': '^24.0.11'
}
})
if (invoking) {
// inject jest type to tsconfig.json
api.render(files => {
const tsconfig = files['tsconfig.json']
if (tsconfig) {
const parsed = JSON.parse(tsconfig)
if (
parsed.compilerOptions.types &&
!parsed.compilerOptions.types.includes('jest')
) {
parsed.compilerOptions.types.push('jest')
}
files['tsconfig.json'] = JSON.stringify(parsed, null, 2)
}
})
}
})