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(expect): add toHaveBeenCalledAfter and toHaveBeenCalledBefore utility #6056

Merged

Conversation

Barbapapazes
Copy link
Contributor

@Barbapapazes Barbapapazes commented Jul 8, 2024

Description

fix #6045

Hello 👋,

This is my first contribution to Vitest so I really appreciate your feedback.

This PR adds two new test APIs:

  • toHaveBeenCalledBefore - checks if a spy was called before another spy
  • toHaveBeenCalledAfter - checks if a spy was called after another spy

The implementation will take a look at the invocation order (first call) of the spies and compare them to determine if the spies were called in the correct order.

I add them to packages/expect/src/jest-expect.ts but I'm not sure if this is the best place for them. I also update types but I'm not sure if I did it correctly so I would appreciate your feedback on that.

I add related tests to for these new APIs.

Once everything is ok for you, I will add related documentation!

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. If the feature is substantial or introduces breaking changes without a discussion, PR might be closed.
  • Ideally, include a test that fails without this PR but passes with it.
  • Please, don't make changes to pnpm-lock.yaml unless you introduce a new test example.

Tests

  • Run the tests with pnpm test:ci.

Documentation

  • If you introduce new functionality, document it. You can run documentation with pnpm run docs command.

Changesets

  • Changes in changelog are generated from PR name. Please, make sure that it explains your changes in an understandable manner. Please, prefix changeset messages with feat:, fix:, perf:, docs:, or chore:.

@Barbapapazes Barbapapazes changed the title feat(core): add toHaveBeenCalledAfter and toHaveBeenCalledBefore utility feat(expect): add toHaveBeenCalledAfter and toHaveBeenCalledBefore utility Jul 8, 2024
function (resultSpy: MockInstance) {
const expectSpy = getSpy(this)

const [firstExpectSpyCall] = expectSpy.mock.invocationCallOrder
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

@Barbapapazes Barbapapazes Jul 10, 2024

Choose a reason for hiding this comment

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

As the calls are ordered in the table (so the first one is always the smallest), I thought that taking the first was enough, but maybe I'm wrong?

packages/expect/src/jest-expect.ts Show resolved Hide resolved
packages/expect/src/jest-expect.ts Outdated Show resolved Hide resolved
packages/expect/src/jest-expect.ts Outdated Show resolved Hide resolved
sheremet-va
sheremet-va previously approved these changes Oct 2, 2024
Copy link
Member

@sheremet-va sheremet-va left a comment

Choose a reason for hiding this comment

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

Sorry, this got lost in notifications. Looks good to me!

@sheremet-va sheremet-va added this to the 2.2.0 milestone Oct 2, 2024
Copy link

netlify bot commented Nov 13, 2024

Deploy Preview for vitest-dev ready!

Name Link
🔨 Latest commit 05a95bd
🔍 Latest deploy log https://app.netlify.com/sites/vitest-dev/deploys/6734d0fbaebafa0008fd4330
😎 Deploy Preview https://deploy-preview-6056--vitest-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@sheremet-va sheremet-va merged commit 85e6f99 into vitest-dev:main Nov 13, 2024
15 of 17 checks passed
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.

Integrate toHaveBeenCalledBefore and toHaveBeenCalledAfter to core
2 participants