2021-06-06 17:09:53 -07: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 expectLibrary from 'expect';
|
2021-07-28 15:44:44 -07:00
|
|
|
import {
|
|
|
|
toBeChecked,
|
|
|
|
toBeDisabled,
|
|
|
|
toBeEditable,
|
|
|
|
toBeEmpty,
|
|
|
|
toBeEnabled,
|
|
|
|
toBeFocused,
|
|
|
|
toBeHidden,
|
|
|
|
toBeSelected,
|
2021-07-28 22:30:37 -07:00
|
|
|
toBeVisible,
|
2021-07-28 15:44:44 -07:00
|
|
|
toContainText,
|
|
|
|
toHaveAttr,
|
|
|
|
toHaveClass,
|
2021-07-29 07:33:19 -07:00
|
|
|
toHaveCount,
|
2021-07-28 22:30:37 -07:00
|
|
|
toHaveCSS,
|
2021-07-28 15:44:44 -07:00
|
|
|
toHaveData,
|
|
|
|
toHaveId,
|
2021-07-28 22:30:37 -07:00
|
|
|
toHaveProp,
|
2021-07-28 15:44:44 -07:00
|
|
|
toHaveText,
|
2021-07-29 07:33:19 -07:00
|
|
|
toHaveTitle,
|
|
|
|
toHaveURL,
|
2021-07-28 15:44:44 -07:00
|
|
|
toHaveValue
|
2021-07-28 22:30:37 -07:00
|
|
|
} from './matchers/matchers';
|
|
|
|
import { toMatchSnapshot } from './matchers/toMatchSnapshot';
|
2021-07-28 12:07:11 -07:00
|
|
|
import type { Expect } from './types';
|
2021-07-30 16:07:02 -07:00
|
|
|
import matchers from 'expect/build/matchers';
|
|
|
|
import { currentTestInfo } from './globals';
|
2021-06-06 17:09:53 -07:00
|
|
|
|
2021-07-27 20:26:12 -07:00
|
|
|
export const expect: Expect = expectLibrary as any;
|
|
|
|
expectLibrary.setState({ expand: false });
|
2021-07-30 16:07:02 -07:00
|
|
|
const customMatchers = {
|
2021-07-28 12:07:11 -07:00
|
|
|
toBeChecked,
|
|
|
|
toBeDisabled,
|
|
|
|
toBeEditable,
|
|
|
|
toBeEmpty,
|
|
|
|
toBeEnabled,
|
|
|
|
toBeFocused,
|
|
|
|
toBeHidden,
|
2021-07-28 15:44:44 -07:00
|
|
|
toBeSelected,
|
2021-07-28 12:07:11 -07:00
|
|
|
toBeVisible,
|
|
|
|
toContainText,
|
|
|
|
toHaveAttr,
|
2021-07-28 15:44:44 -07:00
|
|
|
toHaveClass,
|
2021-07-29 07:33:19 -07:00
|
|
|
toHaveCount,
|
2021-07-28 22:30:37 -07:00
|
|
|
toHaveCSS,
|
2021-07-28 12:07:11 -07:00
|
|
|
toHaveData,
|
|
|
|
toHaveId,
|
2021-07-28 15:44:44 -07:00
|
|
|
toHaveProp,
|
2021-07-28 12:07:11 -07:00
|
|
|
toHaveText,
|
2021-07-29 07:33:19 -07:00
|
|
|
toHaveTitle,
|
|
|
|
toHaveURL,
|
2021-07-28 12:07:11 -07:00
|
|
|
toHaveValue,
|
|
|
|
toMatchSnapshot,
|
2021-07-30 16:07:02 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
let lastExpectSeq = 0;
|
|
|
|
|
|
|
|
function wrap(matcherName: string, matcher: any) {
|
|
|
|
return function(this: any, ...args: any[]) {
|
|
|
|
const testInfo = currentTestInfo();
|
|
|
|
if (!testInfo)
|
|
|
|
return matcher.call(this, ...args);
|
|
|
|
|
|
|
|
const seq = ++lastExpectSeq;
|
|
|
|
testInfo._progress('expect', { phase: 'begin', seq, matcherName });
|
|
|
|
const endPayload: any = { phase: 'end', seq };
|
|
|
|
let isAsync = false;
|
|
|
|
try {
|
|
|
|
const result = matcher.call(this, ...args);
|
|
|
|
endPayload.pass = result.pass;
|
|
|
|
if (this.isNot)
|
|
|
|
endPayload.isNot = this.isNot;
|
|
|
|
if (result.pass === this.isNot && result.message)
|
|
|
|
endPayload.message = result.message();
|
|
|
|
if (result instanceof Promise) {
|
|
|
|
isAsync = true;
|
|
|
|
return result.catch(e => {
|
|
|
|
endPayload.error = e.stack;
|
|
|
|
throw e;
|
|
|
|
}).finally(() => {
|
|
|
|
testInfo._progress('expect', endPayload);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
} catch (e) {
|
|
|
|
endPayload.error = e.stack;
|
|
|
|
throw e;
|
|
|
|
} finally {
|
|
|
|
if (!isAsync)
|
|
|
|
testInfo._progress('expect', endPayload);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const wrappedMatchers: any = {};
|
|
|
|
for (const matcherName in matchers)
|
|
|
|
wrappedMatchers[matcherName] = wrap(matcherName, matchers[matcherName]);
|
|
|
|
for (const matcherName in customMatchers)
|
|
|
|
wrappedMatchers[matcherName] = wrap(matcherName, (customMatchers as any)[matcherName]);
|
|
|
|
|
|
|
|
expectLibrary.extend(wrappedMatchers);
|