-
Notifications
You must be signed in to change notification settings - Fork 673
/
execution-context.js
106 lines (82 loc) · 3.14 KB
/
execution-context.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
import { createContext } from 'vm';
import Module from 'module';
import path from 'path';
import exportableLib from '../exportable-lib';
import NODE_MODULES from '../../utils/node-modules-folder-name';
const OPTIONS_KEY = Symbol('options');
export const DEFAULT_CONTEXT_OPTIONS = {
skipVisibilityCheck: false,
collectionMode: false,
};
function getModuleBasePaths (currentPath) {
const nodePaths = [];
let parentDir = path.dirname(currentPath);
while (currentPath !== parentDir) {
currentPath = parentDir;
parentDir = path.dirname(currentPath);
nodePaths.push(path.join(currentPath, NODE_MODULES));
}
return nodePaths;
}
function createRequire (filename) {
//Deprecated since: Node v12.2.0
if (Module.createRequireFromPath)
return Module.createRequireFromPath(filename);
if (Module.createRequire)
return Module.createRequire(filename);
const dummyModule = new Module(filename, module);
const localModulesPaths = getModuleBasePaths(filename);
dummyModule.filename = filename;
dummyModule.paths = localModulesPaths.concat(module.paths);
return id => dummyModule.require(id);
}
function createSelectorDefinition (testRun) {
return (fn, options = {}) => {
const { skipVisibilityCheck, collectionMode } = testRun.controller ?
testRun.controller.getExecutionContext()[OPTIONS_KEY] :
createExecutionContext(testRun)[OPTIONS_KEY];
if (skipVisibilityCheck)
options.visibilityCheck = false;
if (testRun && testRun.id)
options.boundTestRun = testRun;
if (collectionMode)
options.collectionMode = collectionMode;
return exportableLib.Selector(fn, options);
};
}
function createClientFunctionDefinition (testRun) {
return (fn, options = {}) => {
if (testRun && testRun.id)
options.boundTestRun = testRun;
return exportableLib.ClientFunction(fn, options);
};
}
export function setContextOptions (context, options) {
context[OPTIONS_KEY] = options;
}
export function createExecutionContext (testRun) {
const filename = testRun.test.testFile.filename;
const replacers = {
require: createRequire(filename),
__filename: filename,
__dirname: path.dirname(filename),
t: testRun.controller,
Selector: createSelectorDefinition(testRun),
ClientFunction: createClientFunctionDefinition(testRun),
Role: exportableLib.Role,
RequestLogger: exportableLib.RequestLogger,
RequestMock: exportableLib.RequestMock,
RequestHook: exportableLib.RequestHook,
[OPTIONS_KEY]: DEFAULT_CONTEXT_OPTIONS,
userVariables: exportableLib.userVariables,
};
return createContext(new Proxy(replacers, {
get: (target, property) => {
if (replacers.hasOwnProperty(property))
return replacers[property];
if (global.hasOwnProperty(property))
return global[property];
throw new Error(`${property} is not defined`);
},
}));
}