Open LevYas opened 4 years ago
JS ecosystem is a kind of dark magic...
I updated packages with npm update
in this repo today and the HelloWorld test passed without errors.
Ultimately, I found the reason why my project failed. In my package.json I have a "jest" section with moduleFileExtensions setting. The jest documentation says
We recommend placing the extensions most commonly used in your project on the left
so, since I used TS, my moduleFileExtensions looked like this:
"moduleFileExtensions": [ "ts", "vue", "js", "json" ],
and I got error
Test suite failed to run
Jest encountered an unexpected token
... Details:
D:\Projects\MyAwesomeProject\src\components\TheMap\LodControl.vue:2 import _classCallCheck from "d:\\Projects\\MyAwesomeProject\\node_modules\\@babel\\runtime-corejs2/helpers/esm/classCallCheck"; ^^^^^^ SyntaxError: Unexpected token import
When I changed moduleFileExtensions to
"moduleFileExtensions": [ "js", "ts", "json", "vue" ],
the error disappeared.
This issue is still valid, I can make a PR with this update if needed.
... Oh, no, it still fails on my build-machine...
As for issue in my project:
Phew, I was able to reproduce fail on build-machine on my workstation and fix it.
To perform "clean run" and then start the tests I used the command
npm -i --force install && npm run test:unit
I had a working configuration for TS and I wanted to add tests for the .vue components.
I tried a lot of solutions, and minimum worked solution for me is:
npm install -D @babel/core @babel/plugin-transform-modules-commonjs babel-core@^7.0.0-bridge.0 babel-jest
(I already had jest, @types/jest, and vue-jest)"vue"
to jest's "moduleFileExtensions"
"^.+\\.jsx?$": "babel-jest"
to jest's "transform"
"node_modules/(?!(@babel)/)"
to jest's "transformIgnorePatterns"
test: {
plugins: [
"@babel/plugin-transform-modules-commonjs",
]
}
},
Some of my components contained imports for the .css files, so I:
module.exports = {};
,"allowJs": true,
in my tsconfig.json,"preset": "ts-jest",
to "preset": "ts-jest/presets/js-with-ts",
\"\\.(css|less)$": "<rootDir>/src/__mocks__/styleMock.js"
to jest's "moduleNameMapper" section.Also, I added testSetup.js to initialize vuetify there. Ultimately, I solved this problem. My jest config looks like this:
"preset": "ts-jest/presets/js-with-ts",
"setupFiles": [
"<rootDir>/src/testSetup.js"
],
"moduleFileExtensions": [
"js",
"ts",
"json",
"vue"
],
"transform": {
"^.+\\.ts$": "ts-jest",
".*\\.(vue)$": "vue-jest",
"^.+\\.jsx?$": "babel-jest"
},
"moduleNameMapper": {
"^@/(.*)$": "<rootDir>/src/$1",
"^vuetify/lib$": "vuetify",
"\\.(css|less)$": "<rootDir>/src/__mocks__/styleMock.js"
},
"reporters": [
"default",
"jest-junit"
],
"transformIgnorePatterns": [
"node_modules/(?!(@babel)/)"
],
"testURL": "http://localhost/"
},
As for this project, just the update
is not a solution.
After npm update
and npm audit fix
I tried npm -i --force install && npm run test:unit
and saw an error that babel is not found. I installed babel packages and it worked without additional configuration. I'm not sure is it the right way to solve the problem, that's why I did not make a PR.
@LevYas I'm getting this on windows10 (VueJS/Jest):
Test suite failed to run
Jest encountered an unexpected token
This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".
Here's what you can do:
• 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/en/configuration.html
Details:
C:\Users\PC01\Desktop\\Project01\tests\features\Unit.spec.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import { shallowMount } from '@vue/test-utils';
^^^^^^
SyntaxError: Cannot use import statement outside a module
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1086:14)
● 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:
/home/coducer/Desktop/projects/alarabia/alarabia-backend/node_modules/axios/index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import axios from './lib/axios.js';
^^^^^^
SyntaxError: Cannot use import statement outside a module
Hello, any update on this ticket?
I faced the same issue in my project and tried this one as a reference. Surprisingly, this reference project produced the same error. I use updated packages in my projects, but error is the same. So, I cloned this repo, opened it in VS Code, then put in terminal:
npm install
npm run test
Here is the result:
What could be the reason and how to fix it?
P.S. There is PR to this repo, I tried this one too and it doesn't produce that error. I tried to figure out the defining differences all the previous workday, but, unfortunately, with no success.