简体   繁体   中英

Jest configuration error in React+Typescript+Webpack project

I have faced following problem. FYI, I am a beginner for the Jest, so please let me know what's wrong in configuring Jest.

webpack.config.js

...

resolve: {
  extensions: ['.ts', '.tsx', '.js', '.json', '.scss'],
  alias: {
    'webextension-polyfill-ts': path.resolve(
      path.join(__dirname, 'node_modules', 'webextension-polyfill-ts')
    ),
    reducers: path.resolve(__dirname, 'source/redux'),
    routers: path.resolve(__dirname, 'source/routers'),
    source: path.resolve(__dirname, 'source'),
    ...
  },
},

...

Here's the Jest configuration in package.json

"jest": {
  ...
  "moduleNameMapper": {
    "^reducers": "<rootDir>/source/redux/$1",
    "^routers": "<rootDir>/source/routers/$1",
    "^source": "<rootDir>/source/$1",
    ...
  }
  ...
}

But I got following error while run a test file.

Configuration error:
    
Could not locate module source-map-support mapped as:
/Volumes/WORK/Development/project/source/$1.
    
Please check your configuration for these entries:
{
  "moduleNameMapper": {
    "/^source/": "/Volumes/WORK/Development/project/source/$1"
  },
  "resolver": undefined
}

The issue looks like from you set the wrong pattern for moduleNameMapper . The right one is supposed to be:

{
  "moduleNameMapper": {
    "^reducers/(.*)$": "<rootDir>/source/redux/$1", // You missed the 2nd part `/(.*)`
    // ...
    ...
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM