forked from codeceptjs/CodeceptJS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalphabetical_order_test.js
More file actions
102 lines (87 loc) · 2.9 KB
/
alphabetical_order_test.js
File metadata and controls
102 lines (87 loc) · 2.9 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
const expect = require('chai').expect
const Codecept = require('../../lib/codecept')
const Container = require('../../lib/container')
const path = require('path')
const fs = require('fs')
describe('Test Files Alphabetical Order', () => {
let codecept
const tempDir = path.join(__dirname, '../data/sandbox/configs/alphabetical_order')
const config = {
tests: `${tempDir}/*_test.js`,
gherkin: { features: null },
output: './output',
hooks: [],
}
before(() => {
if (!fs.existsSync(tempDir)) {
fs.mkdirSync(tempDir, { recursive: true })
}
fs.writeFileSync(
path.join(tempDir, 'zzz_test.js'),
`
Feature('Test');
Scenario('zzz test', () => {});
`,
)
fs.writeFileSync(
path.join(tempDir, 'aaa_test.js'),
`
Feature('Test');
Scenario('aaa test', () => {});
`,
)
fs.writeFileSync(
path.join(tempDir, 'mmm_test.js'),
`
Feature('Test');
Scenario('mmm test', () => {});
`,
)
})
after(() => {
const files = ['zzz_test.js', 'aaa_test.js', 'mmm_test.js']
files.forEach(file => {
const filePath = path.join(tempDir, file)
if (fs.existsSync(filePath)) {
fs.unlinkSync(filePath)
}
})
})
beforeEach(() => {
codecept = new Codecept(config, {})
codecept.init(path.join(__dirname, '../data/sandbox'))
})
afterEach(() => {
Container.clear()
})
it('should sort test files alphabetically when run() is called', async () => {
codecept.loadTests()
if (codecept.testFiles.length === 0) {
console.log('No test files found, skipping test')
return
}
// Capture the files that would be passed to mocha during run()
let filesPassedToMocha = null
const mocha = Container.mocha()
mocha.run = callback => {
filesPassedToMocha = [...mocha.files]
// Call callback immediately to complete the run
if (callback) callback()
}
// Call run() which should sort files before passing to mocha
await codecept.run()
// Verify files passed to mocha are sorted alphabetically
expect(filesPassedToMocha).to.not.be.null
const filenames = filesPassedToMocha.map(filePath => path.basename(filePath))
const sortedFilenames = [...filenames].sort()
expect(filenames).to.deep.equal(sortedFilenames, 'Files should be sorted alphabetically for execution')
const aaaIndex = filenames.findIndex(f => f.includes('aaa_test.js'))
const mmmIndex = filenames.findIndex(f => f.includes('mmm_test.js'))
const zzzIndex = filenames.findIndex(f => f.includes('zzz_test.js'))
expect(aaaIndex).to.be.greaterThan(-1)
expect(mmmIndex).to.be.greaterThan(-1)
expect(zzzIndex).to.be.greaterThan(-1)
expect(aaaIndex).to.be.lessThan(mmmIndex, 'aaa_test.js should come before mmm_test.js')
expect(mmmIndex).to.be.lessThan(zzzIndex, 'mmm_test.js should come before zzz_test.js')
})
})