|
| 1 | +# Testing |
| 2 | + |
| 3 | +Write and run tests for your Swift applications running on JavaScript environment using XCTest or swift-testing. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +JavaScriptKit supports running tests written with both XCTest and swift-testing. Tests are compiled to WebAssembly and executed in JavaScript environments (Node.js or browser). Unlike standard Swift testing, building and running tests are integrated into a single command that handles the JavaScript runtime setup. |
| 8 | + |
| 9 | +> Note: See the [Testing example](https://github.com/swiftwasm/JavaScriptKit/tree/main/Examples/Testing) for a complete working example. |
| 10 | +
|
| 11 | +## Setting Up Tests |
| 12 | + |
| 13 | +### Package Configuration |
| 14 | + |
| 15 | +Your `Package.swift` should have a test target with a dependency on your library target. Here's a simple example: |
| 16 | + |
| 17 | +```swift |
| 18 | +// swift-tools-version: 6.2 |
| 19 | + |
| 20 | +import PackageDescription |
| 21 | + |
| 22 | +let package = Package( |
| 23 | + name: "Example", |
| 24 | + products: [ |
| 25 | + .library(name: "Example", targets: ["Example"]), |
| 26 | + ], |
| 27 | + dependencies: [ |
| 28 | + .package(url: "https://github.com/swiftwasm/JavaScriptKit.git", branch: "main") |
| 29 | + ], |
| 30 | + targets: [ |
| 31 | + .target( |
| 32 | + name: "Example", |
| 33 | + dependencies: [ |
| 34 | + .product(name: "JavaScriptKit", package: "JavaScriptKit") |
| 35 | + ] |
| 36 | + ), |
| 37 | + .testTarget( |
| 38 | + name: "ExampleTests", |
| 39 | + dependencies: [ |
| 40 | + "Example", |
| 41 | + .product(name: "JavaScriptEventLoopTestSupport", package: "JavaScriptKit"), |
| 42 | + ] |
| 43 | + ), |
| 44 | + ] |
| 45 | +) |
| 46 | +``` |
| 47 | + |
| 48 | +> Important: Include `JavaScriptEventLoopTestSupport` in your test target dependencies. This is required to run tests in the JavaScript event loop environment. |
| 49 | +
|
| 50 | +### Writing Tests |
| 51 | + |
| 52 | +Create your test files in the `Tests/ExampleTests` directory. JavaScriptKit supports both XCTest and swift-testing. Write your tests using either framework as you would for standard Swift projects. |
| 53 | + |
| 54 | +## Running Tests |
| 55 | + |
| 56 | +### Basic Test Execution |
| 57 | + |
| 58 | +Run your tests using the `js test` subcommand: |
| 59 | + |
| 60 | +```bash |
| 61 | +swift package --disable-sandbox --swift-sdk $SWIFT_SDK_ID js test |
| 62 | +``` |
| 63 | + |
| 64 | +> Important: The `--disable-sandbox` flag is required because the test runner needs to execute npm and Playwright to run tests in JavaScript environments. |
| 65 | +
|
| 66 | +This command will: |
| 67 | +1. Build your test target as WebAssembly |
| 68 | +2. Package the tests with the necessary JavaScript runtime |
| 69 | +3. Execute the tests in Node.js by default |
| 70 | + |
| 71 | +The test binary is produced as `PackageTests.wasm` (where `Package` is your package name) and is located in `.build/plugins/PackageToJS/outputs/PackageTests/`. |
| 72 | + |
| 73 | +### Test Environment Options |
| 74 | + |
| 75 | +**Run tests in Node.js (default):** |
| 76 | + |
| 77 | +```bash |
| 78 | +swift package --disable-sandbox --swift-sdk $SWIFT_SDK_ID js test --environment node |
| 79 | +``` |
| 80 | + |
| 81 | +**Run tests in a browser:** |
| 82 | + |
| 83 | +```bash |
| 84 | +swift package --disable-sandbox --swift-sdk $SWIFT_SDK_ID js test --environment browser |
| 85 | +``` |
| 86 | + |
| 87 | +When running tests in the browser, the command will launch a browser instance using Playwright and execute the tests there. |
| 88 | + |
| 89 | +### Other Test Options |
| 90 | + |
| 91 | +**Build tests without running them:** |
| 92 | + |
| 93 | +```bash |
| 94 | +swift package --disable-sandbox --swift-sdk $SWIFT_SDK_ID js test --build-only |
| 95 | +``` |
| 96 | + |
| 97 | +After building, you can run the tests manually: |
| 98 | + |
| 99 | +```bash |
| 100 | +node .build/plugins/PackageToJS/outputs/PackageTests/bin/test.js |
| 101 | +``` |
| 102 | + |
| 103 | +**List available tests:** |
| 104 | + |
| 105 | +```bash |
| 106 | +swift package --disable-sandbox --swift-sdk $SWIFT_SDK_ID js test --list-tests |
| 107 | +``` |
| 108 | + |
| 109 | +**Filter tests by name:** |
| 110 | + |
| 111 | +```bash |
| 112 | +swift package --disable-sandbox --swift-sdk $SWIFT_SDK_ID js test --filter "testTrivial" |
| 113 | +``` |
| 114 | + |
| 115 | +**Enable inspector for browser tests:** |
| 116 | + |
| 117 | +```bash |
| 118 | +swift package --disable-sandbox --swift-sdk $SWIFT_SDK_ID js test --environment browser --inspect |
| 119 | +``` |
| 120 | + |
| 121 | +This starts a local server without automatically running tests. You can manually open `http://localhost:3000/test.browser.html` in your browser to run the tests and use the browser's DevTools for debugging. |
| 122 | + |
| 123 | +## Code Coverage |
| 124 | + |
| 125 | +To generate code coverage reports: |
| 126 | + |
| 127 | +1. **Run tests with code coverage enabled:** |
| 128 | + |
| 129 | +```bash |
| 130 | +swift package --disable-sandbox --swift-sdk $SWIFT_SDK_ID js test --enable-code-coverage |
| 131 | +``` |
| 132 | + |
| 133 | +2. **Generate HTML coverage report:** |
| 134 | + |
| 135 | +```bash |
| 136 | +llvm-cov show -instr-profile=.build/plugins/PackageToJS/outputs/PackageTests/default.profdata \ |
| 137 | + --format=html .build/plugins/PackageToJS/outputs/PackageTests/main.wasm \ |
| 138 | + -o .build/coverage/html Sources |
| 139 | +``` |
| 140 | + |
| 141 | +3. **View the coverage report:** |
| 142 | + |
| 143 | +```bash |
| 144 | +npx serve .build/coverage/html |
| 145 | +``` |
| 146 | + |
| 147 | + |
| 148 | + |
| 149 | +## Customizing Test Execution |
| 150 | + |
| 151 | +You can customize the test harness by importing the generated test function and calling it with custom options: |
| 152 | + |
| 153 | +```javascript |
| 154 | +// run-tests-custom.mjs |
| 155 | +import { testBrowser } from "./.build/plugins/PackageToJS/outputs/PackageTests/test.js"; |
| 156 | + |
| 157 | +async function runTest(args) { |
| 158 | + const exitCode = await testBrowser({ |
| 159 | + args: args, |
| 160 | + playwright: { |
| 161 | + browser: "chromium", |
| 162 | + launchOptions: { |
| 163 | + headless: false, |
| 164 | + } |
| 165 | + } |
| 166 | + }); |
| 167 | + if (exitCode !== 0) { |
| 168 | + process.exit(exitCode); |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +// Run XCTest test suites |
| 173 | +await runTest([]); |
| 174 | + |
| 175 | +// Run Swift Testing test suites |
| 176 | +await runTest(["--testing-library", "swift-testing"]); |
| 177 | + |
| 178 | +process.exit(0); |
| 179 | +``` |
| 180 | + |
| 181 | +Then run your custom test script: |
| 182 | + |
| 183 | +```bash |
| 184 | +node run-tests-custom.mjs |
| 185 | +``` |
0 commit comments