|
1 | 1 | import { expect } from 'bupkis'; |
2 | | -import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises'; |
3 | | -import { tmpdir } from 'node:os'; |
4 | | -import { join } from 'node:path'; |
5 | | -import { afterEach, beforeEach, describe, it } from 'node:test'; |
| 2 | +import { describe, it } from 'node:test'; |
6 | 3 |
|
7 | 4 | import { runCommand } from '../util.js'; |
| 5 | +import { fixtures } from './fixture-paths.js'; |
8 | 6 |
|
9 | 7 | /** |
10 | 8 | * Integration tests for --bail flag functionality. The bail flag should stop |
11 | 9 | * execution on the first benchmark failure |
12 | 10 | */ |
13 | 11 |
|
14 | 12 | describe('Bail flag (--bail)', () => { |
15 | | - let tempDir: string; |
16 | | - |
17 | | - beforeEach(async () => { |
18 | | - tempDir = await mkdtemp(join(tmpdir(), 'modestbench-test-')); |
19 | | - await mkdir(join(tempDir, 'benchmarks'), { recursive: true }); |
20 | | - }); |
21 | | - |
22 | | - afterEach(async () => { |
23 | | - await rm(tempDir, { force: true, recursive: true }); |
24 | | - }); |
25 | | - |
26 | 13 | it('should stop execution after first failing benchmark', async () => { |
27 | | - // Create first benchmark file that will fail |
28 | | - const failingBench = join(tempDir, 'benchmarks', '1-failing.bench.js'); |
29 | | - await writeFile( |
30 | | - failingBench, |
31 | | - ` |
32 | | - export default { |
33 | | - suites: { |
34 | | - 'Failing Suite': { |
35 | | - benchmarks: { |
36 | | - 'failing task': { |
37 | | - fn: () => { |
38 | | - throw new Error('Intentional failure'); |
39 | | - } |
40 | | - } |
41 | | - } |
42 | | - } |
43 | | - } |
44 | | - }; |
45 | | - `, |
46 | | - ); |
47 | | - |
48 | | - // Create second benchmark file that should NOT run |
49 | | - const successBench = join(tempDir, 'benchmarks', '2-success.bench.js'); |
50 | | - await writeFile( |
51 | | - successBench, |
52 | | - ` |
53 | | - export default { |
54 | | - suites: { |
55 | | - 'Success Suite': { |
56 | | - benchmarks: { |
57 | | - 'success task': { |
58 | | - fn: () => 1 |
59 | | - } |
60 | | - } |
61 | | - } |
62 | | - } |
63 | | - }; |
64 | | - `, |
65 | | - ); |
66 | | - |
67 | | - // Run with --bail flag |
68 | | - const result = await runCommand( |
69 | | - [ |
70 | | - 'run', |
71 | | - join(tempDir, 'benchmarks', '*.bench.js'), |
72 | | - '--bail', |
73 | | - '--reporter', |
74 | | - 'human', |
75 | | - ], |
76 | | - tempDir, |
77 | | - ); |
| 14 | + // Run with --bail flag - failing fixture runs first (alphabetically) |
| 15 | + // failing.bench.js comes before success.bench.js |
| 16 | + const result = await runCommand([ |
| 17 | + 'run', |
| 18 | + fixtures.failing, |
| 19 | + fixtures.success, |
| 20 | + '--bail', |
| 21 | + '--reporter', |
| 22 | + 'human', |
| 23 | + ]); |
78 | 24 |
|
79 | 25 | // Should exit with non-zero code due to failure |
80 | 26 | expect(result.exitCode, 'to be greater than', 0); |
81 | 27 |
|
82 | 28 | const combinedOutput = result.stdout + result.stderr; |
83 | 29 |
|
84 | 30 | // Verify first benchmark ran and failed |
85 | | - expect(combinedOutput, 'to match', /1-failing\.bench\.js/); |
| 31 | + expect(combinedOutput, 'to match', /Failing Suite/); |
86 | 32 | expect(combinedOutput, 'to match', /failing task/); |
87 | 33 | expect(combinedOutput, 'to match', /Intentional failure/); |
88 | 34 |
|
89 | | - // Second benchmark file should NOT appear in output when --bail is used |
| 35 | + // Second benchmark suite should NOT appear in output when --bail is used |
90 | 36 | // (this is the key assertion that will pass once bail is implemented) |
91 | | - expect(combinedOutput, 'not to match', /2-success\.bench\.js/); |
| 37 | + expect(combinedOutput, 'not to match', /Success Suite/); |
92 | 38 | expect(combinedOutput, 'not to match', /success task/); |
93 | 39 | }); |
94 | 40 |
|
95 | 41 | it('should run all benchmarks without --bail flag (control)', async () => { |
96 | | - // Create first benchmark file that will fail |
97 | | - const failingBench = join(tempDir, 'benchmarks', '1-failing.bench.js'); |
98 | | - await writeFile( |
99 | | - failingBench, |
100 | | - ` |
101 | | - export default { |
102 | | - suites: { |
103 | | - 'Failing Suite': { |
104 | | - benchmarks: { |
105 | | - 'failing task': { |
106 | | - fn: () => { |
107 | | - throw new Error('Intentional failure'); |
108 | | - } |
109 | | - } |
110 | | - } |
111 | | - } |
112 | | - } |
113 | | - }; |
114 | | - `, |
115 | | - ); |
116 | | - |
117 | | - // Create second benchmark file |
118 | | - const successBench = join(tempDir, 'benchmarks', '2-success.bench.js'); |
119 | | - await writeFile( |
120 | | - successBench, |
121 | | - ` |
122 | | - export default { |
123 | | - suites: { |
124 | | - 'Success Suite': { |
125 | | - benchmarks: { |
126 | | - 'success task': { |
127 | | - fn: () => 1 |
128 | | - } |
129 | | - } |
130 | | - } |
131 | | - } |
132 | | - }; |
133 | | - `, |
134 | | - ); |
135 | | - |
136 | 42 | // Run WITHOUT --bail flag - both benchmarks should run |
137 | | - const result = await runCommand( |
138 | | - ['run', join(tempDir, 'benchmarks', '*.bench.js'), '--reporter', 'human'], |
139 | | - tempDir, |
140 | | - ); |
| 43 | + const result = await runCommand([ |
| 44 | + 'run', |
| 45 | + fixtures.failing, |
| 46 | + fixtures.success, |
| 47 | + '--reporter', |
| 48 | + 'human', |
| 49 | + ]); |
141 | 50 |
|
142 | 51 | // Should exit with non-zero code due to failure |
143 | 52 | expect(result.exitCode, 'to be greater than', 0); |
144 | 53 |
|
145 | 54 | const combinedOutput = result.stdout + result.stderr; |
146 | 55 |
|
147 | 56 | // Both benchmarks should appear in output (default behavior without --bail) |
148 | | - expect(combinedOutput, 'to match', /1-failing\.bench\.js/); |
| 57 | + expect(combinedOutput, 'to match', /Failing Suite/); |
149 | 58 | expect(combinedOutput, 'to match', /failing task/); |
150 | | - expect(combinedOutput, 'to match', /2-success\.bench\.js/); |
| 59 | + expect(combinedOutput, 'to match', /Success Suite/); |
151 | 60 | expect(combinedOutput, 'to match', /success task/); |
152 | 61 | }); |
153 | 62 | }); |
0 commit comments