( expectedFragments: Array<string | RegExp>, )
| 10 | * Jasmine matcher to verify that a function contains the provided code fragments. |
| 11 | */ |
| 12 | export function functionContaining( |
| 13 | expectedFragments: Array<string | RegExp>, |
| 14 | ): jasmine.AsymmetricMatcher<Function> { |
| 15 | let _actual: Function | null = null; |
| 16 | |
| 17 | const matches = (code: string, fragment: string | RegExp): boolean => { |
| 18 | if (typeof fragment === 'string') { |
| 19 | return code.includes(fragment); |
| 20 | } else { |
| 21 | return fragment.test(code); |
| 22 | } |
| 23 | }; |
| 24 | |
| 25 | return { |
| 26 | asymmetricMatch(actual: Function): boolean { |
| 27 | _actual = actual; |
| 28 | |
| 29 | if (typeof actual !== 'function') { |
| 30 | return false; |
| 31 | } |
| 32 | const code = actual.toString(); |
| 33 | for (const fragment of expectedFragments) { |
| 34 | if (!matches(code, fragment)) { |
| 35 | return false; |
| 36 | } |
| 37 | } |
| 38 | return true; |
| 39 | }, |
| 40 | jasmineToString(pp: (value: any) => string): string { |
| 41 | if (typeof _actual !== 'function') { |
| 42 | return `Expected function to contain code fragments ${pp(expectedFragments)} but got ${pp( |
| 43 | _actual, |
| 44 | )}`; |
| 45 | } |
| 46 | const errors: string[] = []; |
| 47 | const code = _actual.toString(); |
| 48 | errors.push( |
| 49 | `The actual function with code:\n${code}\n\ndid not contain the following fragments:`, |
| 50 | ); |
| 51 | for (const fragment of expectedFragments) { |
| 52 | if (!matches(code, fragment)) { |
| 53 | errors.push(`- ${fragment}`); |
| 54 | } |
| 55 | } |
| 56 | return errors.join('\n'); |
| 57 | }, |
| 58 | }; |
| 59 | } |
no outgoing calls
no test coverage detected