You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用TypeScript/esrun配合Node原生测试运行器时如何跳过测试?

使用esrun配合Node.js内置测试运行器时,--test-name-pattern参数不生效的解决办法

我正在使用Node.js内置测试运行器,编写了如下测试代码:

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.js命令执行时:

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

仅会执行thing测试套件,符合预期。但改用esrun运行时,执行命令:

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

所有测试都会被执行,过滤规则不生效。

解决办法

问题出在esrun的参数传递逻辑上:esrun会将自身的CLI参数和需要传递给Node.js的参数区分开,需要用--作为分隔符,把Node.js测试运行器的参数放在--之后。正确的命令应该是:

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

这样--test-name-pattern="thing"会被正确传递给Node.js内置测试运行器,就能实现按名称过滤测试的效果了。

内容的提问来源于stack exchange,提问作者mikemaccana

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 12:32:36