在我的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');
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句