簡體   English   中英

如何通過命令行告訴 Jest 不要運行某個測試文件,而是運行所有其他文件?

[英]How can I tell Jest through the command line not to run a certain test file, but run all others?

我有三個測試文件, acceptance.test.js routes.test.js snapshots.test.js

我想在我的 package.json 中創建一個 npm 腳本,它在使用時會忽略這些測試文件之一。 我正在嘗試使用 Jest 選項--testPathPattern來執行此操作。 例如:

  "scripts": {
    "no-snapshots-test": "jest --coverage --testPathPattern=^(?!snapshots).*$"
  }

但是,這個特定示例.*$ was unexpected at this time在終端中給出了.*$ was unexpected at this time錯誤。 我試過從最后刪除 .*$ ,但這會導致我的快照測試仍在運行。

允許我指定要忽略的文件(或多個文件)的正確正則表達式是什么? 我需要在 CLI 中執行此操作,因此我無法使用 Jest 配置選項 testPathIgnorePattern。

不支持開頭的^和結尾的$ 忽略它們並使用相同的正則表達式,而不會產生負面的前瞻性:

"scripts": {
    "no-snapshots-test": "jest --coverage --testPathPattern=snapshots.*"
}

只是一個猜測:您可能需要使用正確的JavaScript Regex語法:/ /snapshots.*/g .*/ /snapshots.*/g

而不是試圖用一個負先行在testPathPattern可以使用的testPathIgnorePattern代替。 它沒有記錄在 jest CLI 文檔中,但它仍然有效。

"scripts": {
    "no-snapshots-test": "jest --coverage --testPathIgnorePattern=snapshots"
  }

如果忽略模式與您的測試太多匹配,您可能需要更改忽略模式。

暫無
暫無

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

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