forked from cpojer/best-test-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.js
45 lines (44 loc) · 1.3 KB
/
worker.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
const fs = require('fs');
const expect = require('expect');
const mock = require('jest-mock');
const {describe, it, run, resetState} = require('jest-circus');
const vm = require('vm');
const NodeEnvironment = require('jest-environment-node').default;
const {dirname, basename, join} = require('path');
exports.runTest = async function (testFile) {
const testResult = {
success: false,
errorMessage: null,
};
try {
resetState();
let environment;
const customRequire = (fileName) => {
const code = fs.readFileSync(join(dirname(testFile), fileName), 'utf8');
const moduleFactory = vm.runInContext(
`(function(module, require) {${code}})`,
environment.getVmContext(),
);
const module = {exports: {}};
moduleFactory(module, customRequire);
return module.exports;
};
environment = new NodeEnvironment({
projectConfig: {
testEnvironmentOptions: {
describe,
it,
expect,
mock,
},
},
});
customRequire(basename(testFile));
const {testResults} = await run();
testResult.testResults = testResults;
testResult.success = testResults.every((result) => !result.errors.length);
} catch (error) {
testResult.errorMessage = error.message;
}
return testResult;
};