Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 37 additions & 16 deletions Documentation/WASI.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,52 @@ See https://swift.org/CONTRIBUTORS.txt for Swift project authors
because it provides instructions the reader must follow directly. -->

To run tests for WebAssembly, install a Swift SDK for WebAssembly by following
[these instructions](https://book.swiftwasm.org/getting-started/setup.html).
[these instructions](https://www.swift.org/documentation/articles/wasm-getting-started.html).

Because `swift test` doesn't know what WebAssembly environment you'd like to use
to run your tests, building tests and running them are two separate steps. To
build tests for WebAssembly, use the following command:

In Swift 6.3 and later, running `swift test --swift-sdk <wasm_swift_sdk_id>`
builds and runs your tests. Use `jq` to extract the Swift SDK ID automatically
to build and test in a single command:

swift test --swift-sdk \
"$(swiftc -print-target-info | jq -r '.swiftCompilerTag')_wasm"
```

## Build and Test WebAssembly separately

Prior to Swift 6.3, `swift test` doesn't support `--swift-sdk` option to
indicate the WebAssembly environment to use for tests. In this case, building
tests and running them are two separate steps. To build tests for WebAssembly,
use the following command:

```sh
swift build --swift-sdk wasm32-unknown-wasi --build-tests
swift build \
--swift-sdk "$(swiftc -print-target-info | jq -r '.swiftCompilerTag')_wasm" \
--build-tests
```

After building tests, you can run them using a [WASI](https://wasi.dev/)-compliant
WebAssembly runtime such as [Wasmtime](https://wasmtime.dev/) or
[WasmKit](https://github.com/swiftwasm/WasmKit). For example, to run tests using
Wasmtime, use the following command (replace `{YOURPACKAGE}` with your package's
name):
After building tests, you can run them using
a [WASI](https://wasi.dev/)-compliant WebAssembly runtime such as
[WasmKit](https://github.com/swiftwasm/WasmKit). WasmKit is included in
the Swift toolchain for Linux and macOS for Swift 6.2 and later.
[Download and install an open-source release toolchain from swift.org](https://swift.org/install)
to get a toolchain that includes WasmKit.

To run the rests you built previously using the WasmKit runtime, use the
following command, replacing `{YOURPACKAGE}` with the name of your package:

```sh
wasmtime .build/debug/{YOURPACKAGE}PackageTests.wasm --testing-library swift-testing
wasmkit run .build/debug/{YOURPACKAGE}PackageTests.wasm \
--testing-library swift-testing
```

Most WebAssembly runtimes forward trailing arguments to the WebAssembly program,
so you can pass command-line options of the testing library. For example, to list
all tests and filter them by name, use the following commands:
Most WebAssembly runtimes forward trailing arguments to the WebAssembly
program, so you can pass command-line options of the testing library. For
example, to list all tests and filter them by name, use the following commands:

```sh
wasmtime .build/debug/{YOURPACKAGE}PackageTests.wasm list --testing-library swift-testing
wasmtime .build/debug/{YOURPACKAGE}PackageTests.wasm --testing-library swift-testing --filter "FoodTruckTests.foodTruckExists"
wasmkit run .build/debug/{YOURPACKAGE}PackageTests.wasm list \
--testing-library swift-testing
wasmkit run .build/debug/{YOURPACKAGE}PackageTests.wasm \
--testing-library swift-testing --filter "FoodTruckTests.foodTruckExists"
```
Loading