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,
|
2021-07-28 22:30:37 -07:00
|
|
|
toBeVisible,
|
2021-07-28 15:44:44 -07:00
|
|
|
toContainText,
|
2021-08-06 16:58:42 -07:00
|
|
|
toHaveAttribute,
|
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 15:44:44 -07:00
|
|
|
toHaveId,
|
2021-08-06 16:58:42 -07:00
|
|
|
toHaveJSProperty,
|
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-08-02 22:11:37 -07:00
|
|
|
import type { Expect, TestError } from './types';
|
2021-07-30 16:07:02 -07:00
|
|
|
import matchers from 'expect/build/matchers';
|
|
|
|
import { currentTestInfo } from './globals';
|
2021-08-02 22:11:37 -07:00
|
|
|
import { serializeError } from './util';
|
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,
|
|
|
|
toBeVisible,
|
|
|
|
toContainText,
|
2021-08-06 16:58:42 -07:00
|
|
|
toHaveAttribute,
|
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
|
|
|
toHaveId,
|
2021-08-06 16:58:42 -07:00
|
|
|
toHaveJSProperty,
|
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
|
|
|
};
|
|
|
|
|
|
|
|
function wrap(matcherName: string, matcher: any) {
|
|
|
|
return function(this: any, ...args: any[]) {
|
|
|
|
const testInfo = currentTestInfo();
|
|
|
|
if (!testInfo)
|
|
|
|
return matcher.call(this, ...args);
|
|
|
|
|
2021-08-02 17:17:20 -07:00
|
|
|
const infix = this.isNot ? '.not' : '';
|
|
|
|
const completeStep = testInfo._addStep('expect', `expect${infix}.${matcherName}`);
|
2021-08-02 22:11:37 -07:00
|
|
|
const stack = new Error().stack;
|
2021-08-02 17:17:20 -07:00
|
|
|
|
|
|
|
const reportStepEnd = (result: any) => {
|
2021-08-02 22:11:37 -07:00
|
|
|
const success = result.pass !== this.isNot;
|
|
|
|
let error: TestError | undefined;
|
|
|
|
if (!success)
|
|
|
|
error = { message: result.message(), stack };
|
2021-08-02 17:17:20 -07:00
|
|
|
completeStep?.(error);
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
|
|
|
const reportStepError = (error: Error) => {
|
2021-08-02 22:11:37 -07:00
|
|
|
completeStep?.(serializeError(error));
|
2021-08-02 17:17:20 -07:00
|
|
|
throw error;
|
|
|
|
};
|
|
|
|
|
2021-07-30 16:07:02 -07:00
|
|
|
try {
|
|
|
|
const result = matcher.call(this, ...args);
|
2021-08-02 17:17:20 -07:00
|
|
|
if (result instanceof Promise)
|
|
|
|
return result.then(reportStepEnd).catch(reportStepError);
|
|
|
|
return reportStepEnd(result);
|
2021-07-30 16:07:02 -07:00
|
|
|
} catch (e) {
|
2021-08-02 17:17:20 -07:00
|
|
|
reportStepError(e);
|
2021-07-30 16:07:02 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|