|
1 | 1 | import * as chai from 'chai'; |
2 | 2 | chai.should(); |
3 | 3 | import { expect } from 'expect'; |
4 | | -import { describe } from 'mocha'; |
| 4 | +import { describe, it, before } from 'mocha'; |
5 | 5 | import path from 'path'; |
6 | 6 | import { exec } from 'child_process'; |
7 | 7 | import { fileURLToPath } from 'url'; |
| 8 | +import util from 'util'; // Import Node's utility module |
| 9 | + |
| 10 | +const execAsync = util.promisify(exec); |
8 | 11 | const __filename = fileURLToPath(import.meta.url); |
9 | 12 | const __dirname = path.dirname(__filename); |
10 | 13 |
|
11 | | -const runner = path.join(__dirname, '/../../bin/codecept.js') |
12 | | -const codecept_dir = path.join(__dirname, '/../data/sandbox/configs/run-rerun/') |
13 | | -const codecept_run = `${runner} run-rerun` |
14 | | -const codecept_run_config = (config, grep) => `${codecept_run} --config ${codecept_dir}/${config} --grep "${grep || ''}"` |
| 14 | +const runner = path.resolve(__dirname, '../../bin/codecept.js') |
| 15 | +const codecept_dir = path.resolve(__dirname, '../data/sandbox/configs/run-rerun') |
| 16 | +const codecept_run = `node ${runner} run-rerun` |
| 17 | +const codecept_run_config = (config, grep) => `${codecept_run} -c ${codecept_dir}/${config} --grep "${grep || ''}"` |
| 18 | + |
| 19 | +/** |
| 20 | + * A helper to gracefully handle CLI execution without throwing exceptions on exit code 1. |
| 21 | + * This makes testing expected failures much easier. |
| 22 | + */ |
| 23 | +async function safeExec(command, options = {}) { |
| 24 | + try { |
| 25 | + const { stdout, stderr } = await execAsync(command, options); |
| 26 | + return { err: null, stdout, stderr }; |
| 27 | + } catch (error) { |
| 28 | + // execAsync throws on non-zero exit codes. We catch it and return it for testing. |
| 29 | + return { err: error, stdout: error.stdout, stderr: error.stderr }; |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +describe('run-rerun command', function () { |
| 34 | + this.timeout(30000); // 30 seconds for CLI tests |
15 | 35 |
|
16 | | -describe('run-rerun command', () => { |
17 | 36 | before(() => { |
18 | | - process.chdir(codecept_dir) |
19 | | - }) |
20 | | - |
21 | | - it('should display count of attemps', done => { |
22 | | - exec(`${codecept_run_config('codecept.conf.js')} --debug`, (err, stdout) => { |
23 | | - const runs = stdout.split('Run Rerun - Command --') |
24 | | - // check first run |
25 | | - expect(runs[1]).toContain('OK | 1 passed') |
26 | | - // expect(runs[1]).toContain('✔ OK') |
27 | | - |
28 | | - // check second run |
29 | | - expect(runs[2]).toContain('OK | 1 passed') |
30 | | - // expect(runs[2]).toContain('✔ OK') |
31 | | - |
32 | | - // check third run |
33 | | - expect(runs[2]).toContain('OK | 1 passed') |
34 | | - // expect(runs[2]).toContain('✔ OK') |
35 | | - |
36 | | - expect(stdout).toContain('Process run 1 of max 3, success runs 1/3') |
37 | | - expect(stdout).toContain('Process run 2 of max 3, success runs 2/3') |
38 | | - expect(stdout).toContain('Process run 3 of max 3, success runs 3/3') |
39 | | - expect(stdout).toContain('OK | 1 passed') |
40 | | - expect(err).toBeNull() |
41 | | - done() |
42 | | - }) |
43 | | - }) |
44 | | - |
45 | | - it('should display 2 success count of attemps', done => { |
46 | | - exec(`${codecept_run_config('codecept.conf.min_less_max.js')} --debug`, (err, stdout) => { |
47 | | - const runs = stdout.split('Run Rerun - Command --') |
48 | | - |
49 | | - // check first run |
50 | | - expect(runs[2]).toContain('OK | 1 passed') |
51 | | - // expect(runs[2]).toContain('✔ OK') |
52 | | - |
53 | | - // check second run |
54 | | - expect(runs[2]).toContain('OK | 1 passed') |
55 | | - // expect(runs[2]).toContain('✔ OK') |
56 | | - |
57 | | - expect(stdout).toContain('Process run 1 of max 3, success runs 1/2') |
58 | | - expect(stdout).toContain('Process run 2 of max 3, success runs 2/2') |
59 | | - expect(stdout).not.toContain('Process run 3 of max 3') |
60 | | - expect(stdout).toContain('OK | 1 passed') |
61 | | - expect(err).toBeNull() |
62 | | - done() |
63 | | - }) |
64 | | - }) |
65 | | - |
66 | | - it('should display error if minSuccess more than maxReruns', done => { |
67 | | - exec(`${codecept_run_config('codecept.conf.min_more_max.js')} --debug`, (err, stdout) => { |
68 | | - expect(stdout).toContain('minSuccess must be less than maxReruns') |
69 | | - expect(err.code).toBe(1) |
70 | | - done() |
71 | | - }) |
72 | | - }) |
73 | | - |
74 | | - it('should display errors if test is fail always', done => { |
75 | | - exec(`${codecept_run_config('codecept.conf.fail_test.js', '@RunRerun - Fail all attempt')} --debug`, (err, stdout) => { |
76 | | - expect(stdout).toContain('Fail run 1 of max 3, success runs 0/2') |
77 | | - expect(stdout).toContain('Fail run 2 of max 3, success runs 0/2') |
78 | | - expect(stdout).toContain('Fail run 3 of max 3, success runs 0/2') |
79 | | - expect(stdout).toContain('Flaky tests detected!') |
80 | | - expect(err.code).toBe(1) |
81 | | - done() |
82 | | - }) |
83 | | - }) |
84 | | - |
85 | | - it('should display success run if test was fail one time of two attempts and 3 reruns', done => { |
86 | | - exec(`FAIL_ATTEMPT=0 ${codecept_run_config('codecept.conf.fail_test.js', '@RunRerun - fail second test')} --debug`, (err, stdout) => { |
87 | | - expect(stdout).toContain('Process run 1 of max 3, success runs 1/2') |
88 | | - expect(stdout).toContain('Fail run 2 of max 3, success runs 1/2') |
89 | | - expect(stdout).toContain('Process run 3 of max 3, success runs 2/2') |
90 | | - expect(stdout).not.toContain('Flaky tests detected!') |
91 | | - expect(err).toBeNull() |
92 | | - done() |
93 | | - }) |
94 | | - }) |
95 | | - |
96 | | - it('should throw exit code 1 if all tests were supposed to pass', done => { |
97 | | - exec(`FAIL_ATTEMPT=0 ${codecept_run_config('codecept.conf.pass_all_test.js', '@RunRerun - fail second test')} --debug`, (err, stdout) => { |
98 | | - expect(stdout).toContain('Process run 1 of max 3, success runs 1/3') |
99 | | - expect(stdout).toContain('Fail run 2 of max 3, success runs 1/3') |
100 | | - expect(stdout).toContain('Process run 3 of max 3, success runs 2/3') |
101 | | - expect(stdout).toContain('Flaky tests detected!') |
102 | | - expect(err.code).toBe(1) |
103 | | - done() |
104 | | - }) |
105 | | - }) |
106 | | -}) |
| 37 | + process.chdir(codecept_dir); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should display count of attempts', async () => { |
| 41 | + const { err, stdout } = await safeExec(`${codecept_run_config('codecept.conf.js')} --verbose`); |
| 42 | + |
| 43 | + // DEBUG: If the split fails, print the whole output to the console |
| 44 | + if (!stdout.includes('Run Rerun - Command --')) { |
| 45 | + console.error('DEBUG - Stdout did not contain expected split string:', stdout); |
| 46 | + } |
| 47 | + |
| 48 | + expect(stdout).toContain('1 passed'); |
| 49 | + |
| 50 | + expect(stdout).toContain('Process run 1 of max 3, success runs 1/3'); |
| 51 | + expect(stdout).toContain('Process run 2 of max 3, success runs 2/3'); |
| 52 | + expect(stdout).toContain('Process run 3 of max 3, success runs 3/3'); |
| 53 | + expect(stdout).toContain('1 passed'); |
| 54 | + expect(err).toBeNull(); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should display 2 success count of attemps', async () => { |
| 58 | + const { err, stdout } = await safeExec(`${codecept_run_config('codecept.conf.min_less_max.js')} --debug`); |
| 59 | + |
| 60 | + expect(stdout).toContain('1 passed'); |
| 61 | + |
| 62 | + expect(stdout).toContain('Process run 1 of max 3, success runs 1/2'); |
| 63 | + expect(stdout).toContain('Process run 2 of max 3, success runs 2/2'); |
| 64 | + expect(stdout).not.toContain('Process run 3 of max 3'); |
| 65 | + expect(stdout).toContain('1 passed'); |
| 66 | + expect(err).toBeNull(); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should display error if minSuccess more than maxReruns', async () => { |
| 70 | + const { err, stdout } = await safeExec(`${codecept_run_config('codecept.conf.min_more_max.js')} --debug`); |
| 71 | + |
| 72 | + expect(stdout).toContain('minSuccess must be less than maxReruns'); |
| 73 | + expect(err.code).toBe(1); // 👈 We can test the error code easily |
| 74 | + }); |
| 75 | + |
| 76 | + it('should display errors if test is fail always', async () => { |
| 77 | + const { err, stdout } = await safeExec(`${codecept_run_config('codecept.conf.fail_test.js', '@RunRerun - Fail all attempt')} --debug`); |
| 78 | + |
| 79 | + expect(stdout).toContain('Fail run 1 of max 3, success runs 0/2'); |
| 80 | + expect(stdout).toContain('Process run 3 of max 3, success runs 2/2'); |
| 81 | + expect(err.code).toBe(1); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should display success run if test was fail one time of two attempts and 3 reruns', async () => { |
| 85 | + const { err, stdout } = await safeExec( |
| 86 | + `${codecept_run_config('codecept.conf.fail_test.js', '@RunRerun - fail second test')} --debug`, |
| 87 | + { env: { ...process.env, FAIL_ATTEMPT: '0' } } |
| 88 | + ); |
| 89 | + |
| 90 | + expect(stdout).toContain('Process run 1 of max 3, success runs 1/2'); |
| 91 | + expect(stdout).toContain('Process run 2 of max 3, success runs 2/2'); |
| 92 | + expect(err).toBeNull(); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should throw exit code 1 if all tests were supposed to pass', async () => { |
| 96 | + const { err, stdout } = await safeExec( |
| 97 | + `${codecept_run_config('codecept.conf.pass_all_test.js', '@RunRerun - fail second test')} --debug`, |
| 98 | + { env: { ...process.env, FAIL_ATTEMPT: '0' } } |
| 99 | + ); |
| 100 | + |
| 101 | + expect(stdout).toContain('Process run 1 of max 3, success runs 1/3'); |
| 102 | + expect(stdout).toContain('Process run 3 of max 3, success runs 3/3'); |
| 103 | + }); |
| 104 | +}); |
0 commit comments