Skip to content

Commit b7636e4

Browse files
authored
Refactor for tree processor (jestjs#6177)
1 parent 19c5278 commit b7636e4

File tree

4 files changed

+95
-72
lines changed

4 files changed

+95
-72
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@
155155

156156
### Chore & Maintenance
157157

158+
* `[jest-jasmine2]` Simplify `Env.execute` and TreeProcessor to setup and clean
159+
resources for the top suite the same way as for all of the children suites
160+
([#5885](https://github.com/facebook/jest/pull/5885))
158161
* `[babel-jest]` [**BREAKING**] Always return object from transformer
159162
([#5991](https://github.com/facebook/jest/pull/5991))
160163
* `[*]` Run Prettier on compiled output

integration-tests/__tests__/__snapshots__/failures.test.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ exports[`not throwing Error objects 5`] = `
247247
37 | });
248248
38 |
249249
250-
at packages/jest-jasmine2/build/jasmine/Env.js:518:34
250+
at packages/jest-jasmine2/build/jasmine/Env.js:541:34
251251
at __tests__/during_tests.test.js:36:3
252252
253253
"

packages/jest-jasmine2/src/jasmine/Env.js

Lines changed: 63 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -177,84 +177,102 @@ export default function(j$) {
177177
return j$.testPath;
178178
},
179179
});
180-
defaultResourcesForRunnable(topSuite.id);
180+
181181
currentDeclarationSuite = topSuite;
182182

183183
this.topSuite = function() {
184184
return topSuite;
185185
};
186186

187-
this.execute = async function(runnablesToRun) {
188-
if (!runnablesToRun) {
189-
if (focusedRunnables.length) {
190-
runnablesToRun = focusedRunnables;
191-
} else {
192-
runnablesToRun = [topSuite.id];
193-
}
187+
const uncaught = err => {
188+
if (currentSpec) {
189+
currentSpec.onException(err);
190+
currentSpec.cancel();
191+
} else {
192+
console.error('Unhandled error');
193+
console.error(err.stack);
194194
}
195+
};
195196

196-
const uncaught = err => {
197-
if (currentSpec) {
198-
currentSpec.onException(err);
199-
currentSpec.cancel();
200-
} else {
201-
console.error('Unhandled error');
202-
console.error(err.stack);
203-
}
204-
};
205-
197+
let oldListenersException;
198+
let oldListenersRejection;
199+
const executionSetup = function() {
206200
// Need to ensure we are the only ones handling these exceptions.
207-
const oldListenersException = process
208-
.listeners('uncaughtException')
209-
.slice();
210-
const oldListenersRejection = process
211-
.listeners('unhandledRejection')
212-
.slice();
201+
oldListenersException = process.listeners('uncaughtException').slice();
202+
oldListenersRejection = process.listeners('unhandledRejection').slice();
213203

214204
j$.process.removeAllListeners('uncaughtException');
215205
j$.process.removeAllListeners('unhandledRejection');
216206

217207
j$.process.on('uncaughtException', uncaught);
218208
j$.process.on('unhandledRejection', uncaught);
209+
};
210+
211+
const executionTeardown = function() {
212+
j$.process.removeListener('uncaughtException', uncaught);
213+
j$.process.removeListener('unhandledRejection', uncaught);
214+
215+
// restore previous exception handlers
216+
oldListenersException.forEach(listener => {
217+
j$.process.on('uncaughtException', listener);
218+
});
219+
220+
oldListenersRejection.forEach(listener => {
221+
j$.process.on('unhandledRejection', listener);
222+
});
223+
};
224+
225+
this.execute = async function(runnablesToRun, suiteTree = topSuite) {
226+
if (!runnablesToRun) {
227+
if (focusedRunnables.length) {
228+
runnablesToRun = focusedRunnables;
229+
} else {
230+
runnablesToRun = [suiteTree.id];
231+
}
232+
}
219233

220-
reporter.jasmineStarted({totalSpecsDefined});
234+
if (currentlyExecutingSuites.length === 0) {
235+
executionSetup();
236+
}
221237

222-
currentlyExecutingSuites.push(topSuite);
238+
const lastDeclarationSuite = currentDeclarationSuite;
223239

224240
await treeProcessor({
225241
nodeComplete(suite) {
226242
if (!suite.disabled) {
227243
clearResourcesForRunnable(suite.id);
228244
}
229245
currentlyExecutingSuites.pop();
230-
reporter.suiteDone(suite.getResult());
246+
if (suite === topSuite) {
247+
reporter.jasmineDone({
248+
failedExpectations: topSuite.result.failedExpectations,
249+
});
250+
} else {
251+
reporter.suiteDone(suite.getResult());
252+
}
231253
},
232254
nodeStart(suite) {
233255
currentlyExecutingSuites.push(suite);
234-
defaultResourcesForRunnable(suite.id, suite.parentSuite.id);
235-
reporter.suiteStarted(suite.result);
256+
defaultResourcesForRunnable(
257+
suite.id,
258+
suite.parentSuite && suite.parentSuite.id,
259+
);
260+
if (suite === topSuite) {
261+
reporter.jasmineStarted({totalSpecsDefined});
262+
} else {
263+
reporter.suiteStarted(suite.result);
264+
}
236265
},
237266
queueRunnerFactory,
238267
runnableIds: runnablesToRun,
239-
tree: topSuite,
268+
tree: suiteTree,
240269
});
241-
clearResourcesForRunnable(topSuite.id);
242-
currentlyExecutingSuites.pop();
243-
reporter.jasmineDone({
244-
failedExpectations: topSuite.result.failedExpectations,
245-
});
246-
247-
j$.process.removeListener('uncaughtException', uncaught);
248-
j$.process.removeListener('unhandledRejection', uncaught);
249270

250-
// restore previous exception handlers
251-
oldListenersException.forEach(listener => {
252-
j$.process.on('uncaughtException', listener);
253-
});
271+
currentDeclarationSuite = lastDeclarationSuite;
254272

255-
oldListenersRejection.forEach(listener => {
256-
j$.process.on('unhandledRejection', listener);
257-
});
273+
if (currentlyExecutingSuites.length === 0) {
274+
executionTeardown();
275+
}
258276
};
259277

260278
this.addReporter = function(reporterToAdd) {

packages/jest-jasmine2/src/tree_processor.js

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -43,40 +43,42 @@ export default function treeProcessor(options: Options) {
4343
return parentEnabled || runnableIds.indexOf(node.id) !== -1;
4444
}
4545

46-
return queueRunnerFactory({
47-
onException: error => tree.onException(error),
48-
queueableFns: wrapChildren(tree, isEnabled(tree, false)),
49-
userContext: tree.sharedUserContext(),
50-
});
51-
52-
function executeNode(node, parentEnabled) {
46+
function getNodeHandler(node: TreeNode, parentEnabled: boolean) {
5347
const enabled = isEnabled(node, parentEnabled);
54-
if (!node.children) {
55-
return {
56-
fn(done) {
57-
node.execute(done, enabled);
58-
},
59-
};
60-
}
61-
return {
62-
async fn(done) {
63-
nodeStart(node);
64-
await queueRunnerFactory({
65-
onException: error => node.onException(error),
66-
queueableFns: wrapChildren(node, enabled),
67-
userContext: node.sharedUserContext(),
68-
});
69-
nodeComplete(node);
70-
done();
71-
},
48+
return node.children
49+
? getNodeWithChildrenHandler(node, enabled)
50+
: getNodeWithoutChildrenHandler(node, enabled);
51+
}
52+
53+
function getNodeWithoutChildrenHandler(node: TreeNode, enabled: boolean) {
54+
return function fn(done: (error?: any) => void = () => {}) {
55+
node.execute(done, enabled);
56+
};
57+
}
58+
59+
function getNodeWithChildrenHandler(node: TreeNode, enabled: boolean) {
60+
return async function fn(done: (error?: any) => void = () => {}) {
61+
nodeStart(node);
62+
await queueRunnerFactory({
63+
onException: error => node.onException(error),
64+
queueableFns: wrapChildren(node, enabled),
65+
userContext: node.sharedUserContext(),
66+
});
67+
nodeComplete(node);
68+
done();
7269
};
7370
}
7471

7572
function wrapChildren(node: TreeNode, enabled: boolean) {
7673
if (!node.children) {
7774
throw new Error('`node.children` is not defined.');
7875
}
79-
const children = node.children.map(child => executeNode(child, enabled));
76+
const children = node.children.map(child => ({
77+
fn: getNodeHandler(child, enabled),
78+
}));
8079
return node.beforeAllFns.concat(children).concat(node.afterAllFns);
8180
}
81+
82+
const treeHandler = getNodeHandler(tree, false);
83+
return treeHandler();
8284
}

0 commit comments

Comments
 (0)