Open ja1984 opened 2 years ago
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
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
Test Suites: 1 failed, 1 total Tests: 0 total Snapshots: 0 total Time: 0.401 s Ran all test suites.`
My package.json
jest.config.js
FTButton.test.tjs
FTButton.vue