Skip to content

Commit

Permalink
fix: fix arguments delegation
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Nov 21, 2023
1 parent 5d5d0c1 commit d98562e
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 22 deletions.
4 changes: 2 additions & 2 deletions packages/vitest/src/runtime/mocker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ export class VitestMocker {
const spyModule = this.spyModule
if (!spyModule)
throw this.createError('[vitest] `spyModule` is not defined. This is Vitest error. Please open a new issue with reproduction.')
const mock = spyModule.spyOn(newContainer, property).mockImplementation(function (this: any, ...args: any[]) {
const mock = spyModule.spyOn(newContainer, property).mockImplementation(function (this: any) {
// jest reference
// https://github.com/jestjs/jest/blob/2c3d2409879952157433de215ae0eee5188a4384/packages/jest-mock/src/index.ts#L678-L691

Expand All @@ -341,7 +341,7 @@ export class VitestMocker {
// TODO: ability to restore?
// mock but delegate to original prototype method, which should be also mocked already
const original = this[key]
spyModule.spyOn(this, key).mockImplementation(() => original.apply(this, args))
spyModule.spyOn(this, key).mockImplementation((...args: any[]) => original.apply(this, args))
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/core/src/mockedE.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export class MockedE {
public doSomething(arg: string) {
public testFn(arg: string) {
return arg.repeat(2)
}
}
98 changes: 79 additions & 19 deletions test/core/test/mocked-wip.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,83 @@ test('mock each instance method separately', () => {
const instance1 = new MockedE()
const instance2 = new MockedE()
expect(instance1).not.toBe(instance2)
expect(instance1.doSomething).not.toBe(instance2.doSomething)
expect(instance1.doSomething).not.toBe(MockedE.prototype.doSomething)
expect(vi.mocked(instance1.doSomething).mock).not.toBe(vi.mocked(instance2.doSomething).mock)

// TODO: check input/output
instance1.doSomething('a')
expect(instance1.doSomething).toBeCalledTimes(1)
expect(instance2.doSomething).toBeCalledTimes(0)
expect(MockedE.prototype.doSomething).toBeCalledTimes(1)

instance2.doSomething('b')
expect(instance1.doSomething).toBeCalledTimes(1)
expect(instance2.doSomething).toBeCalledTimes(1)
expect(MockedE.prototype.doSomething).toBeCalledTimes(2)

instance1.doSomething('c')
expect(instance1.doSomething).toBeCalledTimes(2)
expect(instance2.doSomething).toBeCalledTimes(1)
expect(MockedE.prototype.doSomething).toBeCalledTimes(3)
expect(instance1.testFn).not.toBe(instance2.testFn)
expect(instance1.testFn).not.toBe(MockedE.prototype.testFn)
expect(vi.mocked(instance1.testFn).mock).not.toBe(vi.mocked(instance2.testFn).mock)

instance1.testFn('a')
expect(instance1.testFn).toBeCalledTimes(1)
expect(instance2.testFn).toBeCalledTimes(0)
expect(MockedE.prototype.testFn).toBeCalledTimes(1)
expect(vi.mocked(instance1.testFn).mock.calls).toMatchInlineSnapshot(`
[
[
"a",
],
]
`)
expect(vi.mocked(MockedE.prototype.testFn).mock.calls).toMatchInlineSnapshot(`
[
[
"a",
],
]
`)

instance2.testFn('b')
expect(instance1.testFn).toBeCalledTimes(1)
expect(instance2.testFn).toBeCalledTimes(1)
expect(MockedE.prototype.testFn).toBeCalledTimes(2)
expect(vi.mocked(instance2.testFn).mock.calls).toMatchInlineSnapshot(`
[
[
"b",
],
]
`)
expect(vi.mocked(MockedE.prototype.testFn).mock.calls).toMatchInlineSnapshot(`
[
[
"a",
],
[
"b",
],
]
`)

instance1.testFn('c')
expect(instance1.testFn).toBeCalledTimes(2)
expect(instance2.testFn).toBeCalledTimes(1)
expect(MockedE.prototype.testFn).toBeCalledTimes(3)
expect(vi.mocked(instance1.testFn).mock.calls).toMatchInlineSnapshot(`
[
[
"a",
],
[
"c",
],
]
`)
expect(vi.mocked(instance2.testFn).mock.calls).toMatchInlineSnapshot(`
[
[
"b",
],
]
`)
expect(vi.mocked(MockedE.prototype.testFn).mock.calls).toMatchInlineSnapshot(`
[
[
"a",
],
[
"b",
],
[
"c",
],
]
`)
})

0 comments on commit d98562e

Please sign in to comment.