@sprucelabs/test-utils
Version:
Helpful utilities to make asserting more complicated conditions quick and easy! ⚡️
96 lines (95 loc) • 3.33 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TestLifecycleListeners = void 0;
class SpruceTestResolver {
static resolveTestClass(target) {
if (!this.__activeTest) {
this.__activeTest = this.ActiveTestClass
? new this.ActiveTestClass()
: target;
}
return this.__activeTest;
}
static getActiveTest() {
return this.__activeTest;
}
static reset() {
delete this.__activeTest;
}
static onWillCallBeforeAll(cb) {
TestLifecycleListeners.willBeforeAllListeners.push(cb);
}
static onDidCallBeforeAll(cb) {
TestLifecycleListeners.didBeforeAllListeners.push(cb);
}
static onWillCallBeforeEach(cb) {
TestLifecycleListeners.willBeforeEachListeners.push(cb);
}
static onDidCallBeforeEach(cb) {
TestLifecycleListeners.didBeforeEachListeners.push(cb);
}
static onWillCallAfterEach(cb) {
TestLifecycleListeners.willAfterEachListeners.push(cb);
}
static onDidCallAfterEach(cb) {
TestLifecycleListeners.didAfterEachListeners.push(cb);
}
static onWillCallAfterAll(cb) {
TestLifecycleListeners.willAfterAllListeners.push(cb);
}
static onDidCallAfterAll(cb) {
TestLifecycleListeners.didAfterAllListeners.push(cb);
}
}
exports.default = SpruceTestResolver;
class TestLifecycleListeners {
static async emitWillRunBeforeAll() {
for (const cb of this.willBeforeAllListeners) {
await cb(SpruceTestResolver.getActiveTest().constructor);
}
}
static async emitDidRunBeforeAll() {
for (const cb of this.didBeforeAllListeners) {
await cb(SpruceTestResolver.getActiveTest().constructor);
}
}
static async emitWillRunBeforeEach() {
for (const cb of this.willBeforeEachListeners) {
await cb(SpruceTestResolver.getActiveTest());
}
}
static async emitDidRunBeforeEach() {
for (const cb of this.didBeforeEachListeners) {
await cb(SpruceTestResolver.getActiveTest());
}
}
static async emitWillRunAfterEach() {
for (const cb of this.willAfterEachListeners) {
await cb(SpruceTestResolver.getActiveTest());
}
}
static async emitDidRunAfterEach() {
for (const cb of this.didAfterEachListeners) {
await cb(SpruceTestResolver.getActiveTest());
}
}
static async emitWillRunAfterAll() {
for (const cb of this.willAfterAllListeners) {
await cb(SpruceTestResolver.getActiveTest().constructor);
}
}
static async emitDidRunAfterAll() {
for (const cb of this.didAfterAllListeners) {
await cb(SpruceTestResolver.getActiveTest().constructor);
}
}
}
exports.TestLifecycleListeners = TestLifecycleListeners;
TestLifecycleListeners.willBeforeAllListeners = [];
TestLifecycleListeners.didBeforeAllListeners = [];
TestLifecycleListeners.willBeforeEachListeners = [];
TestLifecycleListeners.didBeforeEachListeners = [];
TestLifecycleListeners.willAfterEachListeners = [];
TestLifecycleListeners.didAfterEachListeners = [];
TestLifecycleListeners.willAfterAllListeners = [];
TestLifecycleListeners.didAfterAllListeners = [];