javascript - 如何从 VSCode 中的多个位置运行 Jest 测试

标签 javascript typescript visual-studio-code jestjs

我设法在一定程度上模拟了 typeorm,但现在我面临着一个奇怪的问题,我将在这里进行说明。

import { myEntity } from './entity';
import typeorm = require('typeorm');

describe('test suire'), () => {

  let myRepository: typeorm.Repository<myEntity>;

  test('my test case', () => {

    typeorm.Repository = jest.fn().mockReturnValue({
      createQueryBuilder: jest.fn().mockReturnValue({
        where: jest.fn().mockReturnThis(),
        getMany: jest.fn().mockReturnValue([]);
      })
    });

    myRepository = new typeorm.Repository();

    expect(myRepository.createQueryBuilder).toHaveBeenCalledTimes(0);

  })
})

我有一个像这样的包结构:

root/
    package.json
    project1/
            package.json
            src/
               the_above_test.spec.ts

当我从 project1 运行 node_modules/.bin/jest path_to_above_test.spec.ts 时,它可以工作。但是当我从 root 运行相同的命令时,我得到:

Matcher error: received value must be a mock or spy function

Received has value: undefined at line:

    expect(myRepository.createQueryBuilder).toHaveBeenCalledTimes(0);

此处的目的是从 VS Code 中调试测试。但由于 VS code 在根级别打开,因此它从那里执行测试。如果我的代码中没有错误,我如何告诉 VS Code 从 project1 目录运行测试?

最佳答案

您可以在 /.vscode/launch.json 中指定不同的 jest 配置

每个配置都有自己的工作目录,使用“cwd”指定。

这是一个工作示例:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "name": "functions",
            "request": "launch",
            "program": "${workspaceFolder}/packages/functions/node_modules/jest/bin/jest",
            "args": [
                "./test/eventHooks.test.ts",
                "--runInBand"
            ],
            "cwd": "${workspaceFolder}/packages/functions",
            "console": "integratedTerminal",
            "internalConsoleOptions": "neverOpen"

        },
        {
            "type": "node",
            "name": "functions",
            "request": "launch",
            "program": "${workspaceFolder}/packages/types/node_modules/jest/bin/jest",
            "args": [
                "./test/eventHooks.test.ts",
                "--runInBand"
            ],
            "cwd": "${workspaceFolder}/packages/types",
            "console": "integratedTerminal",
            "internalConsoleOptions": "neverOpen"

        }

    ]
}

关于javascript - 如何从 VSCode 中的多个位置运行 Jest 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61051681/

相关文章:

Angular 5 模式验证不适用于动态 react 形式

javascript - 如何设置VSCode将花括号放在新行上?

python - ubuntu 上的 Visual Studio Code 无法运行 os.environ

javascript - 有条件地渲染一个 react 组件

javascript - 在多选 Material UI 上 react 渲染同级

javascript - 验证检查后执行验证中间件

typescript -> AST -> typescript

Angular Material 选项卡 : Prevent tab change of mat-tab-group if the form in current tab is dirty

php - Visual Studio Code PHP Intelephense 不断显示不必要的错误

javascript - jquery mobile 后的文本框样式