[READ-ONLY] Mirror of https://github.com/vitest-dev/vitest. Next generation testing framework powered by Vite. vitest.dev
test testing-tools vite
12

Configure Feed

Select the types of activity you want to include in your feed.

fix: allow referring to first seen value while automocking (#1879)

authored by

Simon Abbott and committed by
GitHub
(Aug 19, 2022, 8:27 AM +0300) 285ac7c2 637e2b37

+19 -1
+4
test/core/src/mockedD.ts
··· 1 + import { MockedC } from './mockedC' 2 + 3 + export default MockedC 4 + export * from './mockedC'
+14
test/core/test/mocked.test.ts
··· 5 5 import * as mocked from '../src/mockedA' 6 6 import { mockedB } from '../src/mockedB' 7 7 import { MockedC, asyncFunc, exportedStream } from '../src/mockedC' 8 + import MockedDefault, { MockedC as MockedD } from '../src/mockedD' 8 9 import * as globalMock from '../src/global-mock' 9 10 10 11 vitest.mock('../src/submodule') 11 12 vitest.mock('virtual-module', () => ({ value: 'mock' })) 12 13 vitest.mock('../src/mockedC') 14 + vitest.mock('../src/mockedD') 13 15 14 16 test('submodule is mocked to return "two" as 3', () => { 15 17 assert.equal(3, two) ··· 37 39 test('should not delete the prototype', () => { 38 40 expect(MockedC).toBeTypeOf('function') 39 41 expect(MockedC.prototype.doSomething).toBeTypeOf('function') 42 + expect(MockedC.prototype.constructor).toBe(MockedC) 40 43 }) 41 44 42 45 test('should mock the constructor', () => { ··· 54 57 55 58 expect(MockedC.prototype.doSomething).toHaveBeenCalledOnce() 56 59 expect(MockedC.prototype.doSomething).not.toHaveReturnedWith('A') 60 + }) 61 + }) 62 + 63 + describe('default exported classes', () => { 64 + test('should preserve equality for re-exports', () => { 65 + expect(MockedDefault).toEqual(MockedD) 66 + }) 67 + 68 + test('should preserve prototype', () => { 69 + expect(MockedDefault.prototype.constructor).toBe(MockedDefault) 70 + expect(MockedD.prototype.constructor).toBe(MockedD) 57 71 }) 58 72 }) 59 73
+1 -1
packages/vitest/src/runtime/mocker.ts
··· 224 224 // Special handling of references we've seen before to prevent infinite 225 225 // recursion in circular objects. 226 226 const refId = refs.getId(value) 227 - if (refId) { 227 + if (refId !== undefined) { 228 228 finalizers.push(() => define(newContainer, property, refs.getMockedValue(refId))) 229 229 continue 230 230 }