summaryrefslogtreecommitdiff
path: root/lib/internal/test_runner/tests_stream.js
blob: 7640e6742c19cec600fe5c2bed13593d2aae6446 (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
'use strict';
const {
  ArrayPrototypePush,
  ArrayPrototypeShift,
  Symbol,
} = primordials;
const Readable = require('internal/streams/readable');

const kEmitMessage = Symbol('kEmitMessage');
class TestsStream extends Readable {
  #buffer;
  #canPush;

  constructor() {
    super({ objectMode: true });
    this.#buffer = [];
    this.#canPush = true;
  }

  _read() {
    this.#canPush = true;

    while (this.#buffer.length > 0) {
      const obj = ArrayPrototypeShift(this.#buffer);

      if (!this.#tryPush(obj)) {
        return;
      }
    }
  }

  fail(nesting, file, testNumber, name, details, directive) {
    this[kEmitMessage]('test:fail', { __proto__: null, name, nesting, file, testNumber, details, ...directive });
  }

  ok(nesting, file, testNumber, name, details, directive) {
    this[kEmitMessage]('test:pass', { __proto__: null, name, nesting, file, testNumber, details, ...directive });
  }

  plan(nesting, file, count) {
    this[kEmitMessage]('test:plan', { __proto__: null, nesting, file, count });
  }

  getSkip(reason = undefined) {
    return { __proto__: null, skip: reason ?? true };
  }

  getTodo(reason = undefined) {
    return { __proto__: null, todo: reason ?? true };
  }

  start(nesting, file, name) {
    this[kEmitMessage]('test:start', { __proto__: null, nesting, file, name });
  }

  diagnostic(nesting, file, message) {
    this[kEmitMessage]('test:diagnostic', { __proto__: null, nesting, file, message });
  }

  coverage(nesting, file, summary) {
    this[kEmitMessage]('test:coverage', { __proto__: null, nesting, file, summary });
  }

  end() {
    this.#tryPush(null);
  }

  [kEmitMessage](type, data) {
    this.emit(type, data);
    this.#tryPush({ type, data });
  }

  #tryPush(message) {
    if (this.#canPush) {
      this.#canPush = this.push(message);
    } else {
      ArrayPrototypePush(this.#buffer, message);
    }

    return this.#canPush;
  }
}

module.exports = { TestsStream, kEmitMessage };