Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jest+vue: "Cannot find module ....vue"

Tags:

vue.js

jestjs

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.

like image 551
eddow Avatar asked Nov 27 '19 19:11

eddow


People also ask

How to fix cannot find module issue in Vue jest testing?

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.

How to configure jest in Vue JS?

// 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.

How to unit test fancyheading with jest and Vue?

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.

Why do I get jest errors when importing a package?

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).


3 Answers

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;
}
like image 60
Ben Harper Avatar answered Sep 20 '22 11:09

Ben Harper


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'
}
like image 39
Sunali Bandara Avatar answered Sep 17 '22 11:09

Sunali Bandara


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.

like image 30
rethab Avatar answered Sep 17 '22 11:09

rethab