forked from redhat-developer/vscode-tekton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingle-test-run.ts
67 lines (53 loc) · 1.83 KB
/
single-test-run.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
/*-----------------------------------------------------------------------------------------------
* Copyright (c) Red Hat, Inc. All rights reserved.
* Licensed under the MIT License. See LICENSE file in the project root for license information.
*-----------------------------------------------------------------------------------------------*/
import * as path from 'path';
import Mocha = require('mocha');
// Linux: prevent a weird NPE when mocha on Linux requires the window size from the TTY
// Since we are not running in a tty environment, we just implement the method statically
// eslint-disable-next-line @typescript-eslint/no-var-requires
const tty = require('tty');
if (!tty.getWindowSize) {
tty.getWindowSize = (): number[] => {
return [80, 75];
};
}
// eslint-disable-next-line @typescript-eslint/ban-types
const config: {} = {
ui: 'tdd',
timeout: 15000,
color: true,
fullStackTrace: true,
};
const mocha = new Mocha(config);
// eslint-disable-next-line @typescript-eslint/ban-types
export function run(testsRoots: string, cb: (error: {}, failures?: number) => void): void {
const testsRoot = path.resolve(__dirname);
const testFile = process.env.VSCODE_SINGLE_TEST;
if (!testFile) {
cb('Cannot find tests');
return;
}
if (path.extname(testFile) !== '.ts') {
cb(`Cannot run test from: ${testFile}`);
return;
}
const nFileName = path.basename(testFile, path.extname(testFile)) + '.js';
const pathSegments = testFile.split(path.sep);
mocha.addFile(path.join(testsRoot, ...pathSegments.slice(1, -1), nFileName));
try {
mocha.run(failures => {
setTimeout(() => {
if (failures > 0) {
cb(new Error(`${failures} tests failed.`));
} else {
cb(null, failures);
}
}, 100);
});
} catch (err) {
console.error(err);
cb(err);
}
}