forked from bazel-contrib/rules_nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjasmine_runner.js
114 lines (97 loc) · 3.91 KB
/
jasmine_runner.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
const fs = require('fs');
const path = require('path');
let jasmineCore = null
let JasmineRunner = require('jasmine/lib/jasmine');
const Report = require('c8/lib/report');
if (global.jasmine) {
// global.jasmine has been initialized which means a bootstrap script
// has already required `jasmine-core` and called jasmineCore.boot()
jasmineCore = global.jasmineCore || require('jasmine-core');
// Override the jasmineCore boot function so that the jasmine
// runner gets the already initialize jasmine and its shared environment
jasmineCore.boot = function() {
return global.jasmine
};
}
const UTF8 = {
encoding: 'utf-8'
};
// These exit codes are handled specially by Bazel:
// https://github.com/bazelbuild/bazel/blob/486206012a664ecb20bdb196a681efc9a9825049/src/main/java/com/google/devtools/build/lib/util/ExitCode.java#L44
const BAZEL_EXIT_TESTS_FAILED = 3;
const BAZEL_EXIT_NO_TESTS_FOUND = 4;
// Set the StackTraceLimit to infinity. This will make stack capturing slower, but more useful.
// Since we are running tests having proper stack traces is very useful and should be always set to
// the maximum (See: https://nodejs.org/api/errors.html#errors_error_stacktracelimit)
Error.stackTraceLimit = Infinity;
const IS_TEST_FILE = /[^a-zA-Z0-9](spec|test)\.js$/i;
const IS_MODULE_MODULE = /\/node_modules\//
function main(args) {
if (!args.length) {
throw new Error('Spec file manifest expected argument missing');
}
const manifest = require.resolve(args[0]);
// Remove the manifest, some tested code may process the argv.
process.argv.splice(2, 1)[0];
const jrunner = new JasmineRunner({jasmineCore: jasmineCore});
const allFiles = fs.readFileSync(manifest, UTF8)
.split('\n')
.filter(l => l.length > 0)
// Filter out files from node_modules that match test.js or spec.js
.filter(f => !IS_MODULE_MODULE.test(f))
// the relative directory the coverage reporter uses to find the files
const cwd = process.cwd()
const sourceFiles = allFiles
// Filter out all .spec and .test files so we only report
// coverage against the source files
.filter(f => !IS_TEST_FILE.test(f))
.map(f => require.resolve(f))
// the reporting lib resolves the relative path instead of using the absolute one
// so match it here
.map(f => path.relative(cwd, f))
allFiles
// Filter here so that only files ending in `spec.js` and `test.js`
// are added to jasmine as spec files. This is important as other
// deps such as "@npm//typescript" if executed may cause the test to
// fail or have unexpected side-effects. "@npm//typescript" would
// try to execute tsc, print its help, and process.exit(1)
.filter(f => IS_TEST_FILE.test(f))
.forEach(f => jrunner.addSpecFile(f));
var noSpecsFound = true;
jrunner.addReporter({
specDone: () => {
noSpecsFound = false
},
});
// addReporter throws away the default console reporter
// so we need to add it back
jrunner.configureDefaultReporter({});
jrunner.onComplete((passed) => {
let exitCode = passed ? 0 : BAZEL_EXIT_TESTS_FAILED;
if (noSpecsFound) exitCode = BAZEL_EXIT_NO_TESTS_FOUND;
console.log(process.env.NODE_V8_COVERAGE)
console.log('covfiles', fs.readdirSync(process.env.NODE_V8_COVERAGE));
if (process.env.NODE_V8_COVERAGE) {
const report = Report({
include: sourceFiles,
exclude: [],
// only output a text-summary
// we can output other formats when bazel coverage can pick them up
reporter: ['text-summary'],
tempDirectory: process.env.NODE_V8_COVERAGE,
watermarks: undefined,
resolve: cwd,
omitRelative: undefined,
wrapperLength: undefined
});
report.run();
}
process.exit(exitCode);
});
jrunner.execute();
return 0;
}
if (require.main === module) {
console.log(process.env.NODE_V8_COVERAGE)
process.exitCode = main(process.argv.slice(2));
}