wasm-pack 測試
wasm-pack test
命令包裝了 wasm-bindgen-test-runner CLI,讓您無需自行安裝不同的 Web 驅動程式,即可在不同的瀏覽器中執行 wasm 測試。
wasm-pack test --help
路徑
wasm-pack test
命令可以接受一個可選的路徑參數。
此路徑應指向包含 Cargo.toml
檔案的目錄。如果未提供路徑,則 test
命令將在目前目錄中執行。
# Run tests for the current directory's crate
wasm-pack test
# Run tests for a specified crate
wasm-pack test crates/crate-in-my-workspace
設定檔
test
命令接受一個可選的設定檔參數:--release
。
如果未提供任何參數,則將使用除錯測試版本。
測試環境
透過傳入任何測試環境旗標組合來選擇執行測試的位置。
--headless
適用於在 CI 流程中以無頭瀏覽器模式執行瀏覽器測試。
wasm-pack test --node --firefox --chrome --safari --headless
額外選項
即使 wasm-pack 不支援,test
命令也可以將額外選項直接傳遞給 cargo test
。
要使用它們,只需將額外參數添加到命令的最後,就像您對 cargo test
所做的一樣。
cargo test -h
可以查看您可以傳遞的所有選項列表。
僅執行部分測試
在除錯特定問題時,您可能會發現自己想要執行一部分測試,而不是整個測試套件。
以下是一些如何執行一部分測試的範例
# Example directory structure
$ tree crates/foo
├── Cargo.toml
├── README.md
├── src
│ ├── diff
│ │ ├── diff_test_case.rs
│ │ └── mod.rs
│ ├── lib.rs
└── tests
├── diff_patch.rs
└── node.rs
# Run all tests in tests/diff_patch.rs in Firefox
wasm-pack test crates/foo --firefox --headless --test diff_patch
# Run all tests in tests/diff_patch.rs that contain the word "replace"
wasm-pack test crates/foo --firefox --headless --test diff_patch replace
# Run all tests inside of a `tests` module inside of src/lib/diff.rs
wasm-pack test crates/foo --firefox --headless --lib diff::tests
# Same as the above, but only if they contain the word replace
wasm-pack test crates/foo --firefox --headless --lib diff::tests::replace
請注意,您也可以根據測試應執行的目標位置來篩選測試。例如
# Run all tests which are intended to execute in Node.js
wasm-pack test --node
# Run all tests which are intended to be executed in a browser
wasm-pack test --firefox --headless