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: use DiffFormatter for expect's toHaveBeenNthCalledWith #9002

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

camc314
Copy link

@camc314 camc314 commented Feb 19, 2024

warning - this is my first time writing zig, so this might be not be the best way todo it 🙂

What does this PR do?

uses DiffFormatter to show a richer diff when using expect(mock).toHaveBeenNthCalledWith()

Previously:

524 |                   const test = mock();
525 |                   test();
526 | 
527 |                   expect(test).toHaveBeenNthCalledWith(1, "heheh", "AAA");
         ^
error: expect(received).toHaveBeenNthCalledWith(expected)

n: 1
Received: []

Number of calls: 1

Now:

524 |                   const test = mock();
525 |                   test({ foo: "bar" });
526 | 
527 |                   expect(test).toHaveBeenNthCalledWith(1, "heheh", "AAA");
         ^
error: expect(received).toHaveBeenNthCalledWith(expected)

n: 1
Received:   [
+   {
+     foo: "bar",
+   }
-   "heheh",
-   "AAA",
-   empty item
  ]

- Expected  - 3
+ Received  + 3

Number of calls: 1

It does this by first convertin the arguments (expected) and recieved to a JSArray and then passing these to the DiffFormatter

  • Documentation or TypeScript types (it's okay to leave the rest blank in this case)
  • Code changes

How did you verify your code works?

I tested it manually - i couldn't see any automated tests (snapshot) for these so didn't write any. If you want me to add tests, let me know

  • I checked the lifetime of memory allocated to verify it's (1) freed and (2) only freed when it should be
  • I included a test for the new code, or an existing test covers it
  • JSValue used outside outside of the stack is either wrapped in a JSC.Strong or is JSValueProtect'ed
  • I wrote TypeScript/JavaScript tests and they pass locally (bun-debug test test-file-name.test)

^^ not sure about these especially the first/third

@camc314 camc314 force-pushed the c/tohavebeennthcalledwith-diff branch from c64796e to c7db7d9 Compare February 20, 2024 08:41
@camc314 camc314 marked this pull request as ready for review February 20, 2024 08:41
// handle failure
var formatter = JSC.ConsoleObject.Formatter{ .globalThis = globalObject, .quote_strings = true };
const received_fmt = nthCallValue.toFmt(globalObject, &formatter);
const received = JSValue.createEmptyArray(globalObject, nthCallValue.getLength(globalObject));
Copy link
Collaborator

Choose a reason for hiding this comment

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

JSC has a way to call JSC::constructArray() from a JSC::ArgList. JSC::ArgList can be constructed from a JSC::CallFrame.

I also wonder if we could add a slice() binding since that's what we want here (not literally calling Array.prototype.slice but an internal function which performs a similar operation)

Maybe we should add a binding for to JSC.CallFrame (zig) that calls JSC::constructArray instead of doing this?

Copy link
Contributor

Choose a reason for hiding this comment

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

The status quo is very broken, can we follow up on this?

@Electroid Electroid requested a review from Jarred-Sumner March 5, 2024 22:21
Copy link
Contributor

github-actions bot commented Aug 8, 2024

This pull request is stale and may be closed due to inactivity.

@github-actions github-actions bot added the stale label Aug 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants