-
Notifications
You must be signed in to change notification settings - Fork 28
/
index.ts
82 lines (72 loc) · 1.93 KB
/
index.ts
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
/* eslint-disable sonarjs/cognitive-complexity */
import { join, resolve as resolvePath } from 'path'
import Mocha from 'mocha'
import glob from 'glob'
import chai from 'chai'
import chaiAsPromised from 'chai-as-promised'
import sinonChai from 'sinon-chai'
import { Logger } from '../../common/logger'
function setupNyc() {
const NYC = require('nyc')
const defaultExclude = require('@istanbuljs/schema/default-exclude')
const cwd = join(__dirname, '..', '..', '..')
const nyc = new NYC({
cache: true,
cacheDir: join(cwd, '.cache', 'nyc'),
cwd,
exclude: [...defaultExclude, '**/test/**', '**/.vscode-test/**'],
extensions: ['ts'],
hookRequire: true,
hookRunInContext: true,
hookRunInThisContext: true,
instrument: true,
reporter: ['text', 'html'],
sourceMap: true,
tempDirectory: join(cwd, 'coverage', 'integration')
})
nyc.reset()
nyc.wrap()
return nyc
}
export async function run() {
const nyc = setupNyc()
const mocha = new Mocha({
checkLeaks: true,
color: true,
reporterOptions: { maxDiffSize: 0 },
timeout: 4000,
ui: 'tdd'
})
chai.use(sinonChai)
chai.use(chaiAsPromised)
const testsRoot = resolvePath(__dirname, '..')
try {
await new Promise<void>((resolve, reject) => {
glob('**/**.test.js', { cwd: testsRoot }, (err, files) => {
if (err) {
return reject(err)
}
// Add files to the test suite
files.forEach(f => mocha.addFile(resolvePath(testsRoot, f)))
try {
// Run the mocha test
mocha.run(failures => {
if (failures > 0) {
reject(new Error(`${failures} tests failed.`))
} else {
resolve()
}
})
} catch (e: unknown) {
Logger.error((e as Error).toString())
throw e
}
})
})
} finally {
if (nyc) {
nyc.writeCoverageFile()
nyc.report()
}
}
}