-
-
Notifications
You must be signed in to change notification settings - Fork 750
Expand file tree
/
Copy pathfactory.js
More file actions
138 lines (120 loc) · 4.53 KB
/
factory.js
File metadata and controls
138 lines (120 loc) · 4.53 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import Mocha from 'mocha'
import fsPath from 'path'
import fs from 'fs'
import { fileURLToPath } from 'url'
import reporter from './cli.js'
import gherkinParser, { loadTranslations } from './gherkin.js'
import output from '../output.js'
import scenarioUiFunction from './ui.js'
import { initMochaGlobals } from '../globals.js'
const __filename = fileURLToPath(import.meta.url)
const __dirname = fsPath.dirname(__filename)
let mocha
class MochaFactory {
static create(config, opts) {
mocha = new Mocha(Object.assign(config, opts))
output.process(opts.child)
mocha.ui(scenarioUiFunction)
// Manually trigger UI setup for globals to be available in ESM context
// This ensures Feature, Scenario, Before, etc. are available immediately
if (mocha.suite && mocha.suite.emit) {
const context = {}
mocha.suite.emit('pre-require', context, '', mocha)
// Also set globals immediately so they're available when ESM modules load
initMochaGlobals(context)
}
Mocha.Runner.prototype.uncaught = function (err) {
if (err) {
if (err.toString().indexOf('ECONNREFUSED') >= 0) {
// Handle ECONNREFUSED without dynamic import for now
err = new Error('Connection refused: ' + err.toString())
}
output.error(err)
output.print(err.stack)
process.exit(1)
}
output.error('Uncaught undefined exception')
process.exit(1)
}
// Override loadFiles to handle feature files
const originalLoadFiles = Mocha.prototype.loadFiles
mocha.loadFiles = function (fn) {
// load features
const featureFiles = this.files.filter(file => file.match(/\.feature$/))
if (featureFiles.length > 0) {
// Load translations for Gherkin features
loadTranslations().catch(() => {
// Ignore if translations can't be loaded
})
for (const file of featureFiles) {
const suite = gherkinParser(fs.readFileSync(file, 'utf8'), file)
this.suite.addSuite(suite)
}
// remove feature files
const jsFiles = this.files.filter(file => !file.match(/\.feature$/))
this.files = this.files.filter(file => !file.match(/\.feature$/))
// Load JavaScript test files using original loadFiles
if (jsFiles.length > 0) {
originalLoadFiles.call(this, fn)
}
// add ids for each test and check uniqueness
const dupes = []
let missingFeatureInFile = []
const seenTests = []
this.suite.eachTest(test => {
if (!test) {
return // Skip undefined tests
}
const name = test.fullTitle()
if (seenTests.includes(test.uid)) {
dupes.push(name)
}
seenTests.push(test.uid)
if (name.slice(0, name.indexOf(':')) === '') {
missingFeatureInFile.push(test.file)
}
})
if (dupes.length) {
// ideally this should be no-op and throw (breaking change)...
output.error(`Duplicate test names detected - Feature + Scenario name should be unique:\n${dupes.join('\n')}`)
}
if (missingFeatureInFile.length) {
missingFeatureInFile = [...new Set(missingFeatureInFile)]
output.error(`Missing Feature section in:\n${missingFeatureInFile.join('\n')}`)
}
} else {
// Use original for non-feature files
originalLoadFiles.call(this, fn)
}
}
const presetReporter = opts.reporter || config.reporter
// use standard reporter
if (!presetReporter) {
mocha.reporter(reporter, opts)
return mocha
}
// load custom reporter with options
const reporterOptions = Object.assign(config.reporterOptions || {})
if (opts.reporterOptions !== undefined) {
opts.reporterOptions.split(',').forEach(opt => {
const L = opt.split('=')
if (L.length > 2 || L.length === 0) {
throw new Error(`invalid reporter option '${opt}'`)
} else if (L.length === 2) {
reporterOptions[L[0]] = L[1]
} else {
reporterOptions[L[0]] = true
}
})
}
const attributes = Object.getOwnPropertyDescriptor(reporterOptions, 'codeceptjs-cli-reporter')
if (reporterOptions['codeceptjs-cli-reporter'] && attributes) {
Object.defineProperty(reporterOptions, 'codeceptjs/lib/mocha/cli', attributes)
delete reporterOptions['codeceptjs-cli-reporter']
}
// custom reporters
mocha.reporter(presetReporter, reporterOptions)
return mocha
}
}
export default MochaFactory