Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(test runner): make expect.extend immutable #32366

Merged
merged 13 commits into from
Sep 12, 2024

Conversation

Skn0tt
Copy link
Member

@Skn0tt Skn0tt commented Aug 29, 2024

Changes expect.extend behaviour so that it doesn't mutate the global instance and behaves closer to what users expect. This is formally a breaking change, and I had to remove a test that asserts the breaking behaviour.

TODO:

  • decide wether this is a separate method or a flag for expect.extend
  • figure out if we need to change docs

@Skn0tt Skn0tt self-assigned this Aug 29, 2024

This comment has been minimized.

@Skn0tt Skn0tt marked this pull request as ready for review August 30, 2024 13:36
@Skn0tt Skn0tt changed the title feat(test runner): expect.extendImmutable feat(test runner): make expect.extend immutable Aug 30, 2024

This comment has been minimized.

This comment has been minimized.

@@ -61,6 +61,7 @@ import {
import { zones } from 'playwright-core/lib/utils';
import { TestInfoImpl } from '../worker/testInfo';
import { ExpectError, isExpectError } from './matcherHint';
import { randomUUID } from 'node:crypto';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are using createGuid() in our source base - Playwright was originally targeting Node 12, so consider using it for consistency, it has the same power.

if (typeof matcherName === 'string') {
for (const prefix of this._prefixes) {
for (let i = prefix.length; i > 0; i--) {
const qualifiedName = `${prefix.slice(0, i).join(':')}$${matcherName}`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to extract this key generation since it is used in two distant places.

}

get(target: Object, matcherName: string | symbol, receiver: any): any {
let matcher = Reflect.get(target, matcherName, receiver);

if (typeof matcherName === 'string') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like moving this block below eliminates the check.

@@ -104,11 +105,13 @@ export const printReceivedStringContainExpectedResult = (

type ExpectMessage = string | { message?: string };

function createMatchers(actual: unknown, info: ExpectMetaInfo): any {
return new Proxy(expectLibrary(actual), new ExpectMetaInfoProxyHandler(info));
function createMatchers(actual: unknown, info: ExpectMetaInfo, prefix: string[], parentPrefixes: string[][]): any {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It took me a while to understand why parentPrefixes are needed. I'd suggest a variation of the implementation where instead of parentPrefixes each expect contains a growing list of matchers it has been extended to so far. In that case merge could be implemented like so:

export function mergeExpects(...expects: any[]) {
  let current = expect;
  for (const e of expects)
    current = current.extend(e[getMatchersSymbol]);
  return current;
}

Would that work?

function createExpect(info: ExpectMetaInfo) {
const getPrefixSymbol = Symbol('get prefix');

function createExpect(info: ExpectMetaInfo, prefix: string[] = [], parentPrefixes: string[][] = []) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In situations like this keep all the new params required, that would reduce the subsequent regressions where the param is omitted by accident.

Copy link
Member Author

@Skn0tt Skn0tt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good feedback, implemented all of it.

This comment has been minimized.

This comment has been minimized.

Copy link
Contributor

github-actions bot commented Sep 2, 2024

Test results for "tests 1"

1 failed
❌ [playwright-test] › babel.spec.ts:135:5 › should not transform external

3 flaky ⚠️ [playwright-test] › ui-mode-test-source.spec.ts:68:5 › should show top-level errors in file
⚠️ [playwright-test] › ui-mode-test-source.spec.ts:104:5 › should show syntax errors in file
⚠️ [webkit-library] › library/browsercontext-viewport-mobile.spec.ts:175:5 › mobile viewport › should scroll when emulating a mobile viewport

30141 passed, 865 skipped
✔️✔️✔️

Merge workflow run.

@Skn0tt
Copy link
Member Author

Skn0tt commented Sep 4, 2024

I took a look at our docs for this. We're not showing the mutable case anywhere, so there's no changes needed.

@Skn0tt
Copy link
Member Author

Skn0tt commented Sep 11, 2024

@pavelfeldman could you take another look?

@Skn0tt Skn0tt merged commit ef4be6a into microsoft:main Sep 12, 2024
29 of 30 checks passed
@muhqu
Copy link
Contributor

muhqu commented Sep 20, 2024

@Skn0tt this is indeed a breaking change. It causes custom asymmetric matchers to no longer get exposed on expect. With 1.47.1 the custom asymmetric matchers were accessible via expect[matcherName] even though playwright's type definition did not expose them. See #32562 (comment)

I've created 2 PRs, one branched from latest main (#32740) and another one that branched right before this PR got merged (#32739)

@muhqu
Copy link
Contributor

muhqu commented Sep 20, 2024

Ah… nevermind, got it working in #32740 with 978cd7d

dgozman added a commit that referenced this pull request Sep 26, 2024
This adds a test for a regression introduced by #32366 and fixed by
#32795.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants