簡體   English   中英

如何從 VSCode 中的多個位置運行笑話測試

[英]how to run jest test from multiple locations in VSCode

我設法在一定程度上模擬了 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);

  })
})

我有一個像這樣的 package 結構:

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 代碼在根級別打開,它從那里執行測試。 如果我的代碼中沒有錯誤,我如何告訴 VS Code 從project1目錄運行測試?

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

每個配置都有自己的工作目錄,使用"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"

        }

    ]
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM