mock.module 在 Node.js v22.9.0 节点中未定义:测试模块

问题描述 投票:0回答:1

我正在尝试使用 Node.js v22.9.0 和内置的

node:test
mock.module
来模拟一些模块。

虽然

mock.method
等其他属性可以正常工作,但在尝试访问
mock.module
时我得到了未定义。当我尝试使用它时,这会导致
TypeError

这是我的代码的相关部分:


import { test, mock } from "node:test";

test("should....", (t) => { 
    mock.module('ioredis', { /* mock implementation */ });
});

但是,当我尝试访问

mock.module
时,我得到以下信息:

TypeError [Error]: mock.module is not a function

我也在 Node.js REPL 中尝试过:

Welcome to Node.js v22.9.0.
Type ".help" for more information.
> const { mock } = require("node:test");
undefined
> mock.method
[Function: method]
> mock.module
undefined

好像缺少

mock.module
。我在不同的设置上尝试过,但它总是未定义。

我已经:

  • 已验证

    mock.method
    工作正常,但
    mock.module
    始终未定义。

  • 检查了

    node:test
    的文档,但没有找到有关
    mock.module
    的任何具体内容,并且 Node 22.9 中支持它

  • 尝试过其他 Node 版本。

  • 在脚本和 Node.js REPL 中运行此代码。

环境:

  • Node.js 版本:22.9.0
  • 操作系统:Windows 11 上的 WSL2 Ubuntu

如何让

mock.module
工作以模拟模块进行测试?

javascript node.js node-test-runner
1个回答
0
投票

这仍然是早期开发中的一个功能,隐藏在一个标志后面:

--experimental-test-module-mocks
。以下测试(在
index.test.mjs
中):

import assert from "node:assert/strict";
import { mock, test } from "node:test";

test("node:test has a mock with a module property", (t) => {
    assert(mock.module);
});

即使是最新版本的 Node.js(当前为 22.9)也失败:

node --test
✖ node:test has a mock with a module property (7.442333ms)
  AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value:

    assert(mock.module)

      at TestContext.<anonymous> (file:///Users/jonrsharpe/workspace/so-examples/q79004444/index.test.mjs:5:2)
      at Test.runInAsyncScope (node:async_hooks:211:14)
      at Test.run (node:internal/test_runner/test:930:25)
      at Test.start (node:internal/test_runner/test:829:17)
      at startSubtestAfterBootstrap (node:internal/test_runner/harness:289:17) {
    generatedMessage: true,
    code: 'ERR_ASSERTION',
    actual: undefined,
    expected: true,
    operator: '=='
  }

ℹ tests 1
ℹ suites 0
ℹ pass 0
ℹ fail 1
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 86.419333

✖ failing tests:

test at index.test.mjs:4:1
✖ node:test has a mock with a module property (7.442333ms)
  AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value:

    assert(mock.module)

      at TestContext.<anonymous> (file:///Users/jonrsharpe/workspace/so-examples/q79004444/index.test.mjs:5:2)
      at Test.runInAsyncScope (node:async_hooks:211:14)
      at Test.run (node:internal/test_runner/test:930:25)
      at Test.start (node:internal/test_runner/test:829:17)
      at startSubtestAfterBootstrap (node:internal/test_runner/harness:289:17) {
    generatedMessage: true,
    code: 'ERR_ASSERTION',
    actual: undefined,
    expected: true,
    operator: '=='
  }

除非设置了标志:

node --test --experimental-test-module-mocks
✔ node:test has a mock with a module property (1.00375ms)
ℹ tests 1
ℹ suites 0
ℹ pass 1
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 84.6655
© www.soinside.com 2019 - 2024. All rights reserved.