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
Comments
Thanks for your mention, I'd like to take this when it adds the label. |
@sheremet-va Would If yes, can you add |
I do like the API, but I don't like that |
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();
}); |
@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. |
The vitest-codemod will transform replaceProperty with stubEnv for process.env |
Clear and concise description of the problem
Jest introduced API
replaceProperty
in v29.4.0This API is also available in sinon
https://sinonjs.org/releases/v14/sandbox/#sandboxreplaceobject-property-replacement
Example code tested with jest@29.4.2:
Suggested solution
Add
vi.replaceProprety
APIAlternative
No response
Additional context
No response
Validations
The text was updated successfully, but these errors were encountered: