在 TypeScript/esrun 中使用节点本机测试运行器时如何跳过测试?

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

我正在使用节点测试运行器。在一个小演示文件中进行一些测试:

import { describe, test } from "node:test";
import assert from "node:assert";

describe("thing", () => {
  test("it works", () => {
    assert.ok(true);
  });
});

describe("this should not be tested", () => {
  test("it works", () => {
    assert.ok(true);
  });
});

当我运行时

node --test-name-pattern="thing" runme.test.mjs
我可以看到它跳过
this should not be tested

▶ thing
  ✔ it works (0.153ms)
▶ thing (1.306708ms)

▶ this should not be tested
  ﹣ it works (0.13525ms) # test name does not match pattern
▶ this should not be tested (0.23425ms)

ℹ tests 2
ℹ suites 2
ℹ pass 1
ℹ fail 0
ℹ cancelled 0
ℹ skipped 1
ℹ todo 0
ℹ duration_ms 0.079417

太棒了!我可以看到

test name does not be tested
被跳过了!

但是我想使用 TypeScript 进行测试。使用

esrun
并遵循 esrun 文档 我正在运行:

esrun runme.test.mjs --test-name-pattern="thing"

(是的,这和之前的JS文件是一样的,esrun也可以运行JS文件)

但是测试不会被跳过:

▶ thing
  ✔ it works (0.11725ms)
▶ thing (0.945292ms)

▶ this should not be tested
  ✔ it works (0.040375ms)
▶ this should not be tested (0.100708ms)

ℹ tests 2
ℹ suites 2
ℹ pass 2
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 0.105375

在 TypeScript/esrun 中使用节点本机测试运行程序时如何跳过测试?

node.js typescript unit-testing node-test-runner esrun
1个回答
0
投票

回答我自己的问题来帮助别人

esrun 文档中提到了正确的语法,但我错过了:

您可以通过在选项名称前加上“--node”前缀来将自定义选项传递到节点 cli,如下所示: esrun --node-max-old-space-size=4096 foo.ts

因此,要跳过使用本机测试运行程序的测试,请使用:

esrun --node-test-name-pattern="thing" runme.test.mjs

▶ thing
  ✔ it works (0.144791ms)
▶ thing (1.027917ms)

▶ this should not be tested
  ﹣ it works (0.048459ms) # test name does not match pattern
▶ this should not be tested (0.124208ms)

ℹ tests 2
ℹ suites 2
ℹ pass 1
ℹ fail 0
ℹ cancelled 0
ℹ skipped 1
ℹ todo 0
ℹ duration_ms 0.124166
© www.soinside.com 2019 - 2024. All rights reserved.