From 4188b841a16dd7989866a14d40277cb72508d869 Mon Sep 17 00:00:00 2001 From: rbnayax <92677232+rbnayax@users.noreply.github.com> Date: Mon, 11 Nov 2024 21:33:11 +0200 Subject: [PATCH] =?UTF-8?q?fix(ts-jest):=20ts-jest:=20toPrimitive=20should?= =?UTF-8?q?=20not=20be=20set=20on=20proxies,=20but=20=E2=80=A6=20(#879)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(ts-jest): ts-jest: toPrimitive should not be set on proxies, but mocked when needed * fix(ts-jest): ts-jest: toPrimitive should not be set on proxies, but mocked when needed * fix(ts-jest): ts-jest: toPrimitive should not be set on proxies, but mocked when needed * fix(ts-jest): ts-jest: toPrimitive should not be set on proxies, but mocked when needed * fix(ts-jest): ts-jest: toPrimitive should not be set on proxies, but mocked when needed --- packages/testing/ts-jest/src/mocks.spec.ts | 24 ++++++++++++++-------- packages/testing/ts-jest/src/mocks.ts | 13 ------------ 2 files changed, 16 insertions(+), 21 deletions(-) diff --git a/packages/testing/ts-jest/src/mocks.spec.ts b/packages/testing/ts-jest/src/mocks.spec.ts index 030f90495..bc1ddb1cc 100644 --- a/packages/testing/ts-jest/src/mocks.spec.ts +++ b/packages/testing/ts-jest/src/mocks.spec.ts @@ -226,24 +226,32 @@ describe('Mocks', () => { expect(mock.toString()).toEqual('[object Object]'); expect(mock.nested.toString()).toEqual('function () { [native code] }'); }); - it('nested properties can be implictly casted to string', () => { + + it('nested properties should equal its partial', () => { + const mock = createMock({ foo: { bar: 1 } }); + expect({ mock }).toEqual({ mock: { foo: { bar: 1 } } }); + expect({ foo: mock.foo }).toEqual({ foo: { bar: 1 } }); + }); + + it('nested properties can not be implictly casted to string/number', () => { const mock = createMock<{ nested: any }>(); const testFnNumber = () => mock.nested > 0; const testFnString = () => `${mock.nested}`; - expect(testFnNumber).not.toThrowError(); - expect(testFnString).not.toThrowError(); + expect(testFnNumber).toThrowError(); + expect(testFnString).toThrowError(); }); - it('mocked functions returned values can be implictly casted to string', async () => { + + it('mocked functions returned values can not be implictly casted to string/number', async () => { const mock = createMock(); const result = await mock.func3(); const testFnNumber = () => result.prop > 0; const testFnString = () => `${result.prop}`; - expect(testFnNumber).not.toThrowError(); - expect(testFnString).not.toThrowError(); + expect(testFnNumber).toThrowError(); + expect(testFnString).toThrowError(); }); it('asymmetricMatch should not be set', () => { @@ -377,7 +385,7 @@ describe('Mocks', () => { mockedProvider = module.get>(diToken); dependentProvider = module.get<{ dependent: () => string }>( - dependentToken + dependentToken, ); }); @@ -389,7 +397,7 @@ describe('Mocks', () => { mockedProvider.switchToHttp.mockReturnValueOnce( createMock({ getRequest: () => request, - }) + }), ); const mockResult = mockedProvider.switchToHttp().getRequest(); diff --git a/packages/testing/ts-jest/src/mocks.ts b/packages/testing/ts-jest/src/mocks.ts index 9d66c5327..14e147d77 100644 --- a/packages/testing/ts-jest/src/mocks.ts +++ b/packages/testing/ts-jest/src/mocks.ts @@ -85,19 +85,6 @@ const createProxy: { mockedProp = createProxy(`${name}.${propName}`); } - // Add Symbol.toPrimitive to preserve implicit conversion to primitive types - if (typeof mockedProp === 'object' || typeof mockedProp === 'function') { - mockedProp[Symbol.toPrimitive] = (hint) => { - if (hint === 'string') { - return 'mocked'; - } - if (hint === 'number') { - return 0; - } - throw new TypeError(); - }; - } - cache.set(prop, mockedProp); return mockedProp; },