diff options
author | Nayeem Rahman <nayeemrmn99@gmail.com> | 2020-04-01 09:47:23 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-01 10:47:23 +0200 |
commit | 270e87d9db48e983671848257eb360b4c7405d31 (patch) | |
tree | 93d5de3a77343a7035287831bb535371ed6fea17 /cli/js/tests | |
parent | 017a611131a35ccf5dbfce6a2a665fa569e32ec1 (diff) |
refactor(cli/js/testing): Reduce testing interfaces (#4451)
* Reduce "testing" interfaces
* Use a callback instead of a generator for Deno.runTests()
* Default RunTestsOptions::reportToConsole to true
* Compose TestMessage into a single interface
Diffstat (limited to 'cli/js/tests')
-rw-r--r-- | cli/js/tests/format_error_test.ts | 4 | ||||
-rw-r--r-- | cli/js/tests/test_util.ts | 80 | ||||
-rwxr-xr-x | cli/js/tests/unit_test_runner.ts | 64 | ||||
-rw-r--r-- | cli/js/tests/url_test.ts | 4 |
4 files changed, 60 insertions, 92 deletions
diff --git a/cli/js/tests/format_error_test.ts b/cli/js/tests/format_error_test.ts index 42c16b0c0..0cb963ae6 100644 --- a/cli/js/tests/format_error_test.ts +++ b/cli/js/tests/format_error_test.ts @@ -31,7 +31,3 @@ unitTest(function formatDiagnosticError() { } assert(thrown); }); - -if (import.meta.main) { - Deno.runTests(); -} diff --git a/cli/js/tests/test_util.ts b/cli/js/tests/test_util.ts index da2e917f8..a3b4b6ce4 100644 --- a/cli/js/tests/test_util.ts +++ b/cli/js/tests/test_util.ts @@ -132,19 +132,21 @@ interface UnitTestDefinition extends Deno.TestDefinition { perms: Permissions; } +type TestFunction = () => void | Promise<void>; + export const REGISTERED_UNIT_TESTS: UnitTestDefinition[] = []; -export function unitTest(fn: Deno.TestFunction): void; -export function unitTest(options: UnitTestOptions, fn: Deno.TestFunction): void; +export function unitTest(fn: TestFunction): void; +export function unitTest(options: UnitTestOptions, fn: TestFunction): void; export function unitTest( - optionsOrFn: UnitTestOptions | Deno.TestFunction, - maybeFn?: Deno.TestFunction + optionsOrFn: UnitTestOptions | TestFunction, + maybeFn?: TestFunction ): void { assert(optionsOrFn, "At least one argument is required"); let options: UnitTestOptions; let name: string; - let fn: Deno.TestFunction; + let fn: TestFunction; if (typeof optionsOrFn === "function") { options = {}; @@ -196,44 +198,38 @@ export function createResolvable<T>(): Resolvable<T> { const encoder = new TextEncoder(); -export class SocketReporter implements Deno.TestReporter { - #conn: Deno.Conn; - - constructor(conn: Deno.Conn) { - this.#conn = conn; - } - - // eslint-disable-next-line @typescript-eslint/no-explicit-any - async write(msg: any): Promise<void> { - const encodedMsg = encoder.encode(JSON.stringify(msg) + "\n"); - await Deno.writeAll(this.#conn, encodedMsg); - } - - async start(msg: Deno.TestEventStart): Promise<void> { - await this.write(msg); - } - - async testStart(msg: Deno.TestEventTestStart): Promise<void> { - await this.write(msg); - } - - async testEnd(msg: Deno.TestEventTestEnd): Promise<void> { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - const serializedMsg: any = { ...msg }; - - // Error is a JS object, so we need to turn it into string to - // send over socket. - if (serializedMsg.result.error) { - serializedMsg.result.error = String(serializedMsg.result.error.stack); - } - - await this.write(serializedMsg); - } +// Replace functions with null, errors with their stack strings, and JSONify. +// eslint-disable-next-line @typescript-eslint/no-explicit-any +function serializeTestMessage(message: Deno.TestMessage): string { + return JSON.stringify({ + start: message.start && { + ...message.start, + tests: message.start.tests.map((test) => ({ ...test, fn: null })), + }, + testStart: message.testStart && { ...message.testStart, fn: null }, + testEnd: message.testEnd && { + ...message.testEnd, + error: String(message.testEnd.error?.stack), + }, + end: message.end && { + ...message.end, + results: message.end.results.map((result) => ({ + ...result, + error: result.error?.stack, + })), + }, + }); +} - async end(msg: Deno.TestEventEnd): Promise<void> { - const encodedMsg = encoder.encode(JSON.stringify(msg)); - await Deno.writeAll(this.#conn, encodedMsg); - this.#conn.closeWrite(); +export async function reportToConn( + conn: Deno.Conn, + message: Deno.TestMessage +): Promise<void> { + const line = serializeTestMessage(message); + const encodedMsg = encoder.encode(line + (message.end == null ? "\n" : "")); + await Deno.writeAll(conn, encodedMsg); + if (message.end != null) { + conn.closeWrite(); } } diff --git a/cli/js/tests/unit_test_runner.ts b/cli/js/tests/unit_test_runner.ts index 8d3eaa4f5..0232bb437 100755 --- a/cli/js/tests/unit_test_runner.ts +++ b/cli/js/tests/unit_test_runner.ts @@ -6,18 +6,20 @@ import { permissionCombinations, Permissions, registerUnitTests, - SocketReporter, fmtPerms, parseArgs, + reportToConn, } from "./test_util.ts"; +// eslint-disable-next-line @typescript-eslint/no-explicit-any +const reportToConsole = (Deno as any)[Deno.symbols.internal] + .reportToConsole as (message: Deno.TestMessage) => void; + interface PermissionSetTestResult { perms: Permissions; passed: boolean; - stats: Deno.TestStats; + endMessage: Deno.TestMessage["end"]; permsStr: string; - duration: number; - results: Deno.TestResult[]; } const PERMISSIONS: Deno.PermissionName[] = [ @@ -59,17 +61,16 @@ async function workerRunnerMain( } // Setup reporter const conn = await Deno.connect(addr); - const socketReporter = new SocketReporter(conn); // Drop current process permissions to requested set await dropWorkerPermissions(perms); // Register unit tests that match process permissions await registerUnitTests(); // Execute tests await Deno.runTests({ - failFast: false, exitOnFail: false, - reporter: socketReporter, only: filter, + reportToConsole: false, + onMessage: reportToConn.bind(null, conn), }); } @@ -117,7 +118,6 @@ async function runTestsForPermissionSet( listener: Deno.Listener, addrStr: string, verbose: boolean, - reporter: Deno.ConsoleTestReporter, perms: Permissions, filter?: string ): Promise<PermissionSetTestResult> { @@ -128,22 +128,16 @@ async function runTestsForPermissionSet( const conn = await listener.accept(); let expectedPassedTests; - let endEvent; + let endMessage: Deno.TestMessage["end"]; try { for await (const line of readLines(conn)) { - const msg = JSON.parse(line); - - if (msg.kind === Deno.TestEvent.Start) { - expectedPassedTests = msg.tests; - await reporter.start(msg); - } else if (msg.kind === Deno.TestEvent.TestStart) { - await reporter.testStart(msg); - } else if (msg.kind === Deno.TestEvent.TestEnd) { - await reporter.testEnd(msg); - } else { - endEvent = msg; - await reporter.end(msg); + const message = JSON.parse(line) as Deno.TestMessage; + reportToConsole(message); + if (message.start != null) { + expectedPassedTests = message.start.tests.length; + } else if (message.end != null) { + endMessage = message.end; } } } finally { @@ -151,11 +145,11 @@ async function runTestsForPermissionSet( conn.close(); } - if (expectedPassedTests === undefined) { + if (expectedPassedTests == null) { throw new Error("Worker runner didn't report start"); } - if (endEvent === undefined) { + if (endMessage == null) { throw new Error("Worker runner didn't report end"); } @@ -168,16 +162,13 @@ async function runTestsForPermissionSet( workerProcess.close(); - const passed = - expectedPassedTests === endEvent.stats.passed + endEvent.stats.ignored; + const passed = expectedPassedTests === endMessage.passed + endMessage.ignored; return { perms, passed, permsStr: permsFmt, - duration: endEvent.duration, - stats: endEvent.stats, - results: endEvent.results, + endMessage, }; } @@ -195,7 +186,6 @@ async function masterRunnerMain( } const testResults = new Set<PermissionSetTestResult>(); - const consoleReporter = new Deno.ConsoleTestReporter(); const addr = { hostname: "127.0.0.1", port: 4510 }; const addrStr = `${addr.hostname}:${addr.port}`; const listener = Deno.listen(addr); @@ -205,7 +195,6 @@ async function masterRunnerMain( listener, addrStr, verbose, - consoleReporter, perms, filter ); @@ -217,14 +206,9 @@ async function masterRunnerMain( let testsPassed = true; for (const testResult of testResults) { - const { permsStr, stats, duration, results } = testResult; + const { permsStr, endMessage } = testResult; console.log(`Summary for ${permsStr}`); - await consoleReporter.end({ - kind: Deno.TestEvent.End, - stats, - duration, - results, - }); + reportToConsole({ end: endMessage }); testsPassed = testsPassed && testResult.passed; } @@ -312,11 +296,7 @@ async function main(): Promise<void> { // Running tests matching current process permissions await registerUnitTests(); - await Deno.runTests({ - failFast: false, - exitOnFail: true, - only: filter, - }); + await Deno.runTests({ only: filter }); } main(); diff --git a/cli/js/tests/url_test.ts b/cli/js/tests/url_test.ts index e1b2d47bd..e4d497ffb 100644 --- a/cli/js/tests/url_test.ts +++ b/cli/js/tests/url_test.ts @@ -212,7 +212,3 @@ unitTest(function createBadUrl(): void { new URL("0.0.0.0:8080"); }); }); - -if (import.meta.main) { - Deno.runTests(); -} |