@japa/runner 3.0.0-7 → 3.0.0-9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/build/examples/dot.d.ts +1 -0
  2. package/build/examples/dot.js +11 -0
  3. package/build/examples/ndjson.d.ts +1 -0
  4. package/build/examples/ndjson.js +11 -0
  5. package/build/examples/spec.d.ts +1 -0
  6. package/build/examples/spec.js +11 -0
  7. package/build/factories/create_diverse_tests.d.ts +6 -0
  8. package/build/factories/create_diverse_tests.js +106 -0
  9. package/build/factories/main.d.ts +3 -0
  10. package/build/factories/main.js +12 -0
  11. package/build/factories/runner.d.ts +7 -8
  12. package/build/factories/runner.js +16 -129
  13. package/build/modules/core/main.d.ts +1 -0
  14. package/build/src/config_manager.js +1 -1
  15. package/build/src/debug.d.ts +1 -1
  16. package/build/src/files_manager.d.ts +1 -1
  17. package/build/src/helpers.d.ts +14 -21
  18. package/build/src/helpers.js +26 -2
  19. package/build/src/planner.d.ts +3 -3
  20. package/build/src/plugins/retry.js +2 -2
  21. package/build/src/reporters/dot.js +5 -5
  22. package/build/src/reporters/ndjson.js +1 -1
  23. package/build/src/reporters/spec.js +20 -22
  24. package/build/src/types.d.ts +1 -1
  25. package/build/tests/base_reporter.spec.d.ts +1 -0
  26. package/build/tests/base_reporter.spec.js +111 -0
  27. package/build/tests/cli_parser.spec.d.ts +1 -0
  28. package/build/tests/cli_parser.spec.js +265 -0
  29. package/build/tests/config_manager.spec.d.ts +1 -0
  30. package/build/tests/config_manager.spec.js +741 -0
  31. package/build/tests/core.spec.d.ts +1 -0
  32. package/build/tests/core.spec.js +31 -0
  33. package/build/tests/files_manager.spec.d.ts +1 -0
  34. package/build/tests/files_manager.spec.js +153 -0
  35. package/build/tests/planner.spec.d.ts +1 -0
  36. package/build/tests/planner.spec.js +510 -0
  37. package/build/tests/runner.spec.d.ts +1 -0
  38. package/build/tests/runner.spec.js +442 -0
  39. package/build/tests_helpers/main.d.ts +7 -0
  40. package/build/tests_helpers/main.js +34 -0
  41. package/package.json +10 -16
@@ -8,7 +8,7 @@
8
8
  */
9
9
  import ms from 'ms';
10
10
  import { relative } from 'node:path';
11
- import cliui from '../helpers.js';
11
+ import { colors, icons } from '../helpers.js';
12
12
  import { BaseReporter } from '../../modules/core/main.js';
13
13
  /**
14
14
  * Pretty prints the tests on the console
@@ -24,20 +24,18 @@ export class SpecReporter extends BaseReporter {
24
24
  */
25
25
  #getTestIcon(payload) {
26
26
  if (payload.isTodo) {
27
- return cliui.colors.cyan(cliui.icons.info);
27
+ return colors.cyan(icons.info);
28
28
  }
29
29
  if (payload.isFailing) {
30
- return payload.hasError
31
- ? cliui.colors.magenta(cliui.icons.squareSmallFilled)
32
- : cliui.colors.red(cliui.icons.cross);
30
+ return payload.hasError ? colors.magenta(icons.squareSmallFilled) : colors.red(icons.cross);
33
31
  }
34
32
  if (payload.hasError) {
35
- return cliui.colors.red(cliui.icons.cross);
33
+ return colors.red(icons.cross);
36
34
  }
37
35
  if (payload.isSkipped) {
38
- return cliui.colors.yellow(cliui.icons.bullet);
36
+ return colors.yellow(icons.bullet);
39
37
  }
40
- return cliui.colors.green(cliui.icons.tick);
38
+ return colors.green(icons.tick);
41
39
  }
42
40
  /**
43
41
  * Returns the test message
@@ -45,38 +43,38 @@ export class SpecReporter extends BaseReporter {
45
43
  #getTestMessage(payload) {
46
44
  const message = typeof payload.title === 'string' ? payload.title : payload.title.expanded;
47
45
  if (payload.isTodo) {
48
- return cliui.colors.blue(message);
46
+ return colors.blue(message);
49
47
  }
50
48
  if (payload.isFailing) {
51
- return payload.hasError ? cliui.colors.magenta(message) : cliui.colors.red(message);
49
+ return payload.hasError ? colors.magenta(message) : colors.red(message);
52
50
  }
53
51
  if (payload.hasError) {
54
- return cliui.colors.red(message);
52
+ return colors.red(message);
55
53
  }
56
54
  if (payload.isSkipped) {
57
- return cliui.colors.yellow(message);
55
+ return colors.yellow(message);
58
56
  }
59
- return cliui.colors.grey(message);
57
+ return colors.grey(message);
60
58
  }
61
59
  /**
62
60
  * Returns the subtext message for the test
63
61
  */
64
62
  #getSubText(payload) {
65
63
  if (payload.isSkipped && payload.skipReason) {
66
- return cliui.colors.yellow(payload.skipReason);
64
+ return colors.yellow(payload.skipReason);
67
65
  }
68
66
  if (!payload.isFailing) {
69
67
  return;
70
68
  }
71
69
  if (!payload.hasError) {
72
- return cliui.colors.magenta(`Test marked with ".fails()" must finish with an error`);
70
+ return colors.magenta(`Test marked with ".fails()" must finish with an error`);
73
71
  }
74
72
  if (payload.failReason) {
75
- return cliui.colors.magenta(payload.failReason);
73
+ return colors.magenta(payload.failReason);
76
74
  }
77
75
  const testErrorMessage = payload.errors.find((error) => error.phase === 'test');
78
76
  if (testErrorMessage && testErrorMessage.error) {
79
- return cliui.colors.magenta(testErrorMessage.error.message);
77
+ return colors.magenta(testErrorMessage.error.message);
80
78
  }
81
79
  }
82
80
  /**
@@ -91,11 +89,11 @@ export class SpecReporter extends BaseReporter {
91
89
  #printTest(payload) {
92
90
  const icon = this.#getTestIcon(payload);
93
91
  const message = this.#getTestMessage(payload);
94
- const prefix = payload.isPinned ? cliui.colors.yellow('[PINNED] ') : '';
92
+ const prefix = payload.isPinned ? colors.yellow('[PINNED] ') : '';
95
93
  const indentation = this.currentFileName || this.currentGroupName ? ' ' : '';
96
- const duration = cliui.colors.dim(`(${ms(Number(payload.duration.toFixed(2)))})`);
94
+ const duration = colors.dim(`(${ms(Number(payload.duration.toFixed(2)))})`);
97
95
  const retries = payload.retryAttempt && payload.retryAttempt > 1
98
- ? cliui.colors.dim(`(x${payload.retryAttempt}) `)
96
+ ? colors.dim(`(x${payload.retryAttempt}) `)
99
97
  : '';
100
98
  let subText = this.#getSubText(payload);
101
99
  subText = subText ? `\n${indentation} ${subText}` : '';
@@ -109,7 +107,7 @@ export class SpecReporter extends BaseReporter {
109
107
  ? `${this.currentSuiteName} / ${payload.title}`
110
108
  : payload.title;
111
109
  const suffix = this.currentFileName
112
- ? cliui.colors.dim(` (${this.#getRelativeFilename(this.currentFileName)})`)
110
+ ? colors.dim(` (${this.#getRelativeFilename(this.currentFileName)})`)
113
111
  : '';
114
112
  console.log(`\n${title}${suffix}`);
115
113
  }
@@ -122,7 +120,7 @@ export class SpecReporter extends BaseReporter {
122
120
  * - Test is first in a sequence
123
121
  */
124
122
  if (this.currentFileName && this.#isFirstLoneTest) {
125
- console.log(`\n${cliui.colors.dim(this.#getRelativeFilename(this.currentFileName))}`);
123
+ console.log(`\n${colors.dim(this.#getRelativeFilename(this.currentFileName))}`);
126
124
  }
127
125
  this.#isFirstLoneTest = false;
128
126
  }
@@ -1,4 +1,4 @@
1
- /// <reference types="@types/node" resolution-mode="require"/>
1
+ /// <reference types="node" resolution-mode="require"/>
2
2
  import type { HookHandler } from '@poppinss/hooks/types';
3
3
  import type { Emitter, Refiner, Runner, Suite } from '../modules/core/main.js';
4
4
  import type { FilteringOptions, NamedReporterContract } from '../modules/core/types.js';
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,111 @@
1
+ /*
2
+ * @japa/runner
3
+ *
4
+ * (c) Japa
5
+ *
6
+ * For the full copyright and license information, please view the LICENSE
7
+ * file that was distributed with this source code.
8
+ */
9
+ import { test } from 'node:test';
10
+ import chaiSubset from 'chai-subset';
11
+ import { assert, default as chai } from 'chai';
12
+ chai.use(chaiSubset);
13
+ import { wrapAssertions } from '../tests_helpers/main.js';
14
+ import { Test, Suite, Group, Runner, Refiner, Emitter, TestContext, BaseReporter, } from '../modules/core/main.js';
15
+ test.describe('Base reporter', () => {
16
+ test('extend base reporter to create a custom reporter', async () => {
17
+ const stack = [];
18
+ class MyReporter extends BaseReporter {
19
+ async start() {
20
+ stack.push('reporter started');
21
+ }
22
+ }
23
+ const emitter = new Emitter();
24
+ const runner = new Runner(emitter);
25
+ runner.registerReporter((r, e) => new MyReporter({}).boot(r, e));
26
+ await runner.start();
27
+ await wrapAssertions(() => {
28
+ assert.deepEqual(stack, ['reporter started']);
29
+ });
30
+ });
31
+ test('invoke handlers when suite, groups and tests are executed', async () => {
32
+ const stack = [];
33
+ let summary;
34
+ class MyReporter extends BaseReporter {
35
+ async start() {
36
+ stack.push('reporter started');
37
+ }
38
+ async end() {
39
+ summary = this.runner.getSummary();
40
+ stack.push('reporter ended');
41
+ }
42
+ onTestStart(t) {
43
+ assert.equal(t.title.expanded, '2 + 2');
44
+ assert.equal(this.currentSuiteName, 'unit');
45
+ assert.equal(this.currentGroupName, 'default');
46
+ stack.push('test started');
47
+ }
48
+ onTestEnd(t) {
49
+ assert.equal(t.title.expanded, '2 + 2');
50
+ assert.isFalse(t.hasError);
51
+ stack.push('test ended');
52
+ }
53
+ onGroupStart(g) {
54
+ assert.equal(g.title, 'default');
55
+ assert.equal(this.currentSuiteName, 'unit');
56
+ stack.push('group started');
57
+ }
58
+ onGroupEnd(g) {
59
+ assert.equal(g.title, 'default');
60
+ assert.isFalse(g.hasError);
61
+ stack.push('group ended');
62
+ }
63
+ onSuiteStart(s) {
64
+ assert.equal(s.name, 'unit');
65
+ stack.push('suite started');
66
+ }
67
+ onSuiteEnd(s) {
68
+ assert.equal(s.name, 'unit');
69
+ stack.push('suite ended');
70
+ }
71
+ }
72
+ const emitter = new Emitter();
73
+ const runner = new Runner(emitter);
74
+ const refiner = new Refiner();
75
+ const suite = new Suite('unit', emitter, refiner);
76
+ const group = new Group('default', emitter, refiner);
77
+ const t = new Test('2 + 2', (_t) => new TestContext(_t), emitter, refiner, group);
78
+ group.add(t);
79
+ suite.add(group);
80
+ runner.add(suite);
81
+ runner.registerReporter((r, e) => new MyReporter({}).boot(r, e));
82
+ await runner.start();
83
+ await runner.exec();
84
+ await runner.end();
85
+ await wrapAssertions(() => {
86
+ assert.deepEqual(stack, [
87
+ 'reporter started',
88
+ 'suite started',
89
+ 'group started',
90
+ 'test started',
91
+ 'test ended',
92
+ 'group ended',
93
+ 'suite ended',
94
+ 'reporter ended',
95
+ ]);
96
+ assert.containSubset(summary, {
97
+ aggregates: {
98
+ total: 1,
99
+ failed: 0,
100
+ passed: 0,
101
+ regression: 0,
102
+ skipped: 0,
103
+ todo: 1,
104
+ },
105
+ hasError: false,
106
+ failureTree: [],
107
+ failedTestsTitles: [],
108
+ });
109
+ });
110
+ });
111
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,265 @@
1
+ /*
2
+ * @japa/runner
3
+ *
4
+ * (c) Japa
5
+ *
6
+ * For the full copyright and license information, please view the LICENSE
7
+ * file that was distributed with this source code.
8
+ */
9
+ import { assert } from 'chai';
10
+ import { test } from 'node:test';
11
+ import colors from '@poppinss/colors';
12
+ import { CliParser } from '../src/cli_parser.js';
13
+ import { wrapAssertions } from '../tests_helpers/main.js';
14
+ const DATASET = [
15
+ [
16
+ new CliParser().parse([]),
17
+ {
18
+ '_': [],
19
+ 'files': '',
20
+ 'groups': '',
21
+ 'reporter': '',
22
+ 'h': false,
23
+ 'help': false,
24
+ 'failed': false,
25
+ 'match-all': false,
26
+ 'matchAll': false,
27
+ 'retries': '',
28
+ 'tags': '',
29
+ 'tests': '',
30
+ 'timeout': '',
31
+ },
32
+ ],
33
+ [
34
+ new CliParser().parse(['unit', 'functional']),
35
+ {
36
+ '_': ['unit', 'functional'],
37
+ 'files': '',
38
+ 'reporter': '',
39
+ 'groups': '',
40
+ 'h': false,
41
+ 'help': false,
42
+ 'failed': false,
43
+ 'match-all': false,
44
+ 'matchAll': false,
45
+ 'retries': '',
46
+ 'tags': '',
47
+ 'tests': '',
48
+ 'timeout': '',
49
+ },
50
+ ],
51
+ [
52
+ new CliParser().parse(['--timeout', '1000']),
53
+ {
54
+ '_': [],
55
+ 'files': '',
56
+ 'reporter': '',
57
+ 'groups': '',
58
+ 'h': false,
59
+ 'help': false,
60
+ 'failed': false,
61
+ 'match-all': false,
62
+ 'matchAll': false,
63
+ 'retries': '',
64
+ 'tags': '',
65
+ 'tests': '',
66
+ 'timeout': '1000',
67
+ },
68
+ ],
69
+ [
70
+ new CliParser().parse(['--timeout', '1000', '--retries', '2']),
71
+ {
72
+ '_': [],
73
+ 'files': '',
74
+ 'reporter': '',
75
+ 'groups': '',
76
+ 'h': false,
77
+ 'help': false,
78
+ 'failed': false,
79
+ 'match-all': false,
80
+ 'matchAll': false,
81
+ 'retries': '2',
82
+ 'tags': '',
83
+ 'tests': '',
84
+ 'timeout': '1000',
85
+ },
86
+ ],
87
+ [
88
+ new CliParser().parse(['--match-all']),
89
+ {
90
+ '_': [],
91
+ 'files': '',
92
+ 'reporter': '',
93
+ 'groups': '',
94
+ 'h': false,
95
+ 'help': false,
96
+ 'failed': false,
97
+ 'match-all': true,
98
+ 'matchAll': true,
99
+ 'retries': '',
100
+ 'tags': '',
101
+ 'tests': '',
102
+ 'timeout': '',
103
+ },
104
+ ],
105
+ [
106
+ new CliParser().parse(['--force-exit']),
107
+ {
108
+ '_': [],
109
+ 'files': '',
110
+ 'force-exit': true,
111
+ 'forceExit': true,
112
+ 'reporter': '',
113
+ 'groups': '',
114
+ 'h': false,
115
+ 'help': false,
116
+ 'failed': false,
117
+ 'match-all': false,
118
+ 'matchAll': false,
119
+ 'retries': '',
120
+ 'tags': '',
121
+ 'tests': '',
122
+ 'timeout': '',
123
+ },
124
+ ],
125
+ [
126
+ new CliParser().parse(['--browser', 'chrome', '--browser', 'firefox']),
127
+ {
128
+ '_': [],
129
+ 'files': '',
130
+ 'browser': ['chrome', 'firefox'],
131
+ 'reporter': '',
132
+ 'groups': '',
133
+ 'h': false,
134
+ 'help': false,
135
+ 'failed': false,
136
+ 'match-all': false,
137
+ 'matchAll': false,
138
+ 'retries': '',
139
+ 'tags': '',
140
+ 'tests': '',
141
+ 'timeout': '',
142
+ },
143
+ ],
144
+ [
145
+ new CliParser().parse(['--reporter', 'spec']),
146
+ {
147
+ '_': [],
148
+ 'files': '',
149
+ 'reporter': 'spec',
150
+ 'groups': '',
151
+ 'h': false,
152
+ 'help': false,
153
+ 'failed': false,
154
+ 'match-all': false,
155
+ 'matchAll': false,
156
+ 'retries': '',
157
+ 'tags': '',
158
+ 'tests': '',
159
+ 'timeout': '',
160
+ },
161
+ ],
162
+ [
163
+ new CliParser().parse(['--reporter', 'spec', '--reporter', 'dot']),
164
+ {
165
+ '_': [],
166
+ 'files': '',
167
+ 'reporter': ['spec', 'dot'],
168
+ 'groups': '',
169
+ 'h': false,
170
+ 'help': false,
171
+ 'failed': false,
172
+ 'match-all': false,
173
+ 'matchAll': false,
174
+ 'retries': '',
175
+ 'tags': '',
176
+ 'tests': '',
177
+ 'timeout': '',
178
+ },
179
+ ],
180
+ [
181
+ new CliParser().parse(['--reporter', 'spec,dot']),
182
+ {
183
+ '_': [],
184
+ 'files': '',
185
+ 'reporter': 'spec,dot',
186
+ 'groups': '',
187
+ 'h': false,
188
+ 'help': false,
189
+ 'failed': false,
190
+ 'match-all': false,
191
+ 'matchAll': false,
192
+ 'retries': '',
193
+ 'tags': '',
194
+ 'tests': '',
195
+ 'timeout': '',
196
+ },
197
+ ],
198
+ [
199
+ new CliParser().parse(['--failed']),
200
+ {
201
+ '_': [],
202
+ 'files': '',
203
+ 'reporter': '',
204
+ 'groups': '',
205
+ 'h': false,
206
+ 'help': false,
207
+ 'failed': true,
208
+ 'match-all': false,
209
+ 'matchAll': false,
210
+ 'retries': '',
211
+ 'tags': '',
212
+ 'tests': '',
213
+ 'timeout': '',
214
+ },
215
+ ],
216
+ ];
217
+ test.describe('CLI parser', () => {
218
+ test('parse CLI arguments', async () => {
219
+ for (let [cliArgs, output] of DATASET) {
220
+ await wrapAssertions(() => {
221
+ assert.deepEqual(cliArgs, output);
222
+ });
223
+ }
224
+ });
225
+ if (!process.env.CI) {
226
+ test('display help', async () => {
227
+ const ansi = colors.ansi();
228
+ console.log(new CliParser().getHelp());
229
+ await wrapAssertions(() => {
230
+ assert.deepEqual(new CliParser().getHelp().split('\n'), [
231
+ '',
232
+ ansi.yellow('@japa/runner v2.3.0'),
233
+ '',
234
+ `${ansi.green('--tests')} ${ansi.dim('Filter tests by the test title')}`,
235
+ `${ansi.green('--groups')} ${ansi.dim('Filter tests by the group title')}`,
236
+ `${ansi.green('--tags')} ${ansi.dim('Filter tests by tags')}`,
237
+ `${ansi.green('--files')} ${ansi.dim('Filter tests by the file name')}`,
238
+ `${ansi.green('--force-exit')} ${ansi.dim('Forcefully exit the process')}`,
239
+ `${ansi.green('--timeout')} ${ansi.dim('Define default timeout for all tests')}`,
240
+ `${ansi.green('--retries')} ${ansi.dim('Define default retries for all tests')}`,
241
+ `${ansi.green('--reporter')} ${ansi.dim('Activate one or more test reporters')}`,
242
+ `${ansi.green('--failed')} ${ansi.dim('Run tests failed during the last run')}`,
243
+ `${ansi.green('-h, --help')} ${ansi.dim('View help')}`,
244
+ ``,
245
+ `${ansi.yellow('Examples:')}`,
246
+ `${ansi.dim('node bin/test.js --tags="@github"')}`,
247
+ `${ansi.dim('node bin/test.js --tags="~@github"')}`,
248
+ `${ansi.dim('node bin/test.js --tags="@github,@slow,@integration" --match-all')}`,
249
+ `${ansi.dim('node bin/test.js --force-exit')}`,
250
+ `${ansi.dim('node bin/test.js --files="user"')}`,
251
+ `${ansi.dim('node bin/test.js --files="functional/user"')}`,
252
+ `${ansi.dim('node bin/test.js --files="unit/user"')}`,
253
+ ``,
254
+ `${ansi.yellow('Notes:')}`,
255
+ `- When groups and tests filters are applied together. We will first filter the`,
256
+ ` tests by group title and then apply the tests title filter.`,
257
+ `- The timeout defined on test object takes precedence over the ${ansi.green('--timeout')} flag.`,
258
+ `- The retries defined on test object takes precedence over the ${ansi.green('--retries')} flag.`,
259
+ `- The ${ansi.green('--files')} flag checks for the file names ending with the filter substring.`,
260
+ ``,
261
+ ]);
262
+ });
263
+ });
264
+ }
265
+ });
@@ -0,0 +1 @@
1
+ export {};