Skip to content

Commit 00f323c

Browse files
Update testing.md
1 parent 00240ad commit 00f323c

File tree

1 file changed

+35
-60
lines changed

1 file changed

+35
-60
lines changed

src/getting-started/testing.md

Lines changed: 35 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,79 @@
11
# Testing your app
22

3-
You can write a test suite for your SwiftWasm app or library, or run an existing test suite
4-
written for `XCTest` if you port existing code to SwiftWasm. Your project has to have a
5-
`Package.swift` package manifest for this to work. We assume that you use SwiftPM to build your
6-
project and that you have a working package manifest. Please follow [our SwiftPM guide](./swift-package.md) for new projects.
3+
SwiftWasm supports both `swift-testing` and `XCTest` for writing test suites. Your project needs to have a
4+
`Package.swift` package manifest with test targets configured. Please follow [our SwiftPM guide](./swift-package.md) for new projects.
75

8-
## A simple test case
6+
## Testing in JavaScript environments
97

10-
Let's assume you have an `Example` target in your project that you'd like to test. Your
11-
`Package.swift` should also have a test suite target with a dependency on the library target. It
12-
would probably look like this:
8+
If you're building a SwiftWasm app that runs in JavaScript environments (browsers or Node.js), you can use JavaScriptKit's testing utilities to run your tests directly in those environments. For detailed information on how to set up and run tests in JavaScript environments, please refer to the [JavaScriptKit Testing documentation](https://swiftpackageindex.com/swiftwasm/javascriptkit/documentation/javascriptkit/testing).
139

14-
```swift
15-
// swift-tools-version: 5.9
16-
17-
import PackageDescription
18-
19-
let package = Package(
20-
name: "Example",
21-
products: [
22-
.library(name: "Example", targets: ["Example"]),
23-
],
24-
targets: [
25-
.target(name: "Example"),
26-
.testTarget(name: "ExampleTests", dependencies: ["Example"]),
27-
]
28-
)
29-
```
10+
## Standalone testing with WASI
11+
12+
If you prefer to run tests in a standalone environment without JavaScript, you can use WASI-compatible runtimes as described below.
3013

31-
Now you should make sure there's `Tests/ExampleTests` subdirectory in your project.
32-
If you don't have any files in it yet, create `ExampleTests.swift` in it:
14+
Make sure your `Package.swift` has test targets configured. For example:
15+
16+
Note that `swift test` doesn't work for WebAssembly targets. Building tests and running them are two separate steps. After building your tests, you can use a WASI-compatible host such as [wasmtime](https://wasmtime.dev/) or [WasmKit](https://github.com/swiftwasm/WasmKit) to run the test bundle.
3317

3418
```swift
35-
import Example
36-
import XCTest
37-
38-
final class ExampleTests: XCTestCase {
39-
func testTrivial() {
40-
XCTAssertEqual(text, "Hello, world")
41-
}
42-
}
19+
targets: [
20+
.target(name: "Example"),
21+
.testTarget(name: "ExampleTests", dependencies: ["Example"]),
22+
]
4323
```
4424

45-
This code assumes that your `Example` defines some `text` with `"Hello, world"` value
46-
for this test to pass. Your test functions should all start with `test`, please see [XCTest
47-
documentation](https://developer.apple.com/documentation/xctest/defining_test_cases_and_test_methods)
48-
for more details.
25+
### Running swift-testing suites
26+
27+
For running `swift-testing` test suites, please refer to the [swift-testing WASI documentation](https://github.com/swiftlang/swift-testing/blob/main/Documentation/WASI.md).
4928

50-
## Building and running the test suite with `SwiftPM`
29+
### Running XCTest suites
5130

52-
You can build your test suite by running this command in your terminal:
31+
You can build your XCTest suite by running this command in your terminal:
5332

5433
```sh
55-
$ swift build --build-tests --triple wasm32-unknown-wasi
34+
$ swift build --build-tests --swift-sdk $SWIFT_SDK_ID
5635
```
5736

58-
If you're used to running `swift test` to run test suites for other Swift platforms, we have to
59-
warn you that this won't work. `swift test` doesn't know what WebAssembly environment you'd like to
60-
use to run your tests. Because of this building tests and running them are two separate steps when
61-
using `SwiftPM`. After your tests are built, you can use a WASI-compatible host such as
62-
[wasmtime](https://wasmtime.dev/) to run the test bundle:
37+
After building, run the test bundle with a WASI runtime. For example, with wasmtime:
6338

6439
```sh
65-
$ wasmtime --dir . .build/wasm32-unknown-wasi/debug/ExamplePackageTests.wasm
40+
$ wasmtime --dir . .build/wasm32-unknown-wasip1/debug/ExamplePackageTests.wasm
41+
```
42+
43+
Or with WasmKit:
44+
45+
```sh
46+
$ wasmkit run --dir . .build/wasm32-unknown-wasip1/debug/ExamplePackageTests.wasm
6647
```
6748

6849
(`--dir .` is used to allow XCTest to find `Bundle.main` resources placed alongside the executable file.)
6950

7051
As you can see, the produced test binary starts with the name of your package followed by
71-
`PackageTests.wasm`. It is located in the `.build/debug` subdirectory, or in the `.build/release`
52+
`PackageTests.wasm`. It is located in the `.build/wasm32-unknown-wasip1/debug` subdirectory, or in the `.build/wasm32-unknown-wasip1/release`
7253
subdirectory when you build in release mode.
7354

74-
## Code coverage with `SwiftPM`
55+
### Code coverage
7556

7657
> **Note**: Code coverage support is available only in 6.1 and later.
7758
7859
You can also generate code coverage reports for your test suite. To do this, you need to build your
7960
test suite with the `--enable-code-coverage` and linker options `-Xlinker -lwasi-emulated-getpid`:
8061

8162
```sh
82-
$ swift build --build-tests --swift-sdk wasm32-unknown-wasi --enable-code-coverage -Xlinker -lwasi-emulated-getpid
63+
$ swift build --build-tests --swift-sdk $SWIFT_SDK_ID --enable-code-coverage -Xlinker -lwasi-emulated-getpid
8364
```
8465

85-
After building your test suite, you can run it with `wasmtime` as described above. The raw coverage
66+
After building your test suite, you can run it with a WASI runtime (e.g., `wasmtime` or `WasmKit`) as described above. The raw coverage
8667
data will be stored in `default.profraw` file in the current directory. You can use the `llvm-profdata`
8768
and `llvm-cov` tools to generate a human-readable report:
8869

8970
```sh
90-
$ wasmtime --dir . .build/wasm32-unknown-wasi/debug/ExamplePackageTests.wasm
71+
$ wasmtime --dir . .build/wasm32-unknown-wasip1/debug/ExamplePackageTests.wasm
9172
$ llvm-profdata merge default.profraw -o default.profdata
92-
$ llvm-cov show .build/wasm32-unknown-wasi/debug/ExamplePackageTests.wasm -instr-profile=default.profdata
73+
$ llvm-cov show .build/wasm32-unknown-wasip1/debug/ExamplePackageTests.wasm -instr-profile=default.profdata
9374
# or generate an HTML report
94-
$ llvm-cov show .build/wasm32-unknown-wasi/debug/ExamplePackageTests.wasm -instr-profile=default.profdata --format=html -o coverage
75+
$ llvm-cov show .build/wasm32-unknown-wasip1/debug/ExamplePackageTests.wasm -instr-profile=default.profdata --format=html -o coverage
9576
$ open coverage/index.html
9677
```
9778

9879
![](./coverage-support.png)
99-
100-
## Building and running the test suite with `carton`
101-
102-
If you use [`carton`](https://carton.dev) to develop and build your app, as described in [our guide
103-
for browser apps](./browser-app.md), just run `swift run carton test` in the
104-
root directory of your package. This will automatically build the test suite and run it with a WASI runtime for you.

0 commit comments

Comments
 (0)