vue-jest
vue-jest copied to clipboard
SyntaxError: Cannot use import statement outside a module (typescript)
FAIL tests/time-range-input.spec.ts
● 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:
/Users/youri/dev/vue3-components/node_modules/date-fns/esm/format/index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import isValid from "../isValid/index.js";
^^^^^^
SyntaxError: Cannot use import statement outside a module
9 | v-model="start"
10 | :has-error="error.hasError || hasError"
> 11 | ></p-form-input>
| ^
12 | <p-form-input
13 | type="time"
14 | class="end"
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1728:14)
at Object.<anonymous> (src/components/time-range-input/time-range-input.vue:11:40)
Dependencies
"@fullcalendar/core": "^5.10.0",
"@fullcalendar/daygrid": "^5.10.0",
"@fullcalendar/interaction": "^5.10.0",
"@fullcalendar/timegrid": "^5.10.0",
"@fullcalendar/vue3": "^5.10.0",
"@popperjs/core": "^2.10.2",
"@rollup/plugin-commonjs": "^20.0.0",
"@rollup/plugin-node-resolve": "^13.0.4",
"@types/jest": "^27.0.2",
"@vitejs/plugin-vue": "^1.9.3",
"@vue/compiler-sfc": "^3.2.1",
"@vue/test-utils": "^2.0.0-rc.16",
"@vue/vue3-jest": "^27.0.0-alpha.3",
"date-fns": "^2.25.0",
"jest": "^27.3.1",
"rimraf": "^3.0.2",
"rollup-plugin-esbuild": "^4.5.0",
"rollup-plugin-vue": "^6.0.0",
"sass": "^1.37.5",
"sass-loader": "^8.0.2",
"ts-jest": "^27.0.7",
"typescript": "^4.3.5",
"v-calendar": "^3.0.0-alpha.5",
"vite": "^2.6.7",
"vue": "^3.2.1",
"vue-router": "4"
jest.config.js
module.exports = {
transform: {
'^.+\\.vue$': '@vue/vue3-jest',
'^.+\\.tsx?$': 'ts-jest'
},
testMatch: [
'**/tests/**/*.spec.ts'
],
moduleFileExtensions: [
'ts',
'js',
'vue'
],
testEnvironment: 'jsdom',
collectCoverage: true,
collectCoverageFrom: [
'src/components/**/*.{ts,vue}',
'!node_modules/**',
'!src/components/**/index.ts'
],
coverageDirectory: 'coverage',
coverageReporters: ['text', 'html', 'cobertura']
}
tsconfig
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"strict": true,
"importHelpers": true,
"moduleResolution": "node",
"experimentalDecorators": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"sourceMap": true,
"strictNullChecks": true,
"baseUrl": ".",
"types": [
"jest"
],
"paths": {
"@/*": [
"src/*"
]
},
"lib": [
"esnext",
"dom.iterable",
"dom"
]
},
"exclude": [
"node_modules"
]
}
Tried something like:
globals: {
'ts-jest': {
useESM: true,
tsconfig: 'tsconfig.json'
}
}
Didn't work...
@yooouuri try doing a jest --clearCache
then run jest --runInBand
If that works, we may have the same issue.
@jolting
Added
preset: 'ts-jest/presets/js-with-ts-esm',
and
transformIgnorePatterns: [ "/node_modules/(?!(date-fns)/)" ]
To jest.config.js
And
"allowJs": true
To tsconfig.json.
There is no need for babel as I removed every babel dependency in my project.
Now I can test .vue files!!
I think there is still a babel bug. I'm glad you don't need it!
does anyone know how to fix this?
Any updates?