I'm really just trying to have a skeleton of a jest+vue for my library and quickly get this error. I know the structure is not the usual jest structure, I here try to describe one test with an auxiliary control.
Here is the content of my test
folder: arrays.specs.ts
and arrays.vue
Here is the config:
module.exports = {
moduleFileExtensions: [
"js",
"ts",
"vue"
],
roots: [
"<rootDir>/test"
],
testEnvironment: "jest-environment-jsdom",
testMatch: [
"**/?(*.)+(spec|test).[tj]s?(x)"
],
transform: {
".*\\.(vue)$": "vue-jest",
".*\\.(ts)$": "ts-jest"
},
};
I have installed these 3 packages: @vue/test-utils
, ts-jest
and vue-jest
Now, with this, I still end up with this error when running jest:
test/arrays.spec.ts:4:20 - error TS2307: Cannot find module './arrays.vue'.
import Arrays from './arrays.vue'
I really don't see what I am missing.
If the error Cannot find module issue in vue jest testing occures, set the preset property of jest.config.js file as below. module.exports = { preset: '@vue/cli-plugin-unit-jest/presets/typescript-and-babel', testEnvironment: 'node' } In my case, the problem was that the file shims-vue.d.ts was in the root directory of the project.
// jest.config.js transformIgnorePatterns : ['/node_modules/ (?!vue-json-excel)'] Jest's configuration can be defined in the package.json file of your project, or through a jest.config.js file or through the --config <path/to/js|json> option.
In order to unit test this, we’ll need to make a corresponding FancyHeading.spec.js file within the tests/unit directory. A test suite can be thought of as a collection of tests centered around testing a particular module or functionality. Let’s take a look at our first unit test with Jest and Vue.
I use it, but have this error. I have import others package but it all fine. Now this error happens because jest doesn’t transform anything from /node_modules by default, which is why the .vue file is not compiled and causes this error (because a .vue file is not ajvascript).
You might be missing a 'shim' file. Place this in your 'src' directory:
shims-vue.d.ts
declare module '*.vue' {
import Vue from 'vue';
export default Vue;
}
If the error Cannot find module issue in vue jest testing occures, set the preset property of jest.config.js file as below.
module.exports = {
preset: '@vue/cli-plugin-unit-jest/presets/typescript-and-babel',
testEnvironment: 'node'
}
In my case, the problem was that the file shims-vue.d.ts
was in the root directory of the project. I had to move it to ./src
in order to be able to load .vue
modules.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With