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

Update jest-utils.ts #2990

Merged
merged 6 commits into from
May 3, 2023
Merged

Update jest-utils.ts #2990

merged 6 commits into from
May 3, 2023

Conversation

lifeiscontent
Copy link
Contributor

@lifeiscontent lifeiscontent commented Mar 9, 2023

don't do property access to check if something isDomNode

this call is breaking the usage of proxy objects in tests, e.g. if I want to make sure that the object I'm testing is only ever accessing keys it defined.

  it("Should return whatever you pass in", () => {
    const result = fromPartial({ foo: "bar" });

    expect(result).toEqual({ foo: "bar" });
  });

  it("Should throw an error when the test itself accesses a property not defined on the input", () => {
    const func = (input: { id: string }) => {
      return input.id;
    };

    expect(() => func(fromPartial({}))).toThrow();
  });

where fromPartial looks like this:

export const fromPartial = <T extends {}>(mock: PartialDeep<NoInfer<T>>): T => {
  const proxy = new Proxy(mock, {
    get(target, prop, receiver) {
      let value = target[prop as keyof typeof target];
      let v = typeof value === "function" ? value.bind(target) : value;

      console.log({ v, prop }); // this is where I'm seeing nodeType from vitest
      return v;
    },
  });
  return proxy as T;
};

@@ -135,10 +135,6 @@ function eq(
if (typeof a !== 'object' || typeof b !== 'object')
return false

// Use DOM3 method isEqualNode (IE>=9)
if (isDomNode(a) && isDomNode(b))
Copy link
Member

Choose a reason for hiding this comment

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

We can't just remove this because someone wants to.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@sheremet-va I agree, but wouldn't this be something that should enhance functionality? e.g. if you're using the JSDOM adapter? if you're not writing tests for non-browser APIs, why should it exist?

Copy link
Member

Choose a reason for hiding this comment

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

There is no JSDOM adapter for equality. If you are afraid of the Proxy usage here we can change it in a few other ways:

  • Check that global Element exists and check instanceof on the value.
  • Instead of direct access obj.nodeType use 'nodeType' in obj && obj.nodeType

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@sheremet-va that's a great suggestion :) Updated.

@lifeiscontent
Copy link
Contributor Author

@sheremet-va friendly ping

@sheremet-va sheremet-va merged commit f3c3607 into vitest-dev:main May 3, 2023
@lifeiscontent lifeiscontent deleted the patch-1 branch May 26, 2023 08:37
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