From d9a79b23ed990bada8a9fbbf4e331583f78ab0ca Mon Sep 17 00:00:00 2001 From: Chris Blossom Date: Mon, 12 Aug 2019 16:27:31 -0700 Subject: [PATCH] fix(prefer-spy-on): do not change behavior of fixed instances (#390) Fixes #389 --- src/rules/__tests__/prefer-spy-on.test.ts | 11 ++++++----- src/rules/prefer-spy-on.ts | 2 +- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/rules/__tests__/prefer-spy-on.test.ts b/src/rules/__tests__/prefer-spy-on.test.ts index 2bf82f79e..b95d13e2d 100644 --- a/src/rules/__tests__/prefer-spy-on.test.ts +++ b/src/rules/__tests__/prefer-spy-on.test.ts @@ -31,7 +31,7 @@ ruleTester.run('prefer-spy-on', rule, { type: AST_NODE_TYPES.AssignmentExpression, }, ], - output: "jest.spyOn(obj, 'a'); const test = 10;", + output: "jest.spyOn(obj, 'a').mockImplementation(); const test = 10;", }, { code: "Date['now'] = jest['fn']()", @@ -41,7 +41,7 @@ ruleTester.run('prefer-spy-on', rule, { type: AST_NODE_TYPES.AssignmentExpression, }, ], - output: "jest.spyOn(Date, 'now')", + output: "jest.spyOn(Date, 'now').mockImplementation()", }, { code: 'window[`${name}`] = jest[`fn`]()', @@ -51,7 +51,7 @@ ruleTester.run('prefer-spy-on', rule, { type: AST_NODE_TYPES.AssignmentExpression, }, ], - output: 'jest.spyOn(window, `${name}`)', + output: 'jest.spyOn(window, `${name}`).mockImplementation()', }, { code: "obj['prop' + 1] = jest['fn']()", @@ -61,7 +61,7 @@ ruleTester.run('prefer-spy-on', rule, { type: AST_NODE_TYPES.AssignmentExpression, }, ], - output: "jest.spyOn(obj, 'prop' + 1)", + output: "jest.spyOn(obj, 'prop' + 1).mockImplementation()", }, { code: 'obj.one.two = jest.fn(); const test = 10;', @@ -71,7 +71,8 @@ ruleTester.run('prefer-spy-on', rule, { type: AST_NODE_TYPES.AssignmentExpression, }, ], - output: "jest.spyOn(obj.one, 'two'); const test = 10;", + output: + "jest.spyOn(obj.one, 'two').mockImplementation(); const test = 10;", }, { code: 'obj.a = jest.fn(() => 10)', diff --git a/src/rules/prefer-spy-on.ts b/src/rules/prefer-spy-on.ts index 5a6fff6a4..247704fce 100644 --- a/src/rules/prefer-spy-on.ts +++ b/src/rules/prefer-spy-on.ts @@ -79,7 +79,7 @@ export default createRule({ const argSource = arg && context.getSourceCode().getText(arg); const mockImplementation = argSource ? `.mockImplementation(${argSource})` - : ''; + : '.mockImplementation()'; return [ fixer.insertTextBefore(left, `jest.spyOn(`),