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

Sinon: add support for transforming callsFake #500

Merged
merged 1 commit into from
Mar 22, 2023
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
12 changes: 9 additions & 3 deletions src/transformers/sinon.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,11 @@ describe('spies and stubs', () => {

const stub = sinon.stub(Api, 'get');
const putOk = sinon.stub(Api, 'put').returns(200)
sinon.stub();
sinon.stub().returns(Promise.resolve());
sinon.stub(I18n); // unsupported
sinon.stub(I18n, 'extend');
sinon.stub(I18n, 'extend').callsFake(fn => fn);
sinon.stub(AirbnbUser, 'current').returnsArg(1);
sinon.stub(a, b, () => c, d) // too many args

Expand All @@ -69,8 +72,11 @@ describe('spies and stubs', () => {
`
const stub = jest.spyOn(Api, 'get').mockClear().mockImplementation();
const putOk = jest.spyOn(Api, 'put').mockClear().mockReturnValue(200)
jest.fn();
jest.fn().mockReturnValue(Promise.resolve());
sinon.stub(I18n); // unsupported
jest.spyOn(I18n, 'extend').mockClear().mockImplementation();
jest.spyOn(I18n, 'extend').mockClear().mockImplementation(fn => fn);
jest.spyOn(AirbnbUser, 'current').mockClear().mockImplementation((...args) => args[1]);
jest.spyOn(a, b).mockClear().mockImplementation(() => c) // too many args

Expand All @@ -81,9 +87,9 @@ describe('spies and stubs', () => {
`,
{
warnings: [
'jest-codemods warning: (test.js line 6) stubbing all methods in an object is not supported; stub each one you care about individually',
'jest-codemods warning: (test.js line 9) 4+ arguments found in sinon.stub call; did you mean to use this many?',
'jest-codemods warning: (test.js line 14) 4+ arguments found in sinon.spy call; did you mean to use this many?',
'jest-codemods warning: (test.js line 8) stubbing all methods in an object is not supported; stub each one you care about individually',
'jest-codemods warning: (test.js line 12) 4+ arguments found in sinon.stub call; did you mean to use this many?',
'jest-codemods warning: (test.js line 17) 4+ arguments found in sinon.spy call; did you mean to use this many?',
],
}
)
Expand Down
15 changes: 15 additions & 0 deletions src/transformers/sinon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,21 @@ function transformStub(j, ast, sinonExpression, logWarning) {
findParentOfType(np, j.VariableDeclaration.name) ||
findParentOfType(np, j.ExpressionStatement.name)

const callsFake = j(parent).find(j.CallExpression, {
callee: {
type: 'MemberExpression',
property: { type: 'Identifier', name: 'callsFake' },
},
})
const hasCallsFake = callsFake.size() > 0

if (hasCallsFake) {
callsFake.forEach((np) => {
np.node.callee.property.name = 'mockImplementation'
})
return spyOn
}

const hasReturn =
j(parent)
.find(j.CallExpression, {
Expand Down