Skip to content

Commit

Permalink
fix(mock): consider modules as objects (#13513)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tofandel committed Oct 26, 2022
1 parent df38770 commit 81390ac
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
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

0 comments on commit 81390ac

Please sign in to comment.