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-09-24 01:46:46 +02:00
|
|
|
import { expectType } from '../util';
|
2021-07-28 21:07:11 +02:00
|
|
|
|
2021-09-24 01:46:46 +02:00
|
|
|
export async function toBeTruthy(
|
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-09-24 01:46:46 +02:00
|
|
|
query: (isNot: boolean, timeout: number) => Promise<{ pass: boolean }>,
|
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-09-24 01:46:46 +02:00
|
|
|
let defaultExpectTimeout = testInfo.project.expect?.timeout;
|
|
|
|
|
if (typeof defaultExpectTimeout === 'undefined')
|
|
|
|
|
defaultExpectTimeout = 5000;
|
|
|
|
|
const timeout = options.timeout === 0 ? 0 : options.timeout || defaultExpectTimeout;
|
2021-07-28 21:07:11 +02:00
|
|
|
|
2021-09-24 01:46:46 +02:00
|
|
|
const { pass } = await query(this.isNot, timeout);
|
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 };
|
|
|
|
|
}
|