ES6类的jest.mock产生ReferenceError:require未定义 [英] jest.mock of ES6 class yields ReferenceError: require is not defined

查看:106
本文介绍了ES6类的jest.mock产生ReferenceError:require未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的ES6 javascript项目中使用玩笑来创建自动模拟.

I'm trying to create an automatic mock using jest in my ES6 javascript project.

我在ubuntu 18.04.5 上使用节点 v15.0.1 和jest 26.6.0 .

I'm using node v15.0.1, and jest 26.6.0 on 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 | 

由于我没有使用require语句,因此错误令我感到惊讶.

The error is surprising to me as I'm not using a require statement.

我的package.json配置包含以下内容:

My package.json config contains the following:

"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: {}
}

Jest配置文件中的

.默认情况下, transform 选项配置为使用 babel-jest .请参阅此Jest文档部分以了解更多详细信息.还要注意,您应该显式导入 jest :

in your Jest configuration file. By default transform option is configured to use babel-jest. Please refer to this Jest documentation section for more details. Also note that you should import jest explicitly:

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

不幸的是,就像其他评论者已经提到的那样,它在运行测试时仍然会遇到一些问题.可能应该遵循此问题,以跟踪在Jest中对ESM支持所做的更改

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.

例如,我目前不愿意模拟静态模块导入(版本26.6.2):

For example, I was unlucky to mock static module imports at the moment (version 26.6.2):

开玩笑.(不要做)

jest.(do|un)mock

由于ESM具有不同的阶段",在评估模块时,jest.mock不适用于静态导入.不过,它可以用于动态导入,因此我认为我们必须在文档中明确说明它支持什么以及不支持什么.

Since ESM has different "stages" when evaluating a module, jest.mock will not work for static imports. 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调用被挂起,但这在ESM中无济于事.我们可能会考虑将导入事物"转换为import('thing'),这应允许提升工作,但随后是异步的.对于这种方法,可能需要使用顶级等待.我还认为它具有足够的侵入性,因此有必要提供单独的选择.有待讨论的事情-我们不需要为初始发行版提供jest.mock的所有支持.

jest.mock calls are hoisted, but that doesn't help in ESM. We might consider transforming import 'thing' to import('thing') which should allow hoisting to work, but then it's async. Using top-level await is probably a necessity for such an approach. 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.

这篇关于ES6类的jest.mock产生ReferenceError:require未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆