-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Closed
Labels
enhancementNew feature or requestNew feature or requestp2-nice-to-haveNot breaking anything but nice to have (priority)Not breaking anything but nice to have (priority)pr welcome
Description
Clear and concise description of the problem
Jest added support for testing function context ("this") in Mock functions in jest@28.x
PR jestjs/jest#11235
Docs: https://jestjs.io/docs/mock-function-api#mockfnmockcontexts
It would be useful to have this in vitest too.
Suggested solution
mock.context is added in vitest, so that the following test code can be written
test("contexts", () => {
const mockFn = vi.fn();
expect(mockFn.mock.contexts).toHaveLength(0);
const bindContext = { x : 1 };
const boundMockFn = mockFn.bind(bindContext);
boundMockFn();
expect(mockFn.mock.contexts).toHaveLength(1);
expect(mockFn.mock.contexts[0]).toBe(bindContext);
const callContext = { x : 2 };
mockFn.call(callContext);
expect(mockFn.mock.contexts).toHaveLength(2);
expect(mockFn.mock.contexts[1]).toBe(callContext);
const applyContext = { x : 3 };
mockFn.apply(applyContext);
expect(mockFn.mock.contexts).toHaveLength(3);
expect(mockFn.mock.contexts[2]).toBe(applyContext);
})Alternative
Skip adding the support for context.
Additional context
This issue was noticed while adding support for transformation of jest to vi API in trivikr/vitest-codemod#80
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that request the same feature to avoid creating a duplicate.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or requestp2-nice-to-haveNot breaking anything but nice to have (priority)Not breaking anything but nice to have (priority)pr welcome
Type
Projects
Status
Approved