UNPKG

vitest-marbles

Version:

Marble testing helpers library for RxJs and Jest

97 lines 4.36 kB
import { Marblizer } from '../marblizer'; function canMarblize(...messages) { return messages.every(message => message.filter(({ notification: { kind } }) => kind === 'N').every(isCharacter)); } function isCharacter({ notification }) { const value = notification.value; return ((typeof value === 'string' && value.length === 1) || (value !== undefined && JSON.stringify(value).length === 1)); } export const customTestMatchers = { toBeNotifications(actual, expected) { let actualMarble = actual; let expectedMarble = expected; if (canMarblize(actual, expected)) { actualMarble = Marblizer.marblize(actual); expectedMarble = Marblizer.marblize(expected); } const pass = this.equals(actualMarble, expectedMarble); const message = pass ? () => this.utils.matcherHint('.not.toBeNotifications') + '\n\n' + `Expected notifications to not be:\n` + ` ${this.utils.printExpected(expectedMarble)}\n` + `But got:\n` + ` ${this.utils.printReceived(actualMarble)}` : () => { const diffString = this.utils.diff(expectedMarble, actualMarble, { expand: true, }); return (this.utils.matcherHint('.toBeNotifications') + '\n\n' + `Expected notifications to be:\n` + ` ${this.utils.printExpected(expectedMarble)}\n` + `But got:\n` + ` ${this.utils.printReceived(actualMarble)}` + (diffString ? `\n\nDifference:\n\n${diffString}` : '')); }; return { message, pass }; }, toBeSubscriptions(actual, expected) { const actualMarbleArray = Marblizer.marblizeSubscriptions(actual); const expectedMarbleArray = Marblizer.marblizeSubscriptions(expected); const pass = subscriptionsPass(actualMarbleArray, expectedMarbleArray); const message = pass ? () => this.utils.matcherHint('.not.toHaveSubscriptions') + '\n\n' + `Expected observable to not have the following subscription points:\n` + ` ${this.utils.printExpected(expectedMarbleArray)}\n` + `But got:\n` + ` ${this.utils.printReceived(actualMarbleArray)}` : () => { const diffString = this.utils.diff(expectedMarbleArray, actualMarbleArray, { expand: true, }); return (this.utils.matcherHint('.toHaveSubscriptions') + '\n\n' + `Expected observable to have the following subscription points:\n` + ` ${this.utils.printExpected(expectedMarbleArray)}\n` + `But got:\n` + ` ${this.utils.printReceived(actualMarbleArray)}` + (diffString ? `\n\nDifference:\n\n${diffString}` : '')); }; return { message, pass }; }, toHaveEmptySubscriptions(actual) { const pass = !(actual && actual.length > 0); let marbles; if (actual && actual.length > 0) { marbles = Marblizer.marblizeSubscriptions(actual); } const message = pass ? () => this.utils.matcherHint('.not.toHaveNoSubscriptions') + '\n\n' + `Expected observable to have at least one subscription point, but got nothing` + this.utils.printReceived('') : () => this.utils.matcherHint('.toHaveNoSubscriptions') + '\n\n' + `Expected observable to have no subscription points\n` + `But got:\n` + ` ${this.utils.printReceived(marbles)}\n\n`; return { message, pass }; }, }; function subscriptionsPass(actualMarbleArray, expectedMarbleArray) { if (actualMarbleArray.length !== expectedMarbleArray.length) { return false; } let pass = true; for (const actualMarble of actualMarbleArray) { if (!expectedMarbleArray.includes(actualMarble)) { pass = false; break; } } return pass; } expect.extend(customTestMatchers); //# sourceMappingURL=custom-matchers.js.map