Skip to content

Commit

Permalink
fix(ts-jest): ts-jest: toPrimitive should not be set on proxies, but … (
Browse files Browse the repository at this point in the history
#879)

* 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
  • Loading branch information
rbnayax authored Nov 11, 2024
1 parent 7f8404a commit 4188b84
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 21 deletions.
24 changes: 16 additions & 8 deletions packages/testing/ts-jest/src/mocks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>({ 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<TestInterface>();
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', () => {
Expand Down Expand Up @@ -377,7 +385,7 @@ describe('Mocks', () => {

mockedProvider = module.get<DeepMocked<ExecutionContext>>(diToken);
dependentProvider = module.get<{ dependent: () => string }>(
dependentToken
dependentToken,
);
});

Expand All @@ -389,7 +397,7 @@ describe('Mocks', () => {
mockedProvider.switchToHttp.mockReturnValueOnce(
createMock<HttpArgumentsHost>({
getRequest: () => request,
})
}),
);

const mockResult = mockedProvider.switchToHttp().getRequest();
Expand Down
13 changes: 0 additions & 13 deletions packages/testing/ts-jest/src/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
Expand Down

0 comments on commit 4188b84

Please sign in to comment.