/
fixturesUI.js
149 lines (135 loc) · 4.66 KB
/
fixturesUI.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const { FixturePool, registerFixture, registerWorkerFixture, rerunRegistrations } = require('./fixtures');
const { Test, Suite } = require('mocha');
const { installTransform } = require('./transform');
const commonSuite = require('mocha/lib/interfaces/common');
Error.stackTraceLimit = 15;
global.testOptions = require('./testOptions');
global.registerFixture = registerFixture;
global.registerWorkerFixture = registerWorkerFixture;
process.env.JEST_WORKER_ID = 1;
const fixturePool = new FixturePool();
let revertBabelRequire;
function specBuilder(modifiers, specCallback) {
function builder(specs, last) {
const callable = (...args) => {
if (!last || (typeof args[0] === 'string' && typeof args[1] === 'function')) {
// Looks like a body (either it or describe). Assume that last modifier is true.
const newSpecs = { ...specs };
if (last)
newSpecs[last] = [true];
return specCallback(newSpecs, ...args);
}
const newSpecs = { ...specs };
newSpecs[last] = args;
return builder(newSpecs, null);
};
return new Proxy(callable, {
get: (obj, prop) => {
if (typeof prop === 'string' && modifiers.includes(prop)) {
const newSpecs = { ...specs };
// Modifier was not called, assume true.
if (last)
newSpecs[last] = [true];
return builder(newSpecs, prop);
}
return obj[prop];
},
});
}
return builder({}, null);
}
function fixturesUI(testRunner, suite) {
const suites = [suite];
suite.on(Suite.constants.EVENT_FILE_PRE_REQUIRE, function(context, file, mocha) {
const common = commonSuite(suites, context, mocha);
const it = specBuilder(['skip', 'fail', 'slow', 'only'], (specs, title, fn) => {
const suite = suites[0];
if (suite.isPending())
fn = null;
let wrapper;
if (testRunner.trialRun) {
if (fn)
wrapper = () => {};
} else {
const wrapped = fixturePool.wrapTestCallback(fn);
wrapper = wrapped ? (done, ...args) => {
wrapped(...args).then(done).catch(done);
} : undefined;
}
if (wrapper) {
wrapper.toString = () => fn.toString();
wrapper.__original = fn;
}
const test = new Test(title, wrapper);
test.file = file;
suite.addTest(test);
const only = specs.only && specs.only[0];
if (specs.slow && specs.slow[0])
test.timeout(90000);
if (only)
test.markOnly();
if (!only && specs.skip && specs.skip[0])
test.pending = true;
if (!only && specs.fail && specs.fail[0])
test.pending = true;
return test;
});
const describe = specBuilder(['skip', 'fail', 'only'], (specs, title, fn) => {
const suite = common.suite.create({
title: title,
file: file,
fn: fn
});
const only = specs.only && specs.only[0];
if (only)
suite.markOnly();
if (!only && specs.skip && specs.skip[0])
suite.pending = true;
if (!only && specs.fail && specs.fail[0])
suite.pending = true;
return suite;
});
context.beforeEach = (fn) => {
if (testRunner.trialRun)
return;
return common.beforeEach(async () => {
return await fixturePool.resolveParametersAndRun(fn);
});
};
context.afterEach = (fn) => {
if (testRunner.trialRun)
return;
return common.afterEach(async () => {
return await fixturePool.resolveParametersAndRun(fn);
});
};
context.run = mocha.options.delay && common.runWithSuite(suite);
context.describe = describe;
context.fdescribe = describe.only(true);
context.xdescribe = describe.skip(true);
context.it = it;
context.fit = it.only(true);
context.xit = it.skip(true);
revertBabelRequire = installTransform();
});
suite.on(Suite.constants.EVENT_FILE_POST_REQUIRE, function(context, file, mocha) {
revertBabelRequire();
rerunRegistrations(file, 'test');
});
};
module.exports = { fixturesUI, fixturePool, registerFixture, registerWorkerFixture };