break extend

This commit is contained in:
Simon Knott 2024-08-30 15:12:41 +02:00
parent 2ca4a01af2
commit 9a160c6f67
No known key found for this signature in database
GPG key ID: 8CEDC00028084AEC

View file

@ -110,25 +110,6 @@ function createMatchers(actual: unknown, info: ExpectMetaInfo, prefix: string[])
} }
function createExpect(info: ExpectMetaInfo, prefix: string[] = []) { function createExpect(info: ExpectMetaInfo, prefix: string[] = []) {
function extend(matchers: any, qualifier: string) {
const wrappedMatchers: any = {};
for (const [name, matcher] of Object.entries(matchers)) {
wrappedMatchers[qualifier + name] = function(...args: any[]) {
const { isNot, promise, utils } = this;
const newThis: ExpectMatcherState = {
isNot,
promise,
utils,
timeout: currentExpectTimeout()
};
(newThis as any).equals = throwUnsupportedExpectMatcherError;
return (matcher as any).call(newThis, ...args);
};
}
expectLibrary.extend(wrappedMatchers);
}
const expectInstance: Expect<{}> = new Proxy(expectLibrary, { const expectInstance: Expect<{}> = new Proxy(expectLibrary, {
apply: function(target: any, thisArg: any, argumentsList: [unknown, ExpectMessage?]) { apply: function(target: any, thisArg: any, argumentsList: [unknown, ExpectMessage?]) {
const [actual, messageOrOptions] = argumentsList; const [actual, messageOrOptions] = argumentsList;
@ -148,16 +129,26 @@ function createExpect(info: ExpectMetaInfo, prefix: string[] = []) {
if (property === 'extend') { if (property === 'extend') {
return (matchers: any) => { return (matchers: any) => {
extend(matchers, ''); const qualifier = [...prefix, randomUUID()];
return expectInstance;
}; const wrappedMatchers: any = {};
} for (const [name, matcher] of Object.entries(matchers)) {
const key = qualifier.join(':') + '$' + name;
wrappedMatchers[key] = function(...args: any[]) {
const { isNot, promise, utils } = this;
const newThis: ExpectMatcherState = {
isNot,
promise,
utils,
timeout: currentExpectTimeout()
};
(newThis as any).equals = throwUnsupportedExpectMatcherError;
return (matcher as any).call(newThis, ...args);
};
Object.defineProperty(wrappedMatchers[key], 'name', { value: name });
}
expectLibrary.extend(wrappedMatchers);
if (property === 'extendImmutable') {
return (matchers: any) => {
const key = randomUUID();
const qualifier = [...prefix, key];
extend(matchers, `${qualifier.join(':')}$`);
return createExpect(info, qualifier); return createExpect(info, qualifier);
}; };
} }