vue-jest
vue-jest copied to clipboard
SyntaxError: Cannot use import statement outside a module
I tried to implement jest for testing of a components library I'm currently building (Vite + Vue3 + Rollup) but I'm running in to some issues.
` FAIL src/lib-components/FTButton.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:
/Users/Dev/ft-vue-components/src/lib-components/FTButton.test.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import { mount } from '@vue/test-utils'
^^^^^^
SyntaxError: Cannot use import statement outside a module
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1728:14)
Test Suites: 1 failed, 1 total Tests: 0 total Snapshots: 0 total Time: 0.401 s Ran all test suites.`
My package.json
...
"@vue/test-utils": "^2.0.0-rc.18",
"@vue/vue3-jest": "^27.0.0-alpha.4",
"jest": "^27.4.7",
...
jest.config.js
module.exports = {
transform: {
"^.+\\.vue$": "@vue/vue3-jest",
},
moduleFileExtensions: ['vue', 'js', 'json']
}
FTButton.test.tjs
import { mount } from '@vue/test-utils'
import FTButton from './FTButton.vue';
test('Init', () => {
const wrapper = mount(FTButton, {
props: {
label: 'Jonis'
}
});
expect(wrapper.text()).toContainer('Jonis');
})
FTButton.vue
<template>
<button class="ft-button" :class="`ft-button--${buttonStyle}`">
<div class="ft-button__icon">
<slot name="icon"></slot>
</div>
<div class="ft-button__label">
<slot>{{ label }}</slot>
</div>
</button>
</template>
<script>
export default {
name: 'FTButton',
inheritAttrs: true,
props: {
buttonStyle: {
type: String,
default: 'tertiary',
},
label: {
type: String,
default: '',
}
}
};
</script>
<style>
some styels...
</style>
Same issue. Upgraded a Vue 2 project to Vue 3 and my vue-files wont't work with unit tests anymore. Followed instructions on vue-test-utils for v2 beta:
https://test-utils.vuejs.org/installation/
Configured my jest.config.js
to use vue-jest
for .vue
files and ts-jest
for .ts
files.
My component.vue
files wouldn't work in jest resulting in the same error as stated by @ja1984 . Using Vue 3 and jest via vue-cli.
EDIT:
transformIgnorePatterns: ['node_modules', '/node_modules/(?!@scu/vue)']
didn't help (it was the package at which the import in my vue-component file failed).
Try clearing the cache as you've probably got left-over files from a vue-2 implementation. Try jest tests/unit --clearCache
Please try this workaround:
npm install --save-dev @babel/preset-env
and add babel.config.js
to your root of your project:
// babel.config.js
module.exports = {
presets: ['@babel/preset-env'],
}
Reference: https://github.com/vuejs/vue-jest/issues/367#issuecomment-880300172
I was getting this error due @nuxtjs/composition-api
not being registered on Jest, commenting this workaround here for future reference (currently using Vue 2 with composition-api):
// eslint-disable-next-line @typescript-eslint/no-var-requires
const path = require("path")
module.exports = {
moduleNameMapper: {
"^@/(.*)$": "<rootDir>/$1",
"^~/(.*)$": "<rootDir>/$1",
"^vue$": "vue/dist/vue.common.js",
"^@nuxtjs/composition-api$": path.join(
__dirname,
"node_modules/@nuxtjs/composition-api/dist/runtime/index.js"
),
},
moduleFileExtensions: ["js", "vue", "json"],
transform: {
"^.+\\.js$": "babel-jest",
".*\\.(vue)$": "@vue/vue2-jest",
},
}
Getting the exact same error
stuggled with this as well when integrating vuetify using vue3, vuetify3, jest29, vue3-jest29
solved it as follows:
jest.config.ts
...
const config: Config.InitialOptions = {
moduleFileExtensions: ['js', 'ts', 'vue', 'json'],
transform: {
'^.+\\.m?[j|t]sx?$': 'babel-jest', <<< include .mjs
'^.+\\.vue$': '@vue/vue3-jest',
},
moduleNameMapper: {
'.+\\.(css|styl|less|sass|scss|png|jpg|ttf|woff|woff2)$': '<rootDir>/tests/ts/styleMock.ts', <<< map unparsables to mock
'^@/(.*)$': '<rootDir>/resources/js/$1',
},
...
transformIgnorePatterns: ['<rootDir>/node_modules/(?!vuetify)'], <<< dont exclude vuetify from being transformed
...
setupFiles: ['<rootDir>/tests/ts/setup.ts'], <<< add vuetify as default plugin to vue test utils
...
tests/ts/setup.ts
import { config as vueconfig } from '@vue/test-utils';
import * as components from 'vuetify/components';
import * as directives from 'vuetify/directives';
import {createVuetify} from "vuetify";
const vuetify = createVuetify( {
components,
directives
});
vueconfig.global.plugins.push(vuetify);
tests/ts/styleMock.ts
export default {};
get the same error with vue:^3.2.47, jest:^29.5.0, @vue/vue3-jest:^29.2.4, node: v16.19.0
I change babel.config.js
->babel.config.cjs
.
It works.
https://stackoverflow.com/questions/61146112/error-while-loading-config-you-appear-to-be-using-a-native-ecmascript-module-c