Jest ReferenceError:未定义请求

fcg9iug3  于 12个月前  发布在  Jest
关注(0)|答案(1)|浏览(145)

我知道这已经问过了,但我有这个错误与节点18和polyfill的请求或获取应该是必要的。
我有以下的测试在开玩笑:

describe('Strange error', () => {
  it('Why', async () => {
    console.log('Node.js version:', process.version);
    expect(Request).not.toBeUndefined();
  });
});

输出为:

console.log
Node.js version: v18.14.0

  at Object.log (file.spec.tsx:3:13)

FAIL   test  file.spec.tsx
  Strange error
    ✕ Why (11 ms)

  ● Strange error › Why

    ReferenceError: Request is not defined

      2 |   it('Why', async () => {
      3 |     console.log('Node.js version:', process.version);
    > 4 |     expect(Request).not.toBeUndefined();
        |            ^
      5 |   });
      6 | });

我的jest.config是:

module.exports = {
  testRegex:
    'file.spec.tsx',
  transform: {
    '^(?!.*\\.(js|jsx|ts|tsx|css|json)$)': 'jest-preview/transforms/file',
  },
  testEnvironment: 'jsdom', //when commented out this works
};
llmtgqce

llmtgqce1#

Jest有一个outdated jsdom配置,因为它使用过时的jsdom
我在这里应用了解决方案,只要我在jest配置中添加ts文件作为testEnvironment,它就可以工作,但如果我想将其用于单个测试,

/**
 * @jest-environment ./src/test/my-custom-environment
 */

什么也没做,尝试了不同的路径和不同的文件(mjs,cjs,js),但整个事情都被忽略了,即使是不存在的文件也不会产生错误。

相关问题