Skip to content

Commit

Permalink
Provide stack traces (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthur Buldauskas authored Aug 23, 2021
1 parent 85346e8 commit 1efe3d6
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 15 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,14 @@ patchJestAPI({
* @param {Object} options Options for the handler (see below)
*
* Options:
* @param {Number} originTestId The unique ID of the test where the task is oginating from
* @param {Number} currentTestId The unique ID of the currently executing test
* @param {String} testName The name of the test which triggered this event
* @param {String} taskType The type of task being invoked (micro/macro task)
* @param {String} taskSource The source of the taks ("promise.then", "setTimeout" etc)
* @param {Object} logger The logger object (defaults to console)
* @param {Number} originTestId The unique ID of the test where the task is oginating from
* @param {Number} currentTestId The unique ID of the currently executing test
* @param {String} testName The name of the test which triggered this event
* @param {Object} task The task which is about to be invoked
* @param {String} task.type The type of task being invoked (micro/macro task)
* @param {String} task.source The source of the taks ("promise.then", "setTimeout" etc)
* @param {Object} logger The logger object (defaults to console)
* @param {Function} getStackTrace Returns the stack-trace of the stack
*
* @throws {Error} Default: throws. This function _may_ throw an error instead of logging it if
* you would like a stack trace back to the origin of the task being ignored.
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "Jest plugin for async tests",
"main": "index.js",
"scripts": {
"test": "jest"
"test": "jest",
"harness": "node test-harness.js"
},
"engines": {
"node": ">= 12.17.0"
Expand Down
11 changes: 6 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,17 @@ function onInvokeTaskDefault({
// The name of the test from where this task originates
testName,
// The type of the task being acted upon [micro || macro]Task
taskType,
// Promise, setTimeout etc.
taskSource,
task,
// Get the stack trace associated wit the task
getStackTrace,
}) {
// Note that we do not use "testName" for this as they are not guaranteed to be
// unique
if (originZoneId !== currentZoneId) {
throw new Error(
`Test "${testName}" is attempting to invoke a ${taskType}(${taskSource}) after test completion. See stack-trace for details.`
const error = new Error(
`Test "${testName}" is attempting to invoke a ${task.type}(${task.source}) after test completion. See stack-trace for details.`
);
throw error;
}
return true;
}
Expand Down
23 changes: 21 additions & 2 deletions src/zones.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,33 @@ const getZones = ({ onInvokeTask, logger, ignoreStack }) => {
onInvokeTask(delegate, current, target, task, applyThis, applyArgs) {
let error;
let result = true;

// Exposes the stack trace associated with the task
function getStackTrace() {
let stack;
try {
throw new Error();
} catch (e) {
e.task = task;
Zone.longStackTraceZoneSpec.onHandleError(
delegate,
current,
target,
e
);
stack = e.stack;
}
return stack;
}

try {
result = onInvokeTask({
originZoneId: current.get('id'),
currentZoneId: currentZone,
testName: name,
taskType: task.type,
taskSource: task.source,
task,
logger: logger,
getStackTrace,
});
} catch (e) {
error = e;
Expand Down
2 changes: 1 addition & 1 deletion test-harness.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* This script is used to run the e2e folder tests directly to get realtime
* feedback.
*/
const runCLI = require('jest-cli').runCLI;
const runCLI = require('jest').runCLI;

const [env = 'node'] = process.argv.slice(2);
const projectmap = {
Expand Down

0 comments on commit 1efe3d6

Please sign in to comment.