[英]Run only ONE test with Jest
我只想用 Jest 運行一個測試。
我使用it.only
或describe.only
,但它仍然運行大量測試。 我認為自從我上次提交以來它運行了所有測試,但它不應該在明確設置only
標志的情況下出現這種行為,對嗎?
是什么導致了這種行為,我該如何運行單個測試?
Jest並行化測試運行,它不知道應該運行哪些測試以及不應該運行哪些測試。 這意味着當您使用“fit”時,它只會在該文件中運行一個測試。 但它仍然運行您項目中的所有其他測試文件。
fit
、 fdescribe
和it.only
、 describe.only
具有相同的目的:跳過其他測試並只運行我。
來源: https : //github.com/facebook/jest/issues/698#issuecomment-177673281
使用 Jest 過濾機制。 當你運行你的測試時,
jest --config=jest.config.json --watch
您可以通過testname
或filename
過濾測試。 只需按照終端中的說明進行操作即可。
按p
,然后鍵入文件名。
然后你可以使用describe.only
和it.only
,這將跳過過濾后的測試文件中的所有其他測試。
it.only
和describe.only
僅適用於它們所在的模塊。如果您在過濾多個文件中的測試時遇到問題,您可以使用jest -t name-of-spec
,過濾與指定名稱匹配的測試(匹配描述或測試中的名稱)。
來源: Jest CLI 選項
例如,我專注於我目前正在編寫的測試(使用package.json
的測試腳本):
npm test -- -t "test foo"
對我來說,如果我使用兩個這樣的參數,它會起作用:
yarn test --watch -f "src/...fullpath.../reducer.spec.js" -t "Name of the test"
標志:
--watch: is optional
-f: will do filtering of your files, so if you have a lot of tests with the same name, specify the full path to the exact file
-t: works with 'describe' name or 'it' name of your test
是這樣的:
jest sometest.test.js -t "some expression to match a describe or a test"
它將測試所有名為sometest.test.js
並基於 -t 選項匹配的文件。 如果你只想測試一個特定的文件,你可以這樣做:
jest src/user/.../sometest.test.js
我創建了一個讓 Jest 像您(和我)期望的那樣工作的命令。 這是正在進行的工作,但應該像這樣工作:
從具有.only
tests/describes 的文件運行測試,或者如果沒有.only
則運行所有這些文件。
grep --exclude-dir=node_modules -rl . -e 'test.only\|it.only\|describe.only' --null | tr '\n' ' ' | xargs -0 npx jest | grep . || npx jest
即使這在某些邊緣情況下不起作用,您也可以將其作為參考。 我可能會在這里保持更新。
您可以為此創建一個別名,將其放在 shell 腳本或 package.json 腳本中。
更新| grep. || npx jest
| grep. || npx jest
最后的| grep. || npx jest
是一種解決方法,因為 xargs 應該在沒有輸入的情況下運行一次,但它不在我的機器上。 可能平時不需要。
嘗試輸入 0 匹配該模式的完整路徑結果,即使該文件存在並且有幾個斷言。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.