简体   繁体   English

ES6 类的 jest.mock 产生 ReferenceError: require is not defined

[英]jest.mock of ES6 class yields ReferenceError: require is not defined

I'm trying to create an automatic mock using jest in my ES6 javascript project.我正在尝试在我的 ES6 javascript 项目中使用 jest 创建一个自动模拟。

I'm using node v15.0.1 , and jest 26.6.0 on ubuntu 18.04.5 .我使用节点v15.0.1 ,并开玩笑26.6.0在Ubuntu 18.04.5

I have a test file containing the following code:我有一个包含以下代码的测试文件:

import RenderBuffer from './renderbuffer.js'

jest.mock('./renderbuffer.js');

beforeEach(() => {
    RenderBuffer.mockClear();
});

When I run the test I run into the following issue:当我运行测试时,我遇到了以下问题:

ReferenceError: require is not defined

      4 | 
      5 | beforeEach(() => {
    > 6 |     RenderBuffer.mockClear();
        |       ^
      7 | });
      8 | 

The error is surprising to me as I'm not using a require statement.这个错误让我感到惊讶,因为我没有使用 require 语句。

My package.json config contains the following:我的 package.json 配置包含以下内容:

"type": "module",
    "main": "src/index.js",
    "devDependencies": {
        "jest": "^26.5.3",
        "jest-canvas-mock": "^2.3.0"
    },
    "jest": {
        "setupFiles": ["jest-canvas-mock"]
    },
    "scripts": {
        "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
        "test-coverage": "node --experimental-vm-modules node_modules/jest/bin/jest.js --coverage"
    }

Any ideas as to what the root cause of this issue is?关于这个问题的根本原因是什么的任何想法?

You have to disable any source code transformations in order to make it working by setting您必须禁用任何源代码转换才能通过设置使其工作

{
  transform: {}
}

in your Jest configuration file.在你的 Jest 配置文件中。 By default transform option is configured to use babel-jest .默认情况下, 转换选项配置为使用babel-jest Please refer to this Jest documentation section for more details.有关更多详细信息,请参阅此 Jest 文档部分 Also note that you should import jest explicitly:另请注意,您应该明确导入jest

import { jest } from '@jest/globals';

Unfortunatelly, it can still have some issues running your tests as other commenters already mentioned.不幸的是,正如其他评论者已经提到的那样,在运行您的测试时仍然存在一些问题。 Probably, one should follow this issue to keep tracking changes being made in Jest for ESM support.也许,人们应该关注这个问题,以继续跟踪 Jest 中为 ESM 支持所做的更改。

For example, I was unlucky to mock static module imports at the moment (version 26.6.2):例如,我当时很不幸模拟静态模块导入(版本 26.6.2):

jest.(do|un)mock开玩笑。(做|不)嘲笑

Since ESM has different "stages" when evaluating a module, jest.mock will not work for static imports.由于 ESM 在评估模块时有不同的“阶段”,因此 jest.mock 不适用于静态导入。 It can work for dynamic imports though, so I think we just have to be clear in the docs about what it supports and what it doesn't.不过它可以用于动态导入,所以我认为我们只需要在文档中明确它支持什么,不支持什么。

jest.mock calls are hoisted, but that doesn't help in ESM. jest.mock 调用被提升,但这对 ESM 没有帮助。 We might consider transforming import 'thing' to import('thing') which should allow hoisting to work, but then it's async.我们可能会考虑将 import 'thing' 转换为 import('thing') ,这应该允许提升工作,但它是异步的。 Using top-level await is probably a necessity for such an approach.使用顶级 await 可能是这种方法的必要条件。 I also think it's invasive enough to warrant a separate option.我也认为它的侵入性足以保证一个单独的选择。 Something to discuss - we don't need to support everything jest.mock can for for an initial release.有什么要讨论的 - 我们不需要支持 jest.mock 可以为初始版本提供的所有内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM