Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(mock): consider modules as objects #13513

Merged
merged 4 commits into from Oct 26, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,8 @@

### Fixes

- `[jest-mock]` Treat cjs modules as objects so they can be mocked ([#13513](https://github.com/facebook/jest/pull/13513))

### Chore & Maintenance

- `[@jest/transform]` Update `convert-source-map` ([#13509](https://github.com/facebook/jest/pull/13509))
Expand Down
14 changes: 14 additions & 0 deletions packages/jest-mock/src/__tests__/index.test.ts
Expand Up @@ -213,6 +213,20 @@ describe('moduleMocker', () => {
expect(mock.enumGetter).toBeDefined();
});

it('handles custom toString of transpiled modules', () => {
const foo = Object.defineProperties(
{foo: 'bar'},
{
__esModule: {value: true},
[Symbol.toStringTag]: {value: 'Module'},
},
);
const mock = moduleMocker.generateFromMetadata(
moduleMocker.getMetadata(foo),
);
expect(mock.foo).toBeDefined();
});

it('mocks ES2015 non-enumerable methods', () => {
class ClassFoo {
foo() {}
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-mock/src/index.ts
Expand Up @@ -426,7 +426,7 @@ function getType(ref?: unknown): MockMetadataType | null {
return 'function';
} else if (Array.isArray(ref)) {
return 'array';
} else if (typeName === 'Object') {
} else if (typeName === 'Object' || typeName === 'Module') {
return 'object';
} else if (
typeName === 'Number' ||
Expand Down