refactor: unit test runner communicates using TCP socket (#4336)

Rewrites "cli/js/unit_test_runner.ts" to communicate with spawned subprocesses 
using TCP socket.

* Rewrite "Deno.runTests()" by factoring out testing logic to private "TestApi" 
  class. "TestApi" implements "AsyncIterator" that yields "TestEvent"s, 
  which is an interface for different types of event occuring during running
  tests.

* Add "reporter" argument to "Deno.runTests()" to allow users to provide custom
  reporting mechanism for tests. It's represented by "TestReporter" interface,
  that implements hook functions for each type of "TestEvent". If "reporter"
  is not provided then default console reporting is used (via 
  "ConsoleReporter").

* Change how "unit_test_runner" communicates with spawned suprocesses. Instead
  of parsing text data from child's stdout, a TCP socket is created and used
  for communication. "unit_test_runner" can run in either "master" or "worker"
  mode. Former is responsible for test discovery and establishing needed
  permission combinations; while latter (that is spawned by "master") executes
  tests that match given permission set.

* Use "SocketReporter" that implements "TestReporter" interface to send output
  of tests to "master" process. Data is sent as stringified JSON and then
  parsed by "master" as structured data. "master" applies it's own reporting 
  logic to output tests to console (by reusing default "ConsoleReporter").
This commit is contained in:
Bartek Iwańczuk 2020-03-13 15:57:32 +01:00 committed by GitHub
parent e435c2be15
commit aab1acaed1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 561 additions and 857 deletions

View file

@ -32,6 +32,59 @@ declare namespace Deno {
* when `Deno.runTests` is used */
export function test(name: string, fn: TestFunction): void;
interface TestResult {
passed: boolean;
name: string;
skipped: boolean;
hasRun: boolean;
duration: number;
error?: Error;
}
interface TestStats {
filtered: number;
ignored: number;
measured: number;
passed: number;
failed: number;
}
export enum TestEvent {
Start = "start",
Result = "result",
End = "end"
}
interface TestEventStart {
kind: TestEvent.Start;
tests: number;
}
interface TestEventResult {
kind: TestEvent.Result;
result: TestResult;
}
interface TestEventEnd {
kind: TestEvent.End;
stats: TestStats;
duration: number;
results: TestResult[];
}
interface TestReporter {
start(event: TestEventStart): Promise<void>;
result(event: TestEventResult): Promise<void>;
end(event: TestEventEnd): Promise<void>;
}
export class ConsoleTestReporter implements TestReporter {
constructor();
start(event: TestEventStart): Promise<void>;
result(event: TestEventResult): Promise<void>;
end(event: TestEventEnd): Promise<void>;
}
export interface RunTestsOptions {
/** If `true`, Deno will exit with status code 1 if there was
* test failure. Defaults to `true`. */
@ -46,11 +99,19 @@ declare namespace Deno {
skip?: string | RegExp;
/** Disable logging of the results. Defaults to `false`. */
disableLog?: boolean;
/** Custom reporter class. If not provided uses console reporter. */
reporter?: TestReporter;
}
/** Run any tests which have been registered. Always resolves
* asynchronously. */
export function runTests(opts?: RunTestsOptions): Promise<void>;
export function runTests(
opts?: RunTestsOptions
): Promise<{
results: TestResult[];
stats: TestStats;
duration: number;
}>;
/** Get the `loadavg`. Requires `allow-env` permission.
*