【问题标题】:Jest Custom Test Environment Can't Use Absolute PathsJest 自定义测试环境不能使用绝对路径
【发布时间】:2021-09-30 05:01:09
【问题描述】:

我无法在 Jest 自定义测试环境中导入绝对路径。此处简化回购:https://github.com/kyle-banner/ts-customtestenvironment-jest

运行yarn int,你会看到一个错误

 FAIL  path/to/test/something.integration.test.ts
  ● Test suite failed to run

    Cannot find module 'src/config'

【问题讨论】:

    标签: 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;
    

    【讨论】:

      猜你喜欢
      • 2023-02-19
      • 2021-09-08
      • 2020-01-29
      • 1970-01-01
      • 2018-08-26
      • 2015-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多