jest-plugin-must-assert
Advanced tools
Comparing version 1.1.0 to 1.2.0
{ | ||
"name": "jest-plugin-must-assert", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Jest plugin for async tests", | ||
@@ -34,4 +34,5 @@ "main": "index.js", | ||
"dependencies": { | ||
"stack-utils": "^1.0.2", | ||
"zone.js": "^0.9.0" | ||
} | ||
} |
@@ -57,7 +57,23 @@ # `jest-plugin-must-assert` | ||
* | ||
* @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. | ||
* | ||
* @return {Boolean} true/false for whether or not the task should execute | ||
*/ | ||
onInvokeTask(originTestId, currentTestId, log) { | ||
return false; | ||
} | ||
onInvokeTask({ | ||
originZoneId, | ||
currentZoneId, | ||
testName, | ||
taskType, | ||
taskSource, | ||
}) { | ||
// This is the default implementation of onInvokeTask. The error thrown will | ||
// be displayed as a logger.warn with a cleaned up stack trace. | ||
if (originZoneId !== currentZoneId) { | ||
throw new Error( | ||
`Test "${testName}" is attempting to invoke a ${taskType}(${taskSource}) after test completion. Ignoring` | ||
); | ||
} | ||
return true; | ||
}, | ||
/** | ||
@@ -64,0 +80,0 @@ * Logger DI. Used by the internal methods to log warnings/errors. Should match console API. |
@@ -0,3 +1,6 @@ | ||
const StackUtils = require('stack-utils'); | ||
module.exports = patchJestAPI; | ||
const EXPOSE_ERROR = Symbol('EXPOSE_ERROR'); | ||
function onInvokeTaskDefault({ | ||
@@ -9,9 +12,7 @@ originZoneId, | ||
taskSource, | ||
logger, | ||
}) { | ||
if (originZoneId !== currentZoneId) { | ||
logger.warn( | ||
`Test "${testName}" is attempting to invoke a ${taskType}(${taskSource}) after test completion. Ignoring` | ||
throw new Error( | ||
`Test "${testName}" is attempting to invoke a ${taskType}(${taskSource}) after test completion. See stack-trace for details.` | ||
); | ||
return false; | ||
} | ||
@@ -32,6 +33,14 @@ return true; | ||
require('zone.js'); | ||
require('zone.js/dist/long-stack-trace-zone'); | ||
// NOTE: zone.js patches console methods, avoid that. | ||
restoreConsole(); | ||
const stack = new StackUtils({ | ||
cwd: process.cwd(), | ||
internals: StackUtils.nodeInternals().concat([ | ||
/Zone/, | ||
/zone\.js/, | ||
/jest-plugin-must-assert/, | ||
]), | ||
}); | ||
// Zone sets itself as a global... | ||
@@ -58,27 +67,47 @@ const Zone = global.Zone; | ||
const id = uuid(); | ||
const zone = Zone.root.fork({ | ||
name, | ||
properties: { | ||
id, | ||
}, | ||
onHandleError(delegate, current, target, e) { | ||
return false; | ||
}, | ||
onInvokeTask(delegate, current, target, task, applyThis, applyArgs) { | ||
if ( | ||
!onInvokeTask({ | ||
originZoneId: current.get('id'), | ||
currentZoneId: currentZone, | ||
testName: name, | ||
taskType: task.type, | ||
taskSource: task.source, | ||
logger: logger, | ||
}) | ||
) { | ||
return; | ||
} | ||
return delegate.invokeTask(target, task, applyThis, applyArgs); | ||
}, | ||
}); | ||
const zone = Zone.root | ||
.fork({ | ||
name, | ||
properties: { | ||
id, | ||
}, | ||
onHandleError(delegate, current, target, e) { | ||
if (e && e[EXPOSE_ERROR]) { | ||
logger.warn(`${e.message}\n\n${stack.clean(e.stack)}`); | ||
} | ||
return false; | ||
}, | ||
onInvokeTask(delegate, current, target, task, applyThis, applyArgs) { | ||
let error; | ||
let result = true; | ||
try { | ||
result = onInvokeTask({ | ||
originZoneId: current.get('id'), | ||
currentZoneId: currentZone, | ||
testName: name, | ||
taskType: task.type, | ||
taskSource: task.source, | ||
logger: logger, | ||
}); | ||
} catch (e) { | ||
error = e; | ||
} | ||
if (error) { | ||
error[EXPOSE_ERROR] = true; | ||
error.task = task; | ||
throw error; | ||
} | ||
if (!result) { | ||
return; | ||
} | ||
return delegate.invokeTask(target, task, applyThis, applyArgs); | ||
}, | ||
}) | ||
// We fork from the special stack-trace zone so that there is a trail leading | ||
// back to the origin of the ignored tasks | ||
.fork(Zone.longStackTraceZoneSpec); | ||
currentZone = id; | ||
@@ -85,0 +114,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16035
393
100
2
+ Addedstack-utils@^1.0.2
+ Addedescape-string-regexp@2.0.0(transitive)
+ Addedstack-utils@1.0.5(transitive)