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]: Cannot expect function parameters if they are objects modified in finally section #15538

Open
Anutrix opened this issue Mar 11, 2025 · 0 comments

Comments

@Anutrix
Copy link

Anutrix commented Mar 11, 2025

Version

29.6.4

Steps to reproduce

  1. Create a js file calling a imported function with a object modified post call in finally section. Let users.js be:
const axios = require('axios');

class Users {
  static async all(x) {
    try {
      return (await Promise.allSettled([axios.get(x)])).flatMap(
        (result) => result.value
      );
    } finally {
      x.a.b = 'Not C';
    }
  }
}

module.exports = Users;

  1. Write test where the params is expected:
const Users = require('../users');
const axios = require('axios');

jest.mock('axios');

describe('Users', () => {
  describe('.all', () => {
    it('Test', async () => {
      axios.get.mockResolvedValueOnce([]);
      await Users.all({ a: { b: 'c' } });
      expect(axios.get.mock.calls[0][0].a.b).toEqual('c');
    });
  });
});
  1. Run the test.

Stackblitz link: https://stackblitz.com/edit/node-fmebxovs?file=users.js
Not sure how long it'll work.

Expected behavior

Changes in finally section does not affect params. Test in the example should pass.
Should show 'c'.

Actual behavior

Changes in finally section affects params. Test fails.
Shows 'Not c'.

Additional context

No response

Environment

System:
    OS: macOS 15.3.1
    CPU: (16) x64 Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
  Binaries:
    Node: 20.18.1 - ~/.n/bin/node
    npm: 10.8.2 - ~/.n/bin/npm
  npmPackages:
    jest: ^29.7.0 => 29.7.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant