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

Jest compatibility API: replaceProperty #2831

Closed
4 tasks done
trivikr opened this issue Feb 8, 2023 · 7 comments · Fixed by #2871
Closed
4 tasks done

Jest compatibility API: replaceProperty #2831

trivikr opened this issue Feb 8, 2023 · 7 comments · Fixed by #2871

Comments

@trivikr
Copy link
Contributor

trivikr commented Feb 8, 2023

Clear and concise description of the problem

Jest introduced API replaceProperty in v29.4.0

This API is also available in sinon
https://sinonjs.org/releases/v14/sandbox/#sandboxreplaceobject-property-replacement

Example code tested with jest@29.4.2:

test("replaceProperty", () => {
  expect(process.env.TEST_ENV_KEY).toBeUndefined();
  jest.replaceProperty(process, 'env', { TEST_ENV_KEY: "test" });  
  expect(process.env.TEST_ENV_KEY).toBe("test");
  jest.restoreAllMocks();
  expect(process.env.TEST_ENV_KEY).toBeUndefined();
});

Suggested solution

Add vi.replaceProprety API

Alternative

No response

Additional context

No response

Validations

@mysteryven
Copy link
Contributor

mysteryven commented Feb 9, 2023

Thanks for your mention, I'd like to take this when it adds the label.

@trivikr
Copy link
Contributor Author

trivikr commented Feb 14, 2023

@sheremet-va Would replaceProperty API be helpful to vitest users?

If yes, can you add enhancement and/or pr welcome labels?
@mysteryven is interested in implementing it.

@sheremet-va
Copy link
Member

I do like the API, but I don't like that restoreAllMocks affects it. We have a similar API with stubs for a global object (stubGlobal) and env (stubEnv) already. Do we need more?

@trivikr
Copy link
Contributor Author

trivikr commented Feb 14, 2023

The APIs vi.stubGlobal and vi.stubEnv are better fit for this requirement.

I verified that those work:

test("stubEnv", () => {
  expect(process.env.TEST_ENV_KEY).toBeUndefined();
  vi.stubEnv("TEST_ENV_KEY", "test");
  expect(process.env.TEST_ENV_KEY).toBe("test");
  vi.unstubAllEnvs();
  expect(process.env.TEST_ENV_KEY).toBeUndefined();
});

test("stubGlobal", () => {
  expect(global.innerWidth).toBeUndefined();

  const mockInnerWidth = 100;
  vi.stubGlobal("innerWidth", 100);
  expect(innerWidth).toBe(mockInnerWidth);
  expect(global.innerWidth).toBe(mockInnerWidth);

  vi.unstubAllGlobals();
  expect(global.innerWidth).toBeUndefined();
});

@trivikr
Copy link
Contributor Author

trivikr commented Feb 14, 2023

@mysteryven As part of this request, can you add documentation in migration guide calling out the difference with Jest?

@mysteryven
Copy link
Contributor

mysteryven commented Feb 15, 2023

@mysteryven As part of this request, can you add documentation in migration guide calling out the difference with Jest?

Of course, I will add it.

@trivikr
Copy link
Contributor Author

trivikr commented Feb 15, 2023

The vitest-codemod will transform replaceProperty with stubEnv for process.env

trivikr/vitest-codemod#126

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

Successfully merging a pull request may close this issue.

3 participants