首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >通过launch.json运行和调试vs代码(windows) - Mocha grep选项:没有找到测试文件(或者运行所有测试,而不是模式)

通过launch.json运行和调试vs代码(windows) - Mocha grep选项:没有找到测试文件(或者运行所有测试,而不是模式)
EN

Stack Overflow用户
提问于 2022-11-06 06:32:15
回答 1查看 54关注 0票数 0

launch.json中的基本配置是运行vs代码(windows)中的所有mocha测试。

当尝试添加--grep选项时,无法获得预期的模式匹配行为(即只运行匹配测试)。通过尝试不同的组合,我没有发现错误的测试,也没有运行所有的测试。

注意-我可以让grep选项使用命令行(test:grep脚本-尽管模式文本是手动输入的)。

Expect --grep 'CURRENTTEST‘只使用描述中的这个字符串运行测试(例如,1通过测试)。这是我在使用grep选项运行命令行时获得的行为,如下所示:mocha -r ts-node/register -r tsconfig-paths/register "spec/**/*.ts" --grep CURRENTTEST

launch.json的实际行为如下所示:一些其他组合尝试运行所有测试(而不是模式匹配测试)。

其他args组合尝试;

  • 在同一行上具有grep选项作为测试位置,并作为带有单引号、双引号(带转义斜杠)和nothing.

的单独行

  • 模式。

以前的相关问题(但不是重复问题);https://stackoverflow.com/a/39012417/20429097 Running test cases selectively with Mocha https://mochajs.org/#-grep-regexp-g-regexp

(B)编码;

export function testFn(): number { return 1; }

测试;

代码语言:javascript
运行
复制
describe('CURRENTTEST test pass', () => {
  it('should pass', () => {
    expect(testFn()).to.equal(1);
  });
});
describe('test fail', () => {
  it('should fail', () => {
    expect(testFn()).to.equal(2);
  });
});

launch.json

代码语言:javascript
运行
复制
{
  "version": "0.2.0",
  "configurations": [
////////////////////////////// basic config to run all tests - works //////////////////////////////////////
    {
      "name": "mocha tests",
      "type": "node",
      "request": "launch",
      "program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
      "stopOnEntry": false,
      "args": [
        "-r",
        "ts-node/register",
        "${workspaceRoot}/spec/**/*.spec.ts",
        "--no-timeouts",
        "--colors",
        "--recursive",
      ],
      "cwd": "${workspaceRoot}",
      // "internalConsoleOptions": "openOnSessionStart",
      // "console": "integratedTerminal",
    },

/////////////////////// grep config to run CURRENTTEST only - doesn't work ////////////////////////////
    {
      "name": "mocha CURRENTTEST",
      "type": "node",
      "request": "launch",
      "program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
      "stopOnEntry": false,
      "args": [
        "-r",
        "ts-node/register",
        "${workspaceRoot}/spec/**/*.spec.ts --grep 'CURRENTTEST'",
        "--no-timeouts",
        "--colors",
        "--recursive",
      ],
      "cwd": "${workspaceRoot}",
      "internalConsoleOptions": "openOnSessionStart",
      // "console": "integratedTerminal",
    }
  ]
}

package.json

代码语言:javascript
运行
复制
{
  "name": "min code grep test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "dependencies": {
    "@types/chai": "latest",
    "@types/mocha": "latest",
    "@types/node": "latest",
    "chai": "latest",
    "eslint-import-resolver-typescript": "latest",
    "eslint-plugin-jsx-a11y": "latest",
    "eslint-plugin-react": "latest",
    "eslint-plugin-react-hooks": "latest",
    "ts-node": "latest",
    "typescript": "latest"
  },
  "devDependencies": {
    "@typescript-eslint/eslint-plugin": "latest",
    "@typescript-eslint/parser": "latest",
    "eslint": "latest",
    "eslint-config-airbnb-base": "latest",
    "eslint-config-airbnb-typescript": "latest",
    "eslint-config-google": "latest",
    "eslint-config-standard": "latest",
    "eslint-plugin-import": "latest",
    "eslint-plugin-node": "latest",
    "eslint-plugin-promise": "latest",
    "mocha": "latest"
  },
  "scripts": {
    "test": "mocha -r ts-node/register -r tsconfig-paths/register './spec/**/*.spec.ts'",
    "test:grep": "mocha -r ts-node/register -r tsconfig-paths/register \"spec/**/*.ts\" --grep"
  },
  "author": "",
  "license": "ISC"
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-11-11 03:50:55

每个参数都应该是数组中的一个新元素,例如:

代码语言:javascript
运行
复制
{
      "name": "mocha CURRENTTEST",
      "type": "node",
      "request": "launch",
      "program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
      "stopOnEntry": false,
      "args": [
        "-r",
        "ts-node/register",
        "${workspaceRoot}/spec/**/*.spec.ts",
        "--grep",
        "CURRENTTEST",
        "--no-timeouts",
        "--colors",
        "--recursive",
      ],
      "cwd": "${workspaceRoot}",
      "internalConsoleOptions": "openOnSessionStart",
      // "console": "integratedTerminal",
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74333733

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档