jest-plugin-must-assert
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -36,2 +36,9 @@ /** | ||
test('.thens are chained properly', () => { | ||
// Test that our use of zones does not break promise chaining | ||
return Promise.resolve(1) | ||
.then(v => v + 2) | ||
.then(v => expect(v).toBe(3)); | ||
}); | ||
describe('it() should behave the same as test()', () => { | ||
@@ -38,0 +45,0 @@ it('basic tests should pass', () => { |
{ | ||
"name": "jest-plugin-must-assert", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Jest plugin for async tests", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -6,2 +6,6 @@ # `jest-plugin-must-assert` | ||
## WARNING | ||
The plugin is in a working state, but is under construction :construction: | ||
## Install | ||
@@ -8,0 +12,0 @@ |
116
src/index.js
@@ -0,9 +1,14 @@ | ||
const consoleMethods = Object.entries(global.console); | ||
const restoreConsole = () => | ||
consoleMethods.forEach(([key, value]) => { | ||
global.console[key] = value; | ||
}); | ||
require('zone.js'); | ||
// NOTE: zone.js patches console methods, avoid that. | ||
restoreConsole(); | ||
// Zone sets itself as a global... | ||
const Zone = global.Zone; | ||
const testHasNoExplicitAssertionChecks = (state = expect.getState()) => | ||
typeof state.expectedAssertionsNumber !== 'number' && | ||
!state.isExpectingAssertions; | ||
let currentZone = null; | ||
@@ -13,3 +18,45 @@ let uniqueIdentifier = 0; | ||
const wrapTest = fn => { | ||
const testHasNoExplicitAssertionChecks = () => { | ||
// Some misconfigured test (eg overriding expect itself) | ||
if (!(typeof expect !== 'undefined' && 'getState' in expect)) { | ||
return false; | ||
} | ||
const state = expect.getState(); | ||
return ( | ||
typeof state.expectedAssertionsNumber !== 'number' && | ||
!state.isExpectingAssertions | ||
); | ||
}; | ||
const exitZone = () => (currentZone = null); | ||
const enterZone = (callback, name, hasDoneCallback) => { | ||
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 (current.get('id') !== currentZone) { | ||
console.warn( | ||
`Test "${current.name}" is attempting to invoke a ${task.type}(${ | ||
task.source | ||
}) after test completion. Ignoring` | ||
); | ||
return; | ||
} | ||
return delegate.invokeTask(target, task, applyThis, applyArgs); | ||
}, | ||
}); | ||
currentZone = id; | ||
return zone.wrap(hasDoneCallback ? callback : done => callback(done)); | ||
}; | ||
const wrapTest = (fn, name) => { | ||
let testMustAssert; | ||
@@ -20,3 +67,3 @@ const hasDoneCallback = fn.length > 0; | ||
return () => { | ||
const result = fn(); | ||
const result = enterZone(fn, name, false)(); | ||
@@ -32,12 +79,9 @@ if (testHasNoExplicitAssertionChecks()) { | ||
) { | ||
return result.then( | ||
() => (currentZone = null), | ||
e => { | ||
currentZone = null; | ||
throw e; | ||
} | ||
); | ||
return result.then(exitZone, e => { | ||
exitZone(); | ||
throw e; | ||
}); | ||
} | ||
currentZone = null; | ||
exitZone(); | ||
return result; | ||
@@ -49,6 +93,6 @@ }; | ||
const done = () => { | ||
currenZone = null; | ||
exitZone(); | ||
doneOriginal(); | ||
}; | ||
const result = fn(done, ...args); | ||
const result = enterZone(fn, name, true)(done, ...args); | ||
@@ -65,39 +109,3 @@ if (testHasNoExplicitAssertionChecks()) { | ||
return function ehanchedJestMehod(name, fn, timeout) { | ||
const id = uuid(); | ||
const zone = Zone.root.fork({ | ||
name, | ||
properties: { | ||
id, | ||
}, | ||
onInvoke( | ||
delegate, | ||
current, | ||
target, | ||
callback, | ||
applyThis, | ||
applyArgs, | ||
source | ||
) { | ||
currentZone = id; | ||
delegate.invoke(target, callback, applyThis, applyArgs, source); | ||
}, | ||
onInvokeTask(delegate, current, target, task, applyThis, applyArgs) { | ||
if (current.get('id') !== currentZone) { | ||
console.warn( | ||
`Test "${current.name}" is attempting to invoke a ${task.type}(${ | ||
task.source | ||
}) after test completion. Ignoring` | ||
); | ||
} else { | ||
delegate.invokeTask(target, task, applyThis, applyArgs); | ||
} | ||
}, | ||
}); | ||
const hasDoneCallback = fn.length > 0; | ||
const wrapper = zone.wrap(wrapTest(fn)); | ||
return jestTest( | ||
name, | ||
hasDoneCallback ? done => wrapper(done) : wrapper, | ||
timeout | ||
); | ||
return jestTest(name, wrapTest(fn, name), timeout); | ||
}; | ||
@@ -104,0 +112,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
11670
337
47