Files
angular/packages/core/test/render3/jit/matcher.ts
Kristiyan Kostadinov 41223a81f2 build: update to jasmine 4.0 (#45558)
Updates us to version 4.0 of Jasmine and fixes some errors that were the result of us depending upon deprecated APIs. We need to do this both to stay up to date and because it was going to break eventually, because one of the Bazel packages was logging a deprecation warning that version 4.0 was required.

There were also some cases where the state of `ngDevMode` had started leaking out between tests.

PR Close #45558
2022-04-11 16:25:28 +00:00

57 lines
1.6 KiB
TypeScript

/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
/**
* Jasmine matcher to verify that a function contains the provided code fragments.
*/
export function functionContaining(expectedFragments: Array<string|RegExp>):
jasmine.AsymmetricMatcher<Function> {
let _actual: Function|null = null;
const matches = (code: string, fragment: string|RegExp): boolean => {
if (typeof fragment === 'string') {
return code.includes(fragment);
} else {
return fragment.test(code);
}
};
return {
asymmetricMatch(actual: Function): boolean {
_actual = actual;
if (typeof actual !== 'function') {
return false;
}
const code = actual.toString();
for (const fragment of expectedFragments) {
if (!matches(code, fragment)) {
return false;
}
}
return true;
},
jasmineToString(pp: (value: any) => string): string {
if (typeof _actual !== 'function') {
return `Expected function to contain code fragments ${pp(expectedFragments)} but got ${
pp(_actual)}`;
}
const errors: string[] = [];
const code = _actual.toString();
errors.push(
`The actual function with code:\n${code}\n\ndid not contain the following fragments:`);
for (const fragment of expectedFragments) {
if (!matches(code, fragment)) {
errors.push(`- ${fragment}`);
}
}
return errors.join('\n');
}
};
}