UNPKG

@sprucelabs/test-utils

Version:

Helpful utilities to make asserting more complicated conditions quick and easy! ⚡️

91 lines (90 loc) 3.14 kB
export default 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); } } export 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); } } } TestLifecycleListeners.willBeforeAllListeners = []; TestLifecycleListeners.didBeforeAllListeners = []; TestLifecycleListeners.willBeforeEachListeners = []; TestLifecycleListeners.didBeforeEachListeners = []; TestLifecycleListeners.willAfterEachListeners = []; TestLifecycleListeners.didAfterEachListeners = []; TestLifecycleListeners.willAfterAllListeners = []; TestLifecycleListeners.didAfterAllListeners = [];