![](/img/trans.png)
[英]`moduleNameMapper` settings in jest.config.js doesn't work on CircleCI
[英]Importing local files in `jest.config.js` file
在我的jest.config.js
文件中,我需要填充globals
屬性。 為了填充globals
屬性,我需要需要本地模塊,如下所示:
const path = require('path')
const server = require('./server/cfg')
module.exports = {
rootDir: path.resolve(__dirname),
moduleFileExtensions: [
'js',
'json',
'vue',
'ts'
],
moduleNameMapper: {
'^@/(.*)$': '<rootDir>/src/$1'
},
transform: {
".*\\.(vue)$": "<rootDir>/node_modules/vue-jest",
"^.+\\.(js|jsx)?$": "<rootDir>/node_modules/babel-jest",
"^.+\\.ts$": "<rootDir>/node_modules/ts-jest"
},
testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
snapshotSerializers: [
"jest-serializer-vue"
],
setupFiles: [
"<rootDir>/globals.js"
],
testEnvironment: "jsdom",
globals: {
server: {
server
}
}
}
使用此配置,我收到以下錯誤:
Error: Cannot find module './server/cfg'
這是我的文件夾結構
server/
cfg.ts
src/
jest.config.js
webpack.config.js
但是,我可以require
節點的內置模塊。 我無法弄清楚為什么會這樣。 關於如何克服這個問題的任何想法?
Jest 是由node
發起的,而不是ts-node
並且默認情況下它無法解析.ts
文件。
可能添加setupFilesAfterEnv會幫助你。
jest.config.js
module.exports = {
// ... your config
setupFilesAfterEnv: [
"<rootDir>/environmentWithServer.ts`
]
}
environmentWithServer.ts
global.server = require('./server/cfg');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.