Testing with Jest and Webpack aliases

前端 未结 7 1486
生来不讨喜
生来不讨喜 2020-12-14 14:48

I am looking to be able to use webpack aliases to resolve imports when using jest, and optimally, reference the webpack.aliases to avoid duplication.

Je

相关标签:
7条回答
  • 2020-12-14 15:21

    This seems to have been fixed.

    Below is a working setup:

    Versions

    "jest": "~20.0.4"

    "webpack": "^3.5.6"

    package.json

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

    webpack.shared.js

    const paths = {
      APP_DIR: path.resolve(__dirname, '..', 'src'),
    };
    
    exports.resolveRoot = [paths.APP_DIR, 'node_modules'];
    
    exports.aliases = {
      '@root': path.resolve(paths.APP_DIR, ''),
      '@components': path.resolve(paths.APP_DIR, 'components'),
    };
    
    0 讨论(0)
提交回复
热议问题