Skip to content

Commit

Permalink
Test for overeager hoisting in Babel plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
jsnajdr committed Feb 24, 2023
1 parent 3d7a096 commit 1f3f56a
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,59 @@ function _getJestObj() {
}
`;

exports[`babel-plugin-jest-hoist 11. jest.spyOn call on the imported module: 11. jest.spyOn call on the imported module 1`] = `
jest.mock('some-module', () => {
const module = jest.requireActual('some-module');
jest.spyOn(module, 'add');
return module;
});
↓ ↓ ↓ ↓ ↓ ↓
_getJestObj().mock('some-module', () => {
_getJestObj().spyOn(module, 'add');
const module = jest.requireActual('some-module');
return module;
});
function _getJestObj() {
const {jest} = require('@jest/globals');
_getJestObj = () => jest;
return jest;
}
`;

exports[`babel-plugin-jest-hoist 12. jest.spyOn call in class constructor: 12. jest.spyOn call in class constructor 1`] = `
jest.mock('some-module', () => {
const Actual = jest.requireActual('some-module');
return class Mocked extends Actual {
constructor() {
super();
jest.spyOn(module, 'add');
}
};
});
↓ ↓ ↓ ↓ ↓ ↓
_getJestObj().mock('some-module', () => {
const Actual = jest.requireActual('some-module');
return class Mocked extends Actual {
constructor() {
_getJestObj().spyOn(module, 'add');
super();
}
};
});
function _getJestObj() {
const {jest} = require('@jest/globals');
_getJestObj = () => jest;
return jest;
}
`;
26 changes: 26 additions & 0 deletions packages/babel-plugin-jest-hoist/src/__tests__/hoistPlugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,32 @@ pluginTester({
formatResult,
snapshot: true,
},
'jest.spyOn call on the imported module': {
code: formatResult(`
jest.mock('some-module', () => {
const module = jest.requireActual('some-module');
jest.spyOn(module, 'add');
return module;
});
`),
formatResult,
snapshot: true,
},
'jest.spyOn call in class constructor': {
code: formatResult(`
jest.mock('some-module', () => {
const Actual = jest.requireActual('some-module');
return class Mocked extends Actual {
constructor() {
super();
jest.spyOn(module, 'add');
}
};
});
`),
formatResult,
snapshot: true,
},
},
/* eslint-enable */
});

0 comments on commit 1f3f56a

Please sign in to comment.