简体   繁体   中英

How to apply dynamically a moduleNameMapper in Jest configuration for repeated patterns?

I've a question related to JSON configuration for a react project. This project is also based on many themes.

I've a couple of questions for you.

1) Is there a way to make dynamic the mapping of the last three lines of the object moduleNameMapper , maybe with some regex? I don't want to add line by line anytime a new json is required to be added within the project.

2) Is there a way to replace the theme with a process.env.theme?

jest.config.json

  "moduleNameMapper": {
    "\\.(scss)$": "<rootDir>/src/app/mocks/tests/scss.js",
    "\\.(css)$": "identity-obj-proxy",
    "public/data/car/car.json": "<rootDir>/theme/data/car/car.json",
    "public/data/bus/bus.json": "<rootDir>/theme/data/bus/bus.json",
    "public/data/van/van.json": "<rootDir>/theme/data/van/van.json",
  },

Thanks in advance

The moduleMapper RegExp can have capture groups which are accessible in the mapped path by using placeholders ( $1 , $2 , etc).

"moduleNameMapper": {
    "\\.(scss)$": "<rootDir>/src/app/mocks/tests/scss.js",
    "\\.(css)$": "identity-obj-proxy",
    "public/data/car/(.+\\.json)": "<rootDir>/theme/data/car/$1",
},

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