-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.js
206 lines (190 loc) · 5.26 KB
/
lib.js
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
const Mocha = require('mocha')
const path = require('path')
const fs = require('fs')
/**
* does the file has the extension of JS files (*.js)
* @param {string} filePath path to a file
* @returns {boolean} true if the file has .js extension, false otherwise
* @example
* isJSFile('./index.js')
* // true
* isJSFile('./package.json')
* // false
*/
const isJSFile = (filePath) => filePath.substr(-3) === '.js'
/**
* List all the files in a given directory, by default in recursive mode
* @param {string} dir path to a dir
* @param {boolean} [recursive=true] search in nested directories
* @returns {string[]} array of files found in the given directory
* @example
* listFiles('./')
* // ['main.js', 'package.json', 'README.md', 'test/main.test.js']
* listFiles('./', false)
* // ['main.js', 'package.json', 'README.md']
*/
const listFiles = (dir, recursive = true) => {
let JSFiles = []
const dirContents = fs.readdirSync(dir)
for (const item of dirContents) {
const itemPath = path.join(dir, item)
const itemStat = fs.statSync(itemPath)
if (itemStat.isFile()) JSFiles.push(itemPath)
else if (itemStat.isDirectory() && recursive) JSFiles = JSFiles.concat(listFiles(itemPath))
}
return JSFiles
}
/**
* Patch the runTest method of the Mocha Test Runner to pass all tests
* @example
* patchTestRunner()
*/
const patchTestRunner = () => {
Mocha.Runner.prototype.runTest = (callback) => {
callback()
}
}
/**
* Patch beforeEach() to do nothing
* @example
* patchBeforeEach()
*/
const patchBeforeEach = () => {
Mocha.Suite.prototype.beforeEach = () => {
return this
}
}
/**
* Patch afterEach() to do nothing
* @example
* patchAfterEach()
*/
const patchAfterEach = () => {
Mocha.Suite.prototype.afterEach = () => {
return this
}
}
/**
* Patch before() to do nothing
* @example
* patchBeforeAll()
*/
const patchBeforeAll = () => {
Mocha.Suite.prototype.beforeAll = () => {
return this
}
}
/**
* Patch after() to do nothing
* @example
* patchAfterAll()
*/
const patchAfterAll = () => {
Mocha.Suite.prototype.afterAll = () => {
return this
}
}
/**
* This function is ported from Mocha run helpers
*
* Exits Mocha when Mocha itself has finished execution, regardless of
* what the tests or code under test is doing.
* @param {number} code - Exit code; typically # of failures
* @private
*/
const exitMocha = code => {
const clampedCode = Math.min(code, 255)
let draining = 0
// Eagerly set the process's exit code in case stream.write doesn't
// execute its callback before the process terminates.
process.exitCode = clampedCode
// flush output for Node.js Windows pipe bug
const done = () => {
if (!draining--) {
process.exit(clampedCode)
}
}
const streams = [process.stdout, process.stderr]
streams.forEach(stream => {
// submit empty write request and wait for completion
draining += 1
stream.write('', done)
})
done()
}
/**
* Run mocha programatically on given test files
* @param {Array} [files=[]] array of test files
* @param {Object} [mochaOptions={}] mocha options, refer to {@link https://mochajs.org/api/mocha|mocha's api documentation}
* @returns {Mocha.Runner} mocha {@link https://mochajs.org/api/runner|runner} instance that ran all the tests
* @see {@link https://mochajs.org/api/|Mocha API documentation}
*
* @example <caption>To run the tests in ./test/main.test.js with default mocha options</caption>
* runMocha(['./test/main.test.js'])
* //
* // test suite
* // ✓ test case
* //
* // 1 passing (7ms)
*
* @example <caption>To run the tests in ./test/main.test.js with the nyan mocha reporter</caption>
* runMocha(['./test/main.test.js'], { reporter: 'nyan' })
* //
* // 1 -__,------,
* // 0 -__| /\_/\
* // 0 -_~|_( ^ .^)
* // -_ "" ""
* //
* // 1 passing (11ms)
*/
const runMocha = (files = [], mochaOptions = {}) => {
const mocha = new Mocha(mochaOptions)
files.forEach((testFile) => {
mocha.addFile(testFile)
})
patchBeforeEach()
patchAfterEach()
patchBeforeAll()
patchAfterAll()
patchTestRunner()
return mocha.run(exitMocha)
}
/**
* List all tests that were executed by a mocha test runner
* @see {@link https://mochajs.org/api/runner|Mocha Runner}
* @param {Mocha.Runner} runner {@link https://mochajs.org/api/runner|Mocha Runner instance}
* @returns {string[]} array of the full test names that were executed (Root suite name + child suite names + test name)
* @example
* listExecutedTests(runMocha(['./test/main.test.js'], { reporter: 'base' }))
* // [ 'test suite test case' ]
*/
const listExecutedTests = (runner) => {
const listTests = (suite) => {
let tests = []
if (suite === null || suite === undefined) return tests
if (Array.isArray(suite.tests)) suite.tests.forEach(t => tests.push(suite.fullTitle() + ' ' + t.title))
if (Array.isArray(suite.suites)) suite.suites.forEach(s => { tests = tests.concat(listTests(s)) })
return tests
}
return listTests(runner.suite)
}
/**
* Start mocha by requiring the _mocha script
* @example
* startMocha()
*/
const startMocha = () => {
require('mocha/bin/_mocha')
}
module.exports = {
isJSFile,
listFiles,
listExecutedTests,
patchTestRunner,
patchBeforeEach,
patchBeforeAll,
patchAfterEach,
patchAfterAll,
runMocha,
startMocha
}