Skip to content

Commit 9edec72

Browse files
authored
Ensure test files are executed in alphabetical order (#5386)
* Ensure test files are executed in alphabetical order * add unit test
1 parent 7c7ed34 commit 9edec72

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

lib/codecept.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ class Codecept {
184184
}
185185
}
186186

187+
this.testFiles.sort()
188+
187189
if (this.opts.shuffle) {
188190
this.testFiles = this.shuffle(this.testFiles)
189191
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
const expect = require('chai').expect
2+
const Codecept = require('../../lib/codecept')
3+
const path = require('path')
4+
const fs = require('fs')
5+
6+
describe('Test Files Alphabetical Order', () => {
7+
let codecept
8+
const tempDir = path.join(__dirname, '../data/sandbox/configs/alphabetical_order')
9+
const config = {
10+
tests: `${tempDir}/*_test.js`,
11+
gherkin: { features: null },
12+
output: './output',
13+
hooks: [],
14+
}
15+
16+
before(() => {
17+
if (!fs.existsSync(tempDir)) {
18+
fs.mkdirSync(tempDir, { recursive: true })
19+
}
20+
21+
fs.writeFileSync(
22+
path.join(tempDir, 'zzz_test.js'),
23+
`
24+
Feature('Test');
25+
Scenario('zzz test', () => {});
26+
`,
27+
)
28+
fs.writeFileSync(
29+
path.join(tempDir, 'aaa_test.js'),
30+
`
31+
Feature('Test');
32+
Scenario('aaa test', () => {});
33+
`,
34+
)
35+
fs.writeFileSync(
36+
path.join(tempDir, 'mmm_test.js'),
37+
`
38+
Feature('Test');
39+
Scenario('mmm test', () => {});
40+
`,
41+
)
42+
})
43+
44+
after(() => {
45+
const files = ['zzz_test.js', 'aaa_test.js', 'mmm_test.js']
46+
files.forEach(file => {
47+
const filePath = path.join(tempDir, file)
48+
if (fs.existsSync(filePath)) {
49+
fs.unlinkSync(filePath)
50+
}
51+
})
52+
})
53+
54+
beforeEach(() => {
55+
codecept = new Codecept(config, {})
56+
codecept.init(path.join(__dirname, '../data/sandbox'))
57+
})
58+
59+
it('should sort test files alphabetically after loading', () => {
60+
codecept.loadTests()
61+
62+
if (codecept.testFiles.length === 0) {
63+
console.log('No test files found, skipping test')
64+
return
65+
}
66+
67+
const filenames = codecept.testFiles.map(filePath => path.basename(filePath))
68+
69+
const sortedFilenames = [...filenames].sort()
70+
71+
expect(filenames).to.deep.equal(sortedFilenames)
72+
73+
const aaaIndex = filenames.findIndex(f => f.includes('aaa_test.js'))
74+
const mmmIndex = filenames.findIndex(f => f.includes('mmm_test.js'))
75+
const zzzIndex = filenames.findIndex(f => f.includes('zzz_test.js'))
76+
77+
expect(aaaIndex).to.be.greaterThan(-1)
78+
expect(mmmIndex).to.be.greaterThan(-1)
79+
expect(zzzIndex).to.be.greaterThan(-1)
80+
81+
expect(aaaIndex).to.be.lessThan(mmmIndex, 'aaa_test.js should come before mmm_test.js')
82+
expect(mmmIndex).to.be.lessThan(zzzIndex, 'mmm_test.js should come before zzz_test.js')
83+
})
84+
})

0 commit comments

Comments
 (0)