UNPKG

@thisisagile/easy-test

Version:

Straightforward library for testing microservices built with @thisisagile/easy

1 lines 3.15 kB
{"version":3,"sources":["../src/matchers/Check.ts"],"sourcesContent":["import { isDefined } from '../utils/Utils';\n\ntype ToMessage<S> = string | ((s: S[]) => string);\n\nclass Check<S> {\n constructor(\n private ctx: jest.MatcherContext,\n private readonly received: S,\n private readonly expected: S,\n private readonly failed = false,\n private readonly message = ''\n ) {}\n\n print(message: ToMessage<S>): string {\n return (typeof message === 'function' ? message([this.received, this.expected]) : message)\n .replace('{r}', this.ctx.utils.printReceived(this.received))\n .replace('{e}', this.ctx.utils.printExpected(this.expected))\n .replace('{diff}', this.ctx.utils.diff(this.received, this.expected) ?? '');\n }\n\n not(p: (s: S[]) => boolean, message: ToMessage<S>): Check<S> {\n if (this.failed) {\n return this;\n }\n try {\n return new Check<S>(this.ctx, this.received, this.expected, !p([this.received, this.expected]), this.print(message));\n } catch (e: any) {\n return new Check<S>(this.ctx, this.received, this.expected, true, e.message);\n }\n }\n\n undefined(p: (s: S[]) => any, message: ToMessage<S>): Check<S> {\n return this.not(() => isDefined(p([this.received, this.expected])), this.print(message));\n }\n\n else(message: ToMessage<S> = 'Expected {r} not to match with {e}, but it did.'): jest.CustomMatcherResult {\n return {\n pass: !this.failed,\n message: () => (this.failed ? this.message : this.print(message)),\n };\n }\n}\n\nexport const check = <S>(ctx: jest.MatcherContext, received: S, expected?: S): Check<S> => new Check<S>(ctx, received, expected as S);\n\nexport const checkDefined = <S>(ctx: jest.MatcherContext, received: S, expected?: S): Check<S> =>\n new Check<S>(ctx, received, expected as S).undefined(([r]) => r, 'Received array is undefined.').undefined(([, e]) => e, 'Expected array is undefined.');\n"],"mappings":";;;;;AAIA,IAAM,QAAN,MAAM,OAAS;AAAA,EACb,YACU,KACS,UACA,UACA,SAAS,OACT,UAAU,IAC3B;AALQ;AACS;AACA;AACA;AACA;AAAA,EAChB;AAAA,EAEH,MAAM,SAA+B;AACnC,YAAQ,OAAO,YAAY,aAA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