見出し画像

Rust and WebAssembly Game of Life - Testing -

Rust and WebAssembly book の 4.4. Implementing Conway's Game of Life に引き続き、 4.5. Testing Conway's Game of Life に進みます。ここでも見事につまづいたポイントがありました。WSL2上のUbuntuで進めてるがゆえ。。。

まず結論

WSL2 上で wasm-pack test --firefox (多分 --chrome でも) --headless をする場合は、WindowsでFirefox/Chromeをインストールしていても apt install firefox しておくように。

経緯

チュートリアルでは、1世代後のLifeが期待通りに変化するかを検証するテストを作成します。作成したテストを実行するため、以下のコマンドを実行します。

wasm-pack test --firefox --headless

Rust コンパイルが正常に完了した後、WASMとしてのテストが実行されます。ここで、冒頭の install firefox をしていなかった場合(WSL2上にFirefoxがインストールされていなかった場合)、wasm-pack test で下記のようなエラーがレポートされます。

Set timeout to 20 seconds...
Running headless tests in Firefox on `http://127.0.0.1:36159/`
Try find `webdriver.json` for configure browser's capabilities:
Ok
driver status: signal: 9
driver stdout:
1626273576244 geckodriver INFO Listening on 127.0.0.1:36159

Error: non-200 response code: 500
{"value":{"error":"session not created","message":"Expected browser binary location, but unable to find binary in default location, no 'moz:firefoxOptions.binary' capability provided, and no binary flag set on the command line","stacktrace":""}}
error: test failed, to rerun pass '--test web'

Running headless tests in Firefox on ... とあるので一見動いていそうなのですが、テストは実施されていません(のでFailとある)。

はじめは、通信が出来ていないのかな、とか「geckodriverの」Pathが見つからないのかなとか、そんなことをネットで検索、調べていました。だって Firefox は 「Windows」には入れていましたから。

そこで、不意に思い立って wasm-pack help test を実行してヘルプを見てみました。すると、下記の文言が。

USAGE:
    wasm-pack test [FLAGS] [OPTIONS] [path-and-extra-options]...
FLAGS:
    --firefox Run the tests in Firefox. This machine must have a Firefox installation. If the `geckodriver` WebDriver client is not on the `$PATH`, and not specified with `--geckodriver`, then `wasm-pack`

この This machine がポイントで、いまチュートリアルを進めている環境は WSL2 であって、Windowsそのものではありません。よって、WSL2 上の wasm-pack は 「WSL2ローカルの」 firefoxが見つからずにテストを実行出来なかったのでは、と考えました。

そこで、 WSL2 上にfirefoxをインストールし、改めて wasm-pack test を実行すると、下記のようにテストを実行することができるようになりました。

running 2 tests
test web::test_tick ... ok
test web::pass ... ok
test result: ok. 2 passed; 0 failed; 0 ignored
console.log div contained:

test web::pass はデフォルトで入っていたテストで必ずPassするサンプルテストなので無視するとして、 test web::test_tick が今回作成したテストです。どうやらちゃんと実行されているようです。動作結果と期待値が一致するようなテストなので、OK(Pass) となっています。

ここで、期待値側をいじって(違う値にして)再度テストを実行してみます。すると、今度はおびただしいメッセージとともに FAIL とレポートされました。ちゃんとテストが出来ているようです。

test web::test_tick ... FAIL
test web::pass ... ok
failures:
---- web::test_tick output ----
log output:
Universe.new is called.
Universe.new is called.
 error output:
panicked at 'assertion failed: `(left == right)`
left: `[Dead, Dead, Dead, Dead, Dead, Dead, Dead, Dead, Dead, Dead, Dead, Dead, Dead, Alive, Dead, Alive, Dead, Dead, Dead, Dead, Alive, Alive, Dead, Dead, Dead, Dead, Alive, Dead, Dead, Dead, Dead, Dead, Dead, Dead, Dead, Dead]`,
right: `[Dead, Dead, Dead, Dead, Dead, Dead, Dead, Dead, Dead, Dead, Dead, Dead, Dead, Alive, Dead, Alive, Dead, Dead, Dead, Dead, Alive, Alive, Dead, Dead, Dead, Dead, Dead, Alive, Dead, Dead, Dead, Dead, Dead, Dead, Dead, Dead]`', tests/web.rs:43:5
 Stack:
 init/imports.wbg.__wbg_new_59cb74e423758ede/<@http://127.0.0.1:37083/wasm-bindgen-test:693:19
logError@http://127.0.0.1:37083/wasm-bindgen-test:153:18
init/imports.wbg.__wbg_new_59cb74e423758ede@http://127.0.0.1:37083/wasm-bindgen-test:692:66
...

これで、Rust and WebAssembly で Testing までできるようになりました。
チュートリアルの節数としては折返し地点となります(が、最後の Publishing to npm はきっとやらない)。

おわりに

firefox ですが、パスを指定してあげれば Windows上のFirefoxでもキック出来たのでしょうか…。が、あまり変なことはしないことにします。

最後まで読んでいただき、ありがとうございました。

この記事が気に入ったらサポートをしてみませんか?