2021-07-28 21:07:11 +02:00
|
|
|
/**
|
|
|
|
|
* Copyright Microsoft Corporation. All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import { currentTestInfo } from '../globals';
|
|
|
|
|
import type { Expect } from '../types';
|
2021-07-30 06:03:50 +02:00
|
|
|
import { expectType, pollUntilDeadline } from '../util';
|
2021-07-28 21:07:11 +02:00
|
|
|
|
2021-07-29 07:30:37 +02:00
|
|
|
export async function toBeTruthy<T>(
|
2021-07-28 21:07:11 +02:00
|
|
|
this: ReturnType<Expect['getState']>,
|
|
|
|
|
matcherName: string,
|
2021-07-29 16:33:19 +02:00
|
|
|
receiver: any,
|
|
|
|
|
receiverType: string,
|
2021-07-29 00:44:44 +02:00
|
|
|
query: (timeout: number) => Promise<T>,
|
2021-07-28 21:07:11 +02:00
|
|
|
options: { timeout?: number } = {},
|
|
|
|
|
) {
|
|
|
|
|
const testInfo = currentTestInfo();
|
|
|
|
|
if (!testInfo)
|
2021-07-29 00:44:44 +02:00
|
|
|
throw new Error(`${matcherName} must be called during the test`);
|
2021-07-29 16:33:19 +02:00
|
|
|
expectType(receiver, receiverType, matcherName);
|
2021-07-28 21:07:11 +02:00
|
|
|
|
2021-09-14 17:47:06 +02:00
|
|
|
const matcherOptions = {
|
2021-07-28 21:07:11 +02:00
|
|
|
isNot: this.isNot,
|
|
|
|
|
promise: this.promise,
|
|
|
|
|
};
|
|
|
|
|
|
2021-07-29 00:44:44 +02:00
|
|
|
let received: T;
|
2021-07-28 21:07:11 +02:00
|
|
|
let pass = false;
|
|
|
|
|
|
2021-08-08 07:08:56 +02:00
|
|
|
await pollUntilDeadline(testInfo, async remainingTime => {
|
2021-07-29 00:44:44 +02:00
|
|
|
received = await query(remainingTime);
|
|
|
|
|
pass = !!received;
|
|
|
|
|
return pass === !matcherOptions.isNot;
|
2021-08-05 19:55:37 +02:00
|
|
|
}, options.timeout, testInfo._testFinished);
|
2021-07-29 00:44:44 +02:00
|
|
|
|
|
|
|
|
const message = () => {
|
2021-09-14 17:47:06 +02:00
|
|
|
return this.utils.matcherHint(matcherName, undefined, '', matcherOptions);
|
2021-07-29 00:44:44 +02:00
|
|
|
};
|
2021-07-28 21:07:11 +02:00
|
|
|
|
|
|
|
|
return { message, pass };
|
|
|
|
|
}
|