簡體   English   中英

在 Jest 中運行特定測試套件的單個測試

[英]Run single test of a specific test suite in Jest

我有一個名為“test-file-1”的文件,在其中我有一些描述(測試套件)具有唯一名稱,在其中我有測試,這些測試可能在測試套件中具有相似的名稱。

要運行單個測試套件或測試,請鍵入以下命令。

npm test -- -t "Test Suite or Test"

我試圖弄清楚如何運行特定測試套件的單個測試。

給出要測試的文件的路徑:

npm test path/of/your/testfile.js -t "test name"

將僅運行此特定文件的名為“測試名稱”的測試(或組)。

在嘗試了很多不同的事情后,我發現它比我想象的要簡單。

您只需將測試放在同一個字符串中的測試套件之后:

npm test -- -t "<Test Suite> <Test>"

通過"testMatch":["**/path/testSuite/*.js"]指向 jest 配置文件中的測試套件

現在通過jest testName給出唯一的 testname 來jest testName

在這里,您每次都必須更新 testMatch,這是您的 testSuite

對我來說,它只適用於:

npm run test -- tests/01_login.js --testcase "Should login into Dashboard"

npm run <script> -- <test suite path> --testcase "<test case>"

我在 package.json 中的腳本:

"test": "env-cmd -f ./.env nightwatch --retries 2 --env selenium.chrome",

在守夜人版本 1.3.4

import LoggerService from '../LoggerService ';

describe('Method called****', () => {
  it('00000000', () => {
    const logEvent = jest.spyOn(LoggerService , 'logEvent');
    expect(logEvent).toBeDefined();
  });
});

在命令下運行 npm test -- tests/LoggerService.test.ts -t '00000000'

如果您使用的是 jest 配置文件,則可以使用testNamePattern鍵進行配置。 所以你的配置文件可能看起來像

jest-e2e.json

{
  "moduleFileExtensions": ["js", "json", "ts"],
  "rootDir": ".",
  "testEnvironment": "node",
  "testRegex": ".spec.ts$",
  "testNamePattern": "login",
  "transform": {
    "^.+\\.(t|j)s$": "ts-jest"
  },
  "bail": true,
  "verbose": true,
  "testTimeout": 30000
}


這只會運行在測試模式之一中具有“登錄”功能的測試

參考: https : //jestjs.io/docs/cli#--testnamepatternregex

暫無
暫無

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

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