Skip to content
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- `[expect, jest-message-util, jest-pattern, jest-regex-util, jest-util]` Revert `node:` protocol imports to restore webpack/browser-bundle compatibility ([#16167](https://github.com/jestjs/jest/pull/16167))
- `[expect]` Widen `toMatchObject` and `objectContaining` parameter type from `Record<string, unknown>` to `object` so class instances are accepted ([#16196](https://github.com/jestjs/jest/pull/16196))
- `[@jest-environment/jsdom-abstract]` Make `@types/jsdom` a peer dependency ([#16166](https://github.com/jestjs/jest/pull/16166))
- `[jest-mock]` Type constructed mocked class instances returned by `ModuleMocker.generateFromMetadata()` as mocked objects ([#16212](https://github.com/jestjs/jest/pull/16212))
- `[jest-runtime]` Fall back to native ESM when a `.js` file contains ESM syntax but has no `"type":"module"` marker ([#16152](https://github.com/jestjs/jest/pull/16152))
- `[jest-runtime]` Support older test environments whose `moduleMocker` does not implement `clearMocksOnScope` ([#16169](https://github.com/jestjs/jest/pull/16169))

Expand Down
17 changes: 16 additions & 1 deletion packages/jest-mock/__typetests__/ModuleMocker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
*/

import {expect, test} from 'tstyche';
import {type MockMetadata, type Mocked, ModuleMocker} from 'jest-mock';
import {
type MockMetadata,
type Mocked,
type MockedObject,
ModuleMocker,
} from 'jest-mock';

class ExampleClass {
memberA: Array<number>;
Expand Down Expand Up @@ -79,3 +84,13 @@ test('generateFromMetadata', () => {
expect(exampleMock.propertyE).type.toBe<boolean>();
expect(exampleMock.propertyF).type.toBe<symbol>();
});

test('generateFromMetadata constructs mocked class instances', () => {
const classMetadata = moduleMocker.getMetadata(ExampleClass);
const MockClass = moduleMocker.generateFromMetadata(classMetadata!);

const instance = new MockClass();

expect(instance).type.toBeAssignableTo<MockedObject<ExampleClass>>();
expect(instance.memberB.mock.calls).type.toBe<Array<[]>>();
});
1 change: 1 addition & 0 deletions packages/jest-mock/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export type PropertyLikeKeys<T> = Exclude<
export type MockedClass<T extends ClassLike> = MockInstance<
(...args: ConstructorParameters<T>) => Mocked<InstanceType<T>>
> &
(new (...args: ConstructorParameters<T>) => Mocked<InstanceType<T>>) &
MockedObject<T>;

export type MockedFunction<T extends FunctionLike> = MockInstance<T> &
Expand Down
Loading