Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot find module './src/components/Message' from 'MessageToggle.vue' #5

Closed
tsauvajon opened this issue Oct 31, 2017 · 11 comments
Closed

Comments

@tsauvajon
Copy link

tsauvajon commented Oct 31, 2017

If I use an alias to resolve my components path e.g. import Message from '@/components/Message' instead of import Message from ./Message, my tests fail.

Cannot find module './src/components/Message' from 'MessageToggle.vue'

I can temporarly use relative paths instead of @/, but using aliases is extremely convenient for me and my team ...

@defnorep
Copy link

Yeah this seems to be blocking me from adopting Jest.

@eddyerburgh
Copy link
Owner

You can use aliases in Jest.

Checkout this Jest config, it maps @ to src:

https://github.com/vuejs/vue-test-utils-jest-example/blob/master/package.json#L39

More info — https://facebook.github.io/jest/docs/en/configuration.html#modulenamemapper-object-string-string

@tsauvajon
Copy link
Author

tsauvajon commented Nov 14, 2017

It doesn't work for me.

My package.json jest config already contains

"moduleNameMapper": {
  "^@/(.*)$": "<rootDir>/src/$1"
}

You can reproduce within this repository, just change import Message from './Message' to import Message from '@/components/Message' in MessageToggle.vue.

It resolves to ./src (relative to the component), instead of [application root]/src (relative to the package.json). In other words it fails to resolve <rootDir>.

@eddyerburgh eddyerburgh reopened this Nov 14, 2017
@eddyerburgh
Copy link
Owner

eddyerburgh commented Nov 14, 2017

That's strange, it works correctly for me in the vue-test-utils-jest-example.

What are your details (OS, node version, npm version)

@freakyfanny
Copy link

I have the same problem.

@freakyfanny
Copy link

Node 9.2.1
Npm 5.6.0
MACOS Sierra

@eddyerburgh
Copy link
Owner

@freakyfanny with a clean install?

@freakyfanny
Copy link

yes

@tsauvajon
Copy link
Author

tsauvajon commented Dec 18, 2017

I migrated my project to a more recent vue-cli webpack template, due to 2 issues (the current one and this one with vue-jest, probably not related).

I can now import with the @ alias without problem.

@freakyfanny
Copy link

freakyfanny commented Dec 19, 2017 via email

@Gomah
Copy link

Gomah commented Jun 21, 2018

Had a similar issue as I was using babel-plugin-module-resolver.

// jest.config.js
module.exports = {
  moduleFileExtensions: ['js', 'json', 'vue'],
  moduleDirectories: ['node_modules'],
  setupTestFrameworkScriptFile: 'mock-local-storage',
  moduleNameMapper: {
    '^.+\\.(jpg|jpeg)$': 'jest-static-stubs/jpg',
    '^.+\\.(png)$': 'jest-static-stubs/png',
    '^.+\\.(svg)$': 'identity-obj-proxy',
    '@/(.*)': '<rootDir>/client/$1',
    '~/(.*)': '<rootDir>/client/$1',
  },
  transform: {
    '^.+\\.js$': '<rootDir>/node_modules/babel-jest',
    '.*\\.(vue)$': '<rootDir>/node_modules/vue-jest',
  },
  setupFiles: ['./client/plugins/components.js'],
  snapshotSerializers: ['<rootDir>/node_modules/jest-serializer-vue'],
};

.babelrc

{
  "env": {
    "test": {
      "presets": ["env"],
      "plugins": [
        "transform-es2015-modules-commonjs",
        "transform-object-rest-spread",
        "dynamic-import-node",
        [
          "module-resolver",
          {
            "root": ["./"],
            "alias": {
              "@": "./client/",
              "~": "./client/"
            }
          }
        ]
      ]
    }
  }
}

Removing the module-resolver configuration fixed the problem for me, it was probably clashing with jest's moduleNameMapper.

{
  "env": {
    "test": {
      "presets": ["env"],
      "plugins": [
        "transform-es2015-modules-commonjs",
        "transform-object-rest-spread",
        "dynamic-import-node",
      ]
    }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants