diff --git a/Examples/Testing/README.md b/Examples/Testing/README.md index 2f28357a..ae1c68b6 100644 --- a/Examples/Testing/README.md +++ b/Examples/Testing/README.md @@ -31,3 +31,10 @@ llvm-cov show -instr-profile=.build/plugins/PackageToJS/outputs/PackageTests/def ```console npx serve .build/coverage/html ``` +## Customize test harness + +See [./run-tests-with-browser-options.mjs](./run-tests-with-browser-options.mjs) for an example of customizing the test harness to run tests with specific browser options. + +```console +node run-tests-with-browser-options.mjs +``` diff --git a/Examples/Testing/run-tests-with-browser-options.mjs b/Examples/Testing/run-tests-with-browser-options.mjs new file mode 100644 index 00000000..9051c28c --- /dev/null +++ b/Examples/Testing/run-tests-with-browser-options.mjs @@ -0,0 +1,23 @@ +// Import the generated test harness function +import { testBrowser } from "./.build/plugins/PackageToJS/outputs/PackageTests/test.js" + +// Execute the test with custom browser options +async function runTest(args) { + const exitCode = await testBrowser({ + args: args, + playwright: { + browser: "chromium", + launchOptions: { + headless: false, + } + } + }); + if (exitCode !== 0) { + process.exit(exitCode); + } +} +// Run XCTest test suites +await runTest([]); +// Run Swift Testing test suites +await runTest(["--testing-library", "swift-testing"]); +process.exit(0); diff --git a/Plugins/PackageToJS/Templates/test.d.ts b/Plugins/PackageToJS/Templates/test.d.ts index 21383997..03a064f2 100644 --- a/Plugins/PackageToJS/Templates/test.d.ts +++ b/Plugins/PackageToJS/Templates/test.d.ts @@ -9,8 +9,15 @@ export type SetupOptionsFn = ( export function testBrowser( options: { + /** Path to the prelude script to be injected before tests run */ preludeScript?: string, + /** Command-line arguments to pass to the test runner */ args?: string[], + /** Options for Playwright browser */ + playwright?: { + browser?: string, + launchOptions?: import("playwright").LaunchOptions + } } ): Promise diff --git a/Plugins/PackageToJS/Templates/test.js b/Plugins/PackageToJS/Templates/test.js index 518dacf2..19a1deac 100644 --- a/Plugins/PackageToJS/Templates/test.js +++ b/Plugins/PackageToJS/Templates/test.js @@ -96,7 +96,7 @@ Please run the following command to install it: process.exit(1); } })(); - const browser = await playwright.chromium.launch(); + const browser = await playwright[options.playwright?.browser ?? "chromium"].launch(options.playwright?.launchOptions ?? {}); const context = await browser.newContext(); const page = await context.newPage();