Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion packages/testing/ts-jest/src/mocks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,28 @@ interface TestInterface {
func: (num: number, str: string) => boolean;
func2: (entity: TestClass) => void;
func3: () => Promise<{ prop: number }>;
nested: {
someOtherNum: number;
func4: () => boolean;
};
}

class TestClass {
someProperty!: number;

nested = new NestedTestClass();

someMethod() {
return 42;
}
}

class NestedTestClass {
someOtherMethod() {
return 24;
}
}

describe('Mocks', () => {
const request = {
headers: {
Expand Down Expand Up @@ -200,6 +212,22 @@ describe('Mocks', () => {
expect(serviceMock.foo()).toEqual(false);
expect(serviceMock.foo()).toEqual(true);
});

it('should work with nested properties and functions', () => {
const mock = createMock<TestInterface>();
mock.nested.someOtherNum = 99;
mock.nested.func4.mockReturnValueOnce(true);
const result = mock.nested.func4();
expect(mock.nested.someOtherNum).toBe(99);
expect(result).toBe(true);
});

it('should work with classes having nested properties', () => {
const mock = createMock<TestClass>();
mock.nested.someOtherMethod.mockReturnValueOnce(99);
const result = mock.nested.someOtherMethod();
expect(result).toBe(99);
});
});

describe('auto mocked', () => {
Expand Down Expand Up @@ -360,7 +388,9 @@ describe('Mocks', () => {
it('should throw error when calling unstubbed method in strict mode', () => {
const mock = createMock<TestInterface>({}, { strict: true });

expect(() => mock.func(1, 'test')).toThrow('Method mock.func was called without being explicitly stubbed');
expect(() => mock.func(1, 'test')).toThrow(
'Method mock.func was called without being explicitly stubbed',
);

mock.func.mockReturnValue(true);
expect(mock.func(1, 'test')).toBe(true);
Expand Down
12 changes: 9 additions & 3 deletions packages/testing/ts-jest/src/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export type DeepMocked<T> = {
[K in keyof T]: Required<T>[K] extends (...args: any[]) => infer U
? jest.MockInstance<ReturnType<Required<T>[K]>, jest.ArgsType<T[K]>> &
((...args: jest.ArgsType<T[K]>) => DeepMocked<U>)
: T[K];
: DeepMocked<T[K]>;
} & T;

const jestFnProps = new Set([
Expand Down Expand Up @@ -49,7 +49,11 @@ const jestFnProps = new Set([
const createProxy: {
<T extends object>(name: string, strict: boolean, base: T): T;
<T extends Mock<any, any> = Mock<any, any>>(name: string, strict: boolean): T;
} = <T extends object | Mock<any, any>>(name: string, strict: boolean, base?: T): T => {
} = <T extends object | Mock<any, any>>(
name: string,
strict: boolean,
base?: T,
): T => {
const cache = new Map<string | number | symbol, any>();
const handler: ProxyHandler<T> = {
get: (obj, prop, receiver) => {
Expand Down Expand Up @@ -105,7 +109,9 @@ const createProxy: {
return result;
} else {
if (strict) {
throw new Error(`Method ${name} was called without being explicitly stubbed`);
throw new Error(
`Method ${name} was called without being explicitly stubbed`,
);
}
if (!cache.has('__apply')) {
cache.set('__apply', createProxy(name, strict));
Expand Down