Open zeroarst opened 5 years ago
having the. same issue, tried:
globals: {
'ts-jest': {
diagnostics: false,
},
'vue-jest': {
babelConfig: true,
},
},
to tell vue-jest to use babelConfig, but did not work...
@zeroarst your devDependencies
lists vue-jest
as using version ^1.0.2
, but at the time of your posting version 3 had already been out for a year. I'm not sure if upgrading will solve your problem, but it's worth trying first.
@zeroarst your
devDependencies
listsvue-jest
as using version^1.0.2
, but at the time of your posting version 3 had already been out for a year. I'm not sure if upgrading will solve your problem, but it's worth trying first.
I use vue-jest@3.0.6, still has this problem.
https://github.com/vuejs/vue/issues/11088 From this issue, optional chaining in template is not supported in vue@2.x.
vue-template-babel-compiler
might be what you are looking for:
https://github.com/JuniorTour/vue-template-babel-compiler/blob/main/doc/Usage.md#1-vue-jest
Only work for vue-jest >= 4.0.0 && jest <= 26.6.3
// jest.config.js module.exports = { moduleNameMapper: { '^@/(.*)$': '<rootDir>/$1', '^~/(.*)$': '<rootDir>/$1', '^vue$': 'vue/dist/vue.common.js', }, moduleFileExtensions: ['js', 'vue', 'json'], transform: { '^.+\\.js$': 'babel-jest', '.*\\.(vue)$': 'vue-jest', }, globals: { 'vue-jest': { templateCompiler: { compiler: require('vue-template-babel-compiler') } } } }
It doesnt work with jest > 28 with @vue/vue2-jest 28
It doesnt work with jest > 28 with @vue/vue2-jest 28
I am the same. I have confirmed that it does not work with the following versions.
// package.json
"vue": "^2.7.14",
"jest": "^28.0.2",
"@vue/vue2-jest": "28",
"babel-jest": "^28.0.2",
"vue-loader": "^15.10.0",
"vue-template-babel-compiler": "^2.0.0",
"@babel/plugin-proposal-optional-chaining": "^7.21.0",
"@babel/plugin-syntax-optional-chaining": "^7.8.3",
// jest.config.js
module.exports = {
'testRunner': 'jest-jasmine2',
'testEnvironment': 'jest-environment-jsdom',
'moduleNameMapper': {
'^@/(.*)$': '<rootDir>/$1',
'^~/(.*)$': '<rootDir>/$1',
'^vue$': 'vue/dist/vue.common.js',
'\\.(css|scss)$': '<rootDir>/node_modules/jest-css-modules',
'^@vue/test-utils$': require.resolve('@vue/test-utils'),
},
'moduleFileExtensions': ['js', 'vue', 'json'],
'transform': {
'^.+\\.js$': 'babel-jest',
'.*\\.(vue)$': '@vue/vue2-jest',
'.+\\.(css|styl|less|sass|scss|png|jpg|svg|ttf|woff|woff2)$':
'jest-transform-stub',
},
'globals': {
'@vue/vue2-jest': {
templateCompiler: {
compiler: require('vue-template-babel-compiler'),
transformAssetUrls: true,
},
},
},
'collectCoverage': true,
'collectCoverageFrom': [
'<rootDir>/components/**/*.vue',
'<rootDir>/pages/**/*.vue',
],
'setupFilesAfterEnv': ['./test/setup.js'],
};
FAIL test/pages/index.test.js
● Test suite failed to run
Jest encountered an unexpected token
Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.
Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.
By default "node_modules" folder is ignored by transformers.
Here's what you can do:
• If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
• If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
• To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
• If you need a custom transformation specify a "transform" option in your config.
• If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/configuration
For information about custom transformations, see:
https://jestjs.io/docs/code-transformation
Details:
SyntaxError: Unexpected token (3:346)
at Parser.pp$4.raise (node_modules/vue-template-es2015-compiler/buble.js:2757:13)
at Parser.pp.unexpected (node_modules/vue-template-es2015-compiler/buble.js:647:8)
at Parser.pp$3.parseExprAtom (node_modules/vue-template-es2015-compiler/buble.js:2196:10)
at Parser.<anonymous> (node_modules/vue-template-es2015-compiler/buble.js:6003:24)
at Parser.parseExprAtom (node_modules/vue-template-es2015-compiler/buble.js:6129:31)
at Parser.pp$3.parseExprSubscripts (node_modules/vue-template-es2015-compiler/buble.js:2047:19)
at Parser.pp$3.parseMaybeUnary (node_modules/vue-template-es2015-compiler/buble.js:2024:17)
at Parser.pp$3.parseExprOps (node_modules/vue-template-es2015-compiler/buble.js:1966:19)
at Parser.pp$3.parseMaybeConditional (node_modules/vue-template-es2015-compiler/buble.js:1949:19)
at Parser.pp$3.parseMaybeAssign (node_modules/vue-template-es2015-compiler/buble.js:1925:19)
at Parser.pp$3.parseMaybeConditional (node_modules/vue-template-es2015-compiler/buble.js:1954:28)
at Parser.pp$3.parseMaybeAssign (node_modules/vue-template-es2015-compiler/buble.js:1925:19)
at Parser.pp$3.parseParenAndDistinguishExpression (node_modules/vue-template-es2015-compiler/buble.js:2238:30)
at Parser.pp$3.parseExprAtom (node_modules/vue-template-es2015-compiler/buble.js:2163:41)
at Parser.<anonymous> (node_modules/vue-template-es2015-compiler/buble.js:6003:24)
at Parser.parseExprAtom (node_modules/vue-template-es2015-compiler/buble.js:6129:31)
at Parser.pp$3.parseExprSubscripts (node_modules/vue-template-es2015-compiler/buble.js:2047:19)
at Parser.pp$3.parseMaybeUnary (node_modules/vue-template-es2015-compiler/buble.js:2024:17)
at Parser.pp$3.parseExprOps (node_modules/vue-template-es2015-compiler/buble.js:1966:19)
at Parser.pp$3.parseMaybeConditional (node_modules/vue-template-es2015-compiler/buble.js:1949:19)
at Parser.pp$3.parseMaybeAssign (node_modules/vue-template-es2015-compiler/buble.js:1925:19)
at Parser.pp$3.parseExprList (node_modules/vue-template-es2015-compiler/buble.js:2663:20)
at Parser.pp$3.parseExprAtom (node_modules/vue-template-es2015-compiler/buble.js:2175:26)
at Parser.<anonymous> (node_modules/vue-template-es2015-compiler/buble.js:6003:24)
at Parser.parseExprAtom (node_modules/vue-template-es2015-compiler/buble.js:6129:31)
at Parser.pp$3.parseExprSubscripts (node_modules/vue-template-es2015-compiler/buble.js:2047:19)
at Parser.pp$3.parseMaybeUnary (node_modules/vue-template-es2015-compiler/buble.js:2024:17)
at Parser.pp$3.parseExprOps (node_modules/vue-template-es2015-compiler/buble.js:1966:19)
at Parser.pp$3.parseMaybeConditional (node_modules/vue-template-es2015-compiler/buble.js:1949:19)
at Parser.pp$3.parseMaybeAssign (node_modules/vue-template-es2015-compiler/buble.js:1925:19)
at Parser.pp$3.parseExprList (node_modules/vue-template-es2015-compiler/buble.js:2663:20)
at Parser.pp$3.parseSubscripts (node_modules/vue-template-es2015-compiler/buble.js:2075:29)
at Parser.pp$3.parseExprSubscripts (node_modules/vue-template-es2015-compiler/buble.js:2050:21)
at Parser.pp$3.parseMaybeUnary (node_modules/vue-template-es2015-compiler/buble.js:2024:17)
at Parser.pp$3.parseExprOps (node_modules/vue-template-es2015-compiler/buble.js:1966:19)
at Parser.pp$3.parseMaybeConditional (node_modules/vue-template-es2015-compiler/buble.js:1949:19)
at Parser.pp$3.parseMaybeAssign (node_modules/vue-template-es2015-compiler/buble.js:1925:19)
at Parser.pp$3.parseMaybeConditional (node_modules/vue-template-es2015-compiler/buble.js:1954:28)
at Parser.pp$3.parseMaybeAssign (node_modules/vue-template-es2015-compiler/buble.js:1925:19)
at Parser.pp$3.parseExprList (node_modules/vue-template-es2015-compiler/buble.js:2663:20)
at Parser.pp$3.parseExprAtom (node_modules/vue-template-es2015-compiler/buble.js:2175:26)
at Parser.<anonymous> (node_modules/vue-template-es2015-compiler/buble.js:6003:24)
at Parser.parseExprAtom (node_modules/vue-template-es2015-compiler/buble.js:6129:31)
at Parser.pp$3.parseExprSubscripts (node_modules/vue-template-es2015-compiler/buble.js:2047:19)
at Parser.pp$3.parseMaybeUnary (node_modules/vue-template-es2015-compiler/buble.js:2024:17)
at Parser.pp$3.parseExprOps (node_modules/vue-template-es2015-compiler/buble.js:1966:19)
at Parser.pp$3.parseMaybeConditional (node_modules/vue-template-es2015-compiler/buble.js:1949:19)
at Parser.pp$3.parseMaybeAssign (node_modules/vue-template-es2015-compiler/buble.js:1925:19)
at Parser.pp$3.parseExprList (node_modules/vue-template-es2015-compiler/buble.js:2663:20)
at Parser.pp$3.parseSubscripts (node_modules/vue-template-es2015-compiler/buble.js:2075:29)
Upgrading to Jest 29 and related dependencies fixed the problem for us.
Because Jest now uses Node instead of JSDom as the default environment I also had to tell Jest to use JSDom.
// package.json
"jest": "^29.5.0",
"jest-environment-jsdom": "^29.5.0",
"babel-jest": "^29.5.0",
"@vue/vue2-jest": "^29.2.3", // instead of vue-jest
// jest.config.js
testEnvironment: "jsdom",
@Nivani
I was not aware that version 29 was out. I updated and the error disappeared. Thanks for letting me know!
Upgrading to Jest 29 and related dependencies fixed the problem for us.
Because Jest now uses Node instead of JSDom as the default environment I also had to tell Jest to use JSDom.
// package.json "jest": "^29.5.0", "jest-environment-jsdom": "^29.5.0", "babel-jest": "^29.5.0", "@vue/vue2-jest": "^29.2.3", // instead of vue-jest // jest.config.js testEnvironment: "jsdom",
Upgrading the version mentioned above helped
Upgrading to Jest 29 and related dependencies fixed the problem for us.
Because Jest now uses Node instead of JSDom as the default environment I also had to tell Jest to use JSDom.
// package.json "jest": "^29.5.0", "jest-environment-jsdom": "^29.5.0", "babel-jest": "^29.5.0", "@vue/vue2-jest": "^29.2.3", // instead of vue-jest // jest.config.js testEnvironment: "jsdom",
Thanks upgrading from 28 to these versions fixed the issues.
My project is Vue 2.x. I am using
@babel/plugin-proposal-optional-chaining
and I have a line of code in my component.vue file that uses "?"which works well when run the app. However, my unit tests are failing because it cannot recognize the token "?"
my
babel.config.js
my partial
package.json
:my
jest.config.js
I am not sure what is the correct way to make Jest to be able to recognize the plugins that I use for my project. I am thinking it is because the .vue file is processed by vue-jest which does not take babel config... if that is the case I don't know how to fix it...