Giter VIP home page Giter VIP logo

Comments (11)

eddyerburgh avatar eddyerburgh commented on August 16, 2024 2

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

from vue-test-utils-jest-example.

tsauvajon avatar tsauvajon commented on August 16, 2024 2

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

from vue-test-utils-jest-example.

Gomah avatar Gomah commented on August 16, 2024 1

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",
      ]
    }
  }
}

from vue-test-utils-jest-example.

defnorep avatar defnorep commented on August 16, 2024

Yeah this seems to be blocking me from adopting Jest.

from vue-test-utils-jest-example.

eddyerburgh avatar eddyerburgh commented on August 16, 2024

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

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

from vue-test-utils-jest-example.

freakyfanny avatar freakyfanny commented on August 16, 2024

I have the same problem.

from vue-test-utils-jest-example.

freakyfanny avatar freakyfanny commented on August 16, 2024

Node 9.2.1
Npm 5.6.0
MACOS Sierra

from vue-test-utils-jest-example.

eddyerburgh avatar eddyerburgh commented on August 16, 2024

@freakyfanny with a clean install?

from vue-test-utils-jest-example.

freakyfanny avatar freakyfanny commented on August 16, 2024

yes

from vue-test-utils-jest-example.

tsauvajon avatar tsauvajon commented on August 16, 2024

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.

from vue-test-utils-jest-example.

freakyfanny avatar freakyfanny commented on August 16, 2024

from vue-test-utils-jest-example.

Related Issues (5)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.