chore: reowrked tests to use a runBrowserTest to allow previewing the results in a browser

This commit is contained in:
2024-02-17 21:12:54 +01:00
parent 1c55b894c9
commit 3b540d0c48
7 changed files with 254 additions and 132 deletions

View File

@@ -4,7 +4,7 @@
*/
import {puppeteerRunTest, TestFilterOptions, PageTestCallback} from "./puppeteer-run-test.ts";
import {puppeteerRunTest, PageTestCallback, TestOutput} from "./puppeteer-run-test.ts";
import {isInDebugMode} from "./debug-mode.ts";
import {resolve, posix} from "node:path";
@@ -28,7 +28,7 @@ import type {
} from 'http'
import type { ServerOptions } from 'https'
import type {ExecutionContext} from "ava";
import test, {ExecutionContext} from "ava";
import {createReadStream} from "fs";
@@ -37,32 +37,27 @@ type TypeMap = {
};
type ErrorCodeException = Error & {code: string};
export type TestResultCallback = (output: TestOutput)=>void;
export type LogCallback = (...args: string[])=>void;
export interface RollupServeTestOptions {
export interface ServeTestOptions {
/**
* Change the path to be opened when the test is started
* Remember to start with a slash, e.g. `'/different/page'`
*/
path?: string
/**
* Optionally specify what to filter from the output
*/
filterOutput?: TestFilterOptions;
/**
* Fallback to serving from a specified srcDir
* Fallback to serving from a specified srcDir, this allows setting breakpoints on sourcecode and test the sourcemaps
*/
srcDir?: string|boolean;
/**
* A callback to manually take control of the page and simulate user interactions
*/
cb?: PageTestCallback
/**
* The AVA context used to test (ie t.snapshot(..) )
*/
t: ExecutionContext
cb?: PageTestCallback;
/**
* Set to `true` to return index.html (200) instead of error page (404)
@@ -105,6 +100,20 @@ export interface RollupServeTestOptions {
* Execute function after server has begun listening
*/
onListening?: (server: Server) => void
}
export interface RollupServeTestOptions extends ServeTestOptions{
/**
* A callback to run when a test has been run
*/
onResult?: TestResultCallback;
/**
* Callback to log messages
*/
log?: LogCallback;
}
@@ -137,7 +146,7 @@ export default function serveTest (options: RollupServeTestOptions ): Plugin {
info: 34,
warn: 33,
}[mode];
testOptions.t.log(`\u001b[${modeColor}m${msg}\u001b[0m`);
testOptions.log?.(`\u001b[${modeColor}m${msg}\u001b[0m`);
}
const requestListener = async (request: IncomingMessage, response: ServerResponse) => {
@@ -266,11 +275,10 @@ export default function serveTest (options: RollupServeTestOptions ): Plugin {
first = false
const testOutput = await puppeteerRunTest({
page: testOptions.path!,
cb: testOptions.cb,
filterOutput: testOptions.filterOutput,
}, url)
testOptions.t?.snapshot?.(testOutput);
...testOptions
}, url);
testOptions.onResult?.(testOutput);
}
}
},