-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtest-execution-service.ts
81 lines (76 loc) · 2.91 KB
/
test-execution-service.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
declare var global: any;
export class TestExecutionService implements ITestExecutionService {
public runTests(scripts: IScriptInfo[]): ITestExecutionError[] {
const errors = [];
const testScripts = scripts.filter(script => script.type === ScriptTypes.FrameworkAdapterType || script.type === ScriptTypes.TestType);
testScripts
.forEach(script => {
try {
this.runTest(script);
} catch (err) {
errors.push({
msg: err.toString(),
url: script.localPath || script.url,
line: err.lineNumber || 0
});
}
});
return errors;
}
private runTest(script: IScriptInfo): void {
if (script.localPath) {
console.log('NSUTR: require script ' + script.url + ' from ' + script.localPath);
// Add this check in order to prevent the following warning from webpack compiler:
// WARNING in ../node_modules/nativescript-unit-test-runner/main-view-model.js 204:28-53
// Critical dependency: the request of a dependency is an expression
if (!global.TNS_WEBPACK) {
require(script.localPath);
}
} else {
if (script.shouldEval) {
console.log('NSUTR: eval script ' + script.url);
this.loadShim(script.url);
//call eval indirectly to execute the scripts in the global scope
var geval = eval;
geval(script.contents);
this.completeLoading(script.url);
} else {
console.log('NSUTR: ignoring evaluation of script ' + script.url);
}
}
}
private loadShim(url: string) {
if (url.indexOf('mocha') !== -1) {
global.window = global;
global.location = { href: '/' };
global.document = {
getElementById: id => null
}
} else if (url.indexOf('chai') !== -1) {
global.__shim_require = global.require;
global.require = function () {
throw Error();
}
global.window = global;
} else if (url.indexOf('qunit.js') !== -1) {
global.define = function (factory) {
global.QUnit = factory();
}
global.define.amd = true;
}
}
private completeLoading(url: string) {
if (url.indexOf('mocha') !== -1) {
delete global.window;
// delete global.location;
delete global.document;
}
if (url.indexOf('chai') !== -1) {
delete global.window;
global.require = global.__shim_require;
delete global.__shim_require;
} else if (url.indexOf('qunit.js') !== -1) {
delete global.define;
}
}
}