Skip to content

Commit

Permalink
refactor(tests/e2e): simplify spy toHaveBeenCalled matchers (#840)
Browse files Browse the repository at this point in the history
* test(e2e): remove `wait` from spy `toHaveBeenCalled` matchers

* rewrite spy fixtures with expect.poll
  • Loading branch information
sidvishnoi authored Jan 17, 2025
1 parent 16c100e commit da6207c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 35 deletions.
55 changes: 21 additions & 34 deletions tests/e2e/fixtures/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export const test = base.extend<{ page: Page }, BaseScopeWorker>({
const defaultMessage = (
thisType: ExpectMatcherState,
assertionName: string,
pass: boolean,
_pass: boolean,
expected: unknown,
matcherResult?: { actual: unknown },
) => {
Expand All @@ -89,7 +89,7 @@ const defaultMessage = (
undefined,
{ isNot: thisType.isNot },
);
const expectedPart = `Expected:${pass ? '' : ' not '}${thisType.utils.printExpected(expected)}`;
const expectedPart = `Expected: ${thisType.isNot ? 'not ' : ''}${thisType.utils.printExpected(expected)}`;
const receivedPart = matcherResult
? `Received: ${thisType.utils.printReceived(matcherResult.actual)}`
: '';
Expand Down Expand Up @@ -128,27 +128,20 @@ export const expect = test.expect.extend({
async toHaveBeenCalledTimes(
fn: SpyFn,
expected: number,
{ timeout = 5000, wait = 1000 }: { timeout?: number; wait?: number } = {},
{ timeout = 5000 }: { timeout?: number } = {},
) {
const name = 'toHaveBeenCalledTimes';

let pass: boolean;
let result: { actual: number } | undefined;

await sleep(wait);
let remainingTime = timeout;
do {
try {
test.expect(fn.callCount).toBe(expected);
pass = true;
break;
} catch {
result = { actual: fn.callCount };
pass = false;
remainingTime -= 500;
await sleep(500);
}
} while (remainingTime > 0);
try {
await test.expect.poll(() => fn.callCount, { timeout }).toBe(expected);
pass = true;
} catch {
result = { actual: fn.callCount };
pass = false;
}

return {
name,
Expand All @@ -162,29 +155,23 @@ export const expect = test.expect.extend({
async toHaveBeenLastCalledWithMatching(
fn: SpyFn,
expected: Record<string, unknown>,
{ timeout = 5000, wait = 1000 }: { timeout?: number; wait?: number } = {},
{ timeout = 5000 }: { timeout?: number } = {},
) {
const name = 'toHaveBeenLastCalledWithMatching';

let pass: boolean;
let result: { actual: unknown } | undefined;

await sleep(wait);
let remainingTime = timeout;
do {
try {
// we only support matching first argument of last call
const lastCallArg = fn.calls[fn.calls.length - 1][0];
test.expect(lastCallArg).toMatchObject(expected);
pass = true;
break;
} catch {
result = { actual: fn.calls[fn.calls.length - 1]?.[0] };
pass = false;
remainingTime -= 500;
await sleep(500);
}
} while (remainingTime > 0);
try {
// we only support matching first argument of last call
await test.expect
.poll(() => fn.calls[fn.calls.length - 1][0], { timeout })
.toMatchObject(expected);
pass = true;
} catch {
result = { actual: fn.calls[fn.calls.length - 1]?.[0] };
pass = false;
}

return {
name,
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/reconnectAutoKeyTestWallet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ test('Reconnect to test wallet with automatic key addition', async ({

await test.step('asks for key-add consent', async () => {
await connectButton.click();
expect(
await expect(
popup.getByTestId('connect-wallet-auto-key-consent'),
).toBeVisible();
await popup
Expand Down

0 comments on commit da6207c

Please sign in to comment.