這是 wasm-pack未發佈文件,已發佈的文件可在 Rust 和 WebAssembly 主要文件網站上找到。這裡記錄的功能可能無法在 wasm-pack 的發行版本中使用。

wasm-pack 測試

wasm-pack test 命令包裝了 wasm-bindgen-test-runner CLI,讓您無需自行安裝不同的網路驅動程式,即可在不同的瀏覽器中執行 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