玩笑会重新定义错误全局变量吗?

发布于 2025-02-11 19:57:26 字数 1668 浏览 1 评论 0原文

我有一个非常简单的测试:

    beforeAll(async () => {
      try {
        await db.connect();
      } catch(e: any) {
        expect(e).toBeInstanceOf(Error);
      }
    });

但是由于这个非常奇怪的消息而失败

    expect(received).toBeInstanceOf(expected)

    Expected constructor: Error
    Received constructor: Error

,因此我怀疑error在运行嘲笑测试时的值重新定义。

jest.config.js:

/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
  preset:                 "ts-jest",
  testEnvironment:        "node",
  testPathIgnorePatterns: ["/node_modules/"]
};

部分 package.json:

{
  "devDependencies": {
    "@types/jest": "28.1.3",
    "@types/node": "18.0.0",
    "jest": "28.1.1",
    "ts-jest": "28.0.5",
    "ts-node": "10.8.1",
    "typescript": "4.7.4",
  },
  "scripts": {
    "coverage": "jest --coverage",
    "test": "jest",
  }
}

tsconfig.json:

{
  "compilerOptions": {
    "alwaysStrict": true,
    "esModuleInterop": true,
    "moduleResolution": "Node",
    "noImplicitAny": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "strict": true,
    "strictBindCallApply": true,
    "strictFunctionTypes": true,
    "strictNullChecks": true,
    "strictPropertyInitialization": true,
    "target": "ESNext"
  },
  "exclude": ["*.d.ts"],
  "include": ["*.ts", "test/*.ts"]
}

I have a very simple test:

    beforeAll(async () => {
      try {
        await db.connect();
      } catch(e: any) {
        expect(e).toBeInstanceOf(Error);
      }
    });

but it fail with this really strange message

    expect(received).toBeInstanceOf(expected)

    Expected constructor: Error
    Received constructor: Error

so I suspect the value of Error while running a jest test is redefined.

jest.config.js:

/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
  preset:                 "ts-jest",
  testEnvironment:        "node",
  testPathIgnorePatterns: ["/node_modules/"]
};

partial package.json:

{
  "devDependencies": {
    "@types/jest": "28.1.3",
    "@types/node": "18.0.0",
    "jest": "28.1.1",
    "ts-jest": "28.0.5",
    "ts-node": "10.8.1",
    "typescript": "4.7.4",
  },
  "scripts": {
    "coverage": "jest --coverage",
    "test": "jest",
  }
}

tsconfig.json:

{
  "compilerOptions": {
    "alwaysStrict": true,
    "esModuleInterop": true,
    "moduleResolution": "Node",
    "noImplicitAny": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "strict": true,
    "strictBindCallApply": true,
    "strictFunctionTypes": true,
    "strictNullChecks": true,
    "strictPropertyInitialization": true,
    "target": "ESNext"
  },
  "exclude": ["*.d.ts"],
  "include": ["*.ts", "test/*.ts"]
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文