summaryrefslogtreecommitdiff
path: root/cli/js/testing.ts
blob: ed34e08f758df254dbfae74b5dc68cab2566b8a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { gray, green, italic, red, yellow } from "./colors.ts";
import { exit } from "./ops/os.ts";
import { Console, stringifyArgs } from "./web/console.ts";
import { stdout } from "./files.ts";
import { TextEncoder } from "./web/text_encoding.ts";

const RED_FAILED = red("FAILED");
const GREEN_OK = green("ok");
const YELLOW_SKIPPED = yellow("SKIPPED");
const disabledConsole = new Console((_x: string, _isErr?: boolean): void => {});

function formatDuration(time = 0): string {
  const timeStr = `(${time}ms)`;
  return gray(italic(timeStr));
}

export type TestFunction = () => void | Promise<void>;

export interface TestDefinition {
  fn: TestFunction;
  name: string;
  skip?: boolean;
}

const TEST_REGISTRY: TestDefinition[] = [];

export function test(t: TestDefinition): void;
export function test(fn: TestFunction): void;
export function test(name: string, fn: TestFunction): void;
// Main test function provided by Deno, as you can see it merely
// creates a new object with "name" and "fn" fields.
export function test(
  t: string | TestDefinition | TestFunction,
  fn?: TestFunction
): void {
  let testDef: TestDefinition;

  if (typeof t === "string") {
    if (!fn || typeof fn != "function") {
      throw new TypeError("Missing test function");
    }
    if (!t) {
      throw new TypeError("The test name can't be empty");
    }
    testDef = { fn: fn as TestFunction, name: t, skip: false };
  } else if (typeof t === "function") {
    if (!t.name) {
      throw new TypeError("The test function can't be anonymous");
    }
    testDef = { fn: t, name: t.name, skip: false };
  } else {
    if (!t.fn) {
      throw new TypeError("Missing test function");
    }
    if (!t.name) {
      throw new TypeError("The test name can't be empty");
    }
    testDef = { fn: t.fn, name: t.name, skip: Boolean(t.skip) };
  }

  TEST_REGISTRY.push(testDef);
}

interface TestStats {
  filtered: number;
  ignored: number;
  measured: number;
  passed: number;
  failed: number;
}

export interface RunTestsOptions {
  exitOnFail?: boolean;
  failFast?: boolean;
  only?: string | RegExp;
  skip?: string | RegExp;
  disableLog?: boolean;
  reporter?: TestReporter;
}

enum TestStatus {
  Passed = "passed",
  Failed = "failed",
  Skipped = "skipped"
}

interface TestResult {
  name: string;
  status: TestStatus;
  duration: number;
  error?: Error;
}

export enum TestEvent {
  Start = "start",
  TestStart = "testStart",
  TestEnd = "testEnd",
  End = "end"
}

interface TestEventStart {
  kind: TestEvent.Start;
  tests: number;
}

interface TestEventTestStart {
  kind: TestEvent.TestStart;
  name: string;
}

interface TestEventTestEnd {
  kind: TestEvent.TestEnd;
  result: TestResult;
}

interface TestEventEnd {
  kind: TestEvent.End;
  stats: TestStats;
  duration: number;
  results: TestResult[];
}

// TODO: already implements AsyncGenerator<RunTestsMessage>, but add as "implements to class"
// TODO: implements PromiseLike<TestsResult>
class TestApi {
  readonly testsToRun: TestDefinition[];
  readonly stats: TestStats = {
    filtered: 0,
    ignored: 0,
    measured: 0,
    passed: 0,
    failed: 0
  };

  constructor(
    public tests: TestDefinition[],
    public filterFn: (def: TestDefinition) => boolean,
    public failFast: boolean
  ) {
    this.testsToRun = tests.filter(filterFn);
    this.stats.filtered = tests.length - this.testsToRun.length;
  }

  async *[Symbol.asyncIterator](): AsyncIterator<
    TestEventStart | TestEventTestStart | TestEventTestEnd | TestEventEnd
  > {
    yield {
      kind: TestEvent.Start,
      tests: this.testsToRun.length
    };

    const results: TestResult[] = [];
    const suiteStart = +new Date();
    for (const { name, fn, skip } of this.testsToRun) {
      const result: Partial<TestResult> = { name, duration: 0 };
      yield { kind: TestEvent.TestStart, name };
      if (skip) {
        result.status = TestStatus.Skipped;
        this.stats.ignored++;
      } else {
        const start = +new Date();
        try {
          await fn();
          result.status = TestStatus.Passed;
          this.stats.passed++;
        } catch (err) {
          result.status = TestStatus.Failed;
          result.error = err;
          this.stats.failed++;
        } finally {
          result.duration = +new Date() - start;
        }
      }
      yield { kind: TestEvent.TestEnd, result: result as TestResult };
      results.push(result as TestResult);
      if (this.failFast && result.error != null) {
        break;
      }
    }

    const duration = +new Date() - suiteStart;

    yield {
      kind: TestEvent.End,
      stats: this.stats,
      results,
      duration
    };
  }
}

function createFilterFn(
  only: undefined | string | RegExp,
  skip: undefined | string | RegExp
): (def: TestDefinition) => boolean {
  return (def: TestDefinition): boolean => {
    let passes = true;

    if (only) {
      if (only instanceof RegExp) {
        passes = passes && only.test(def.name);
      } else {
        passes = passes && def.name.includes(only);
      }
    }

    if (skip) {
      if (skip instanceof RegExp) {
        passes = passes && !skip.test(def.name);
      } else {
        passes = passes && !def.name.includes(skip);
      }
    }

    return passes;
  };
}

interface TestReporter {
  start(msg: TestEventStart): Promise<void>;
  testStart(msg: TestEventTestStart): Promise<void>;
  testEnd(msg: TestEventTestEnd): Promise<void>;
  end(msg: TestEventEnd): Promise<void>;
}

export class ConsoleTestReporter implements TestReporter {
  private encoder: TextEncoder;

  constructor() {
    this.encoder = new TextEncoder();
  }

  private log(msg: string, noNewLine = false): void {
    if (!noNewLine) {
      msg += "\n";
    }

    // Using `stdout` here because it doesn't force new lines
    // compared to `console.log`; `core.print` on the other hand
    // is line-buffered and doesn't output message without newline
    stdout.writeSync(this.encoder.encode(msg));
  }

  async start(event: TestEventStart): Promise<void> {
    this.log(`running ${event.tests} tests`);
  }

  async testStart(event: TestEventTestStart): Promise<void> {
    const { name } = event;

    this.log(`test ${name} ... `, true);
  }

  async testEnd(event: TestEventTestEnd): Promise<void> {
    const { result } = event;

    switch (result.status) {
      case TestStatus.Passed:
        this.log(`${GREEN_OK} ${formatDuration(result.duration)}`);
        break;
      case TestStatus.Failed:
        this.log(`${RED_FAILED} ${formatDuration(result.duration)}`);
        break;
      case TestStatus.Skipped:
        this.log(`${YELLOW_SKIPPED} ${formatDuration(result.duration)}`);
        break;
    }
  }

  async end(event: TestEventEnd): Promise<void> {
    const { stats, duration, results } = event;
    // Attempting to match the output of Rust's test runner.
    const failedTests = results.filter(r => r.error);

    if (failedTests.length > 0) {
      this.log(`\nfailures:\n`);

      for (const result of failedTests) {
        this.log(`${result.name}`);
        this.log(`${stringifyArgs([result.error!])}`);
        this.log("");
      }

      this.log(`failures:\n`);

      for (const result of failedTests) {
        this.log(`\t${result.name}`);
      }
    }

    this.log(
      `\ntest result: ${stats.failed ? RED_FAILED : GREEN_OK}. ` +
        `${stats.passed} passed; ${stats.failed} failed; ` +
        `${stats.ignored} ignored; ${stats.measured} measured; ` +
        `${stats.filtered} filtered out ` +
        `${formatDuration(duration)}\n`
    );
  }
}

export async function runTests({
  exitOnFail = true,
  failFast = false,
  only = undefined,
  skip = undefined,
  disableLog = false,
  reporter = undefined
}: RunTestsOptions = {}): Promise<{
  results: TestResult[];
  stats: TestStats;
  duration: number;
}> {
  const filterFn = createFilterFn(only, skip);
  const testApi = new TestApi(TEST_REGISTRY, filterFn, failFast);

  if (!reporter) {
    reporter = new ConsoleTestReporter();
  }

  // @ts-ignore
  const originalConsole = globalThis.console;

  if (disableLog) {
    // @ts-ignore
    globalThis.console = disabledConsole;
  }

  let endMsg: TestEventEnd;

  for await (const testMsg of testApi) {
    switch (testMsg.kind) {
      case TestEvent.Start:
        await reporter.start(testMsg);
        continue;
      case TestEvent.TestStart:
        await reporter.testStart(testMsg);
        continue;
      case TestEvent.TestEnd:
        await reporter.testEnd(testMsg);
        continue;
      case TestEvent.End:
        endMsg = testMsg;
        delete endMsg!.kind;
        await reporter.end(testMsg);
        continue;
    }
  }

  if (disableLog) {
    // @ts-ignore
    globalThis.console = originalConsole;
  }

  if (endMsg!.stats.failed > 0 && exitOnFail) {
    exit(1);
  }

  return endMsg!;
}