【问题标题】:Jest Custom Test Environment Can't Use Absolute PathsJest 自定义测试环境不能使用绝对路径
【发布时间】:2021-09-30 05:01:09
【问题描述】:
【问题讨论】:
标签:
node.js
typescript
jestjs
ts-jest
【解决方案1】:
as suggested by the Jest team 的一个潜在解决方案是self-reference a package using its name。我已更新 example repository 以反映此解决方案。
package.json
{
"name": "test-proj",
...
"exports": {
".": "./index.js",
"./serverConfig": "./src/config/index.ts"
}
}
testEnvironment.ts
const NodeEnvironment = require('jest-environment-node');
// @ts-ignore
import serverConfig from 'test-proj/serverConfig';
class CustomEnvironment extends NodeEnvironment {
constructor(config, context) {
super(config, context);
}
async setup() {
await super.setup();
this.global.someGlobalObject = serverConfig;
}
async teardown() {
await super.teardown();
}
}
module.exports = CustomEnvironment;