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

[Bug]: mockImplementationOnce bleeds into withImplementation #13887

Closed
mysteryven opened this issue Feb 10, 2023 · 2 comments · Fixed by #13888
Closed

[Bug]: mockImplementationOnce bleeds into withImplementation #13887

mysteryven opened this issue Feb 10, 2023 · 2 comments · Fixed by #13888

Comments

@mysteryven
Copy link

Version

29.4.2

Steps to reproduce

  1. Open online repo
  2. Run npm install
  3. Run npm test

Expected behavior

 const mock = jest
    .fn(() => 'outside callback')
    .mockImplementationOnce(() => 'once');

  mock.withImplementation(
    () => 'inside callback',
    () => {
      expect(mock()).toBe('inside callback');
    }
  );

  expect(mock()).toBe('once');
  expect(mock()).toBe('outside callback');

Actual behavior

 const mock = jest
    .fn(() => 'outside callback')
    .mockImplementationOnce(() => 'once');

  mock.withImplementation(
    () => 'inside callback',
    () => {
       mock() // 'once'
    }
  );

  mock() // 'outside callback'
  mock() // 'outside callback'

Additional context

I think it is a little counterintuitive when unused implementationOnce bleeds into withImplementation.

Environment

System:
    OS: macOS 13.0.1
    CPU: (8) arm64 Apple M1 Pro
  Binaries:
    Node: 18.12.1 - ~/.nvm/versions/node/v18.12.1/bin/node
    Yarn: 1.22.4 - ~/.yarn/bin/yarn
    npm: 8.19.2 - ~/.nvm/versions/node/v18.12.1/bin/npm
  npmPackages:
    jest: ^29.4.2 => 29.4.2
@mysteryven mysteryven changed the title [Bug]: mockImplementationOnce bleed in withImplementation [Bug]: mockImplementationOnce bleeds into withImplementation Feb 10, 2023
@SimenB
Copy link
Member

SimenB commented Feb 15, 2023

https://github.com/facebook/jest/releases/tag/v29.4.3

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 18, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants