【问题标题】:Jest "No tests found" after update VSCode to 1.32.1将 VSCode 更新到 1.32.1 后开玩笑“未找到测试”
【发布时间】:2019-08-01 09:42:19
【问题描述】:

我正在使用带有 vscode 配置的调试玩笑,这里是 launch.json 配置:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Jest Current File",
            "program": "${workspaceFolder}/node_modules/.bin/jest",
            "args": [
                "${relativeFile}"
            ],
            "env": {
                "cross-env": "1",
                "NODE_PATH": "./src",
                "__PLATFORM__": " WEB",
            },
            "runtimeArgs": [
            ],
            "console": "integratedTerminal",
            "internalConsoleOptions": "neverOpen",
            "windows": {
                "program": "${workspaceFolder}/node_modules/jest/bin/jest",
            }
        }
    ]
}

在我将 VSCode 更新到 1.32.1 之前,此配置正常工作。现在,当我运行 Jest 当前文件时,控制台打印如下:

Debugger attached.
No tests found
In D:\workspace\my-project
  747 files checked.
  testMatch:  - 747 matches
  testPathIgnorePatterns: \\node_modules\\ - 747 matches
  testRegex: (\\__tests__\\.*|(\.|\\)(test))\.js?$ - 15 matches
Pattern: src\utils\storage\my-file-name.test.js - 0 matches
Waiting for the debugger to disconnect...

任何帮助将不胜感激,在此先感谢。

【问题讨论】:

    标签: javascript reactjs visual-studio-code jestjs


    【解决方案1】:

    安装旧版本VSCode(1.30.2)后,我看到了输出:

    Test Suites: 1 passed, 1 total
    Tests:       9 passed, 9 total
    Snapshots:   0 total
    Time:        4.866s
    Ran all test suites matching /src\\utils\\storage\\my-file-name.test.js/i.
    Waiting for the debugger to disconnect...
    

    区别是Pattern

    • v1.30.2:/src\\utils\\storage\\my-file-name.test.js/i.
    • v1.32.1:src\utils\storage\my-file-name.test.js

    VSCode 将 ${relativeFile} 的分隔符从 \\ 更改为 \,这就是为什么 jest 找不到测试文件的原因


    对于那些被卡住的人,只需将launch.json配置中的"${relativeFile}"更改为"${fileBasenameNoExtension}"即可:

    {
        "version": "0.2.0",
        "configurations": [
            {
                "type": "node",
                "request": "launch",
                "name": "Jest Current File",
                "program": "${workspaceFolder}/node_modules/.bin/jest",
                "args": [
                    "./${fileBasename}"
                ],
                "env": {
                    "cross-env": "1",
                    "NODE_PATH": "./src",
                    "__PLATFORM__": " WEB",
                },
                "runtimeArgs": [
                ],
                "console": "integratedTerminal",
                "internalConsoleOptions": "neverOpen",
                "windows": {
                    "program": "${workspaceFolder}/node_modules/jest/bin/jest",
                }
            }
        ]
    }
    

    【讨论】:

    • 对于拥有多个同名测试文件的其他人,此答案可能不起作用,因为 jest 将运行所有同名测试文件。相反,您可以将${fileBasenameNoExtension} 替换为${file}"。如果您不希望 Visual Studio Code 在每次运行测试时都打开一个新终端,还应该包含 --runInBand 参数:"args": ["${file}", "--runInBand"],
    【解决方案2】:

    最好使用--runTestsByPath ${relativeFile},它总是有效的。

    【讨论】:

    • 我在不同的目录中有几个同名的测试,并且为 windows 添加 --runTestsByPath 标志对我有用!:"args": ["test", "--runInBand", "--no-cache", "--no-watch", "${relativeFile}"], "windows": {"args": ["test", "--runInBand", "--no-cache", "--no-watch", "--runTestsByPath", "${relativeFile}"]},
    猜你喜欢
    • 2018-09-10
    • 2021-01-01
    • 2017-11-28
    • 1970-01-01
    • 1970-01-01
    • 2021-03-19
    • 2018-12-09
    • 2020-12-15
    • 2017-11-01
    相关资源
    最近更新 更多