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; },