From f850cbf8212ba5717e17a337fbc6cac14db4d67f Mon Sep 17 00:00:00 2001 From: Folke Date: Fri, 10 Jan 2020 09:38:52 +0100 Subject: [PATCH 1/3] feat(expect-expect): support chained function names (#471) --- docs/rules/expect-expect.md | 20 +++++++++++++++++ src/rules/__tests__/expect-expect.test.ts | 27 +++++++++++++++++++++++ src/rules/utils.ts | 4 ++++ 3 files changed, 51 insertions(+) diff --git a/docs/rules/expect-expect.md b/docs/rules/expect-expect.md index 6ba708903..b6ea9c1e6 100644 --- a/docs/rules/expect-expect.md +++ b/docs/rules/expect-expect.md @@ -75,3 +75,23 @@ test('returns sum', () => .run(); ); ``` + +Examples of **correct** code for deep assertion functions with the +`{ "assertFunctionNames": ["expect", "tester.foo.expect"] }` option: + +```js +/* eslint jest/expect-expect: ["error", { "assertFunctionNames": ["expect", "tester.foo.expect"] }] */ +test('nested expect method call', () => { + class Foo { + expect(k) { + return k; + } + } + let tester = { + foo: function() { + return new Foo(); + }, + }; + tester.foo().expect(123); +}); +``` diff --git a/src/rules/__tests__/expect-expect.test.ts b/src/rules/__tests__/expect-expect.test.ts index 3ef7ed9a8..5513329ac 100644 --- a/src/rules/__tests__/expect-expect.test.ts +++ b/src/rules/__tests__/expect-expect.test.ts @@ -26,6 +26,33 @@ ruleTester.run('expect-expect', rule, { code: 'it("should return undefined",() => expectSaga(mySaga).returns());', options: [{ assertFunctionNames: ['expectSaga'] }], }, + { + code: `test('verifies expect method call', () => { + class Foo { + expect(k) { + return k; + } + } + new Foo().expect(123); + });`, + options: [{ assertFunctionNames: ['Foo.expect'] }], + }, + { + code: `test('verifies deep expect method call', () => { + class Foo { + expect(k) { + return k; + } + } + let tester = { + foo: function() { + return new Foo() + } + } + tester.foo().expect(123); + });`, + options: [{ assertFunctionNames: ['tester.foo.expect'] }], + }, { code: [ 'test("verifies the function call", () => {', diff --git a/src/rules/utils.ts b/src/rules/utils.ts index 9cc2e7a03..c5b0a6eee 100644 --- a/src/rules/utils.ts +++ b/src/rules/utils.ts @@ -614,6 +614,10 @@ export function getNodeName(node: TSESTree.Node): string | null { break; case AST_NODE_TYPES.MemberExpression: return joinNames(getNodeName(node.object), getNodeName(node.property)); + case AST_NODE_TYPES.NewExpression: + return getNodeName(node.callee); + case AST_NODE_TYPES.CallExpression: + return getNodeName(node.callee); } return null; From e5a573ff64ddc24d3d7f3da73b361e0357346912 Mon Sep 17 00:00:00 2001 From: Folke Date: Fri, 10 Jan 2020 10:08:42 +0100 Subject: [PATCH 2/3] feat(expect-expect): updated docs with an example for supertest --- docs/rules/expect-expect.md | 31 ++++++++++++----------- src/rules/__tests__/expect-expect.test.ts | 19 ++++++++++++++ 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/docs/rules/expect-expect.md b/docs/rules/expect-expect.md index b6ea9c1e6..258114cad 100644 --- a/docs/rules/expect-expect.md +++ b/docs/rules/expect-expect.md @@ -76,22 +76,23 @@ test('returns sum', () => ); ``` -Examples of **correct** code for deep assertion functions with the -`{ "assertFunctionNames": ["expect", "tester.foo.expect"] }` option: +Examples of **correct** code for working with the HTTP assertions library +[SuperTest](https://www.npmjs.com/package/supertest) with the +`{ "assertFunctionNames": ["expect", "request.get.expect"] }` option: ```js -/* eslint jest/expect-expect: ["error", { "assertFunctionNames": ["expect", "tester.foo.expect"] }] */ -test('nested expect method call', () => { - class Foo { - expect(k) { - return k; - } - } - let tester = { - foo: function() { - return new Foo(); - }, - }; - tester.foo().expect(123); +/* eslint jest/expect-expect: ["error", { "assertFunctionNames": ["expect", "request.get.expect"] }] */ +const request = require('supertest'); +const express = require('express'); + +const app = express(); + +describe('GET /user', function() { + it('responds with json', function(done) { + request(app) + .get('/user') + .expect('Content-Type', /json/) + .expect(200, done); + }); }); ``` diff --git a/src/rules/__tests__/expect-expect.test.ts b/src/rules/__tests__/expect-expect.test.ts index 5513329ac..209a48505 100644 --- a/src/rules/__tests__/expect-expect.test.ts +++ b/src/rules/__tests__/expect-expect.test.ts @@ -53,6 +53,25 @@ ruleTester.run('expect-expect', rule, { });`, options: [{ assertFunctionNames: ['tester.foo.expect'] }], }, + { + code: `test('verifies recursive expect method call', () => { + class Foo { + expect(k) { + return this; + } + bar() { + return this; + } + } + let tester = { + foo: function() { + return new Foo() + } + } + tester.foo().bar().expect(456); + });`, + options: [{ assertFunctionNames: ['tester.foo.bar.expect'] }], + }, { code: [ 'test("verifies the function call", () => {', From ef4090225f42b20fe9174741c24245948ddaf589 Mon Sep 17 00:00:00 2001 From: Folke Date: Fri, 10 Jan 2020 11:13:12 +0100 Subject: [PATCH 3/3] fix: coverage for no-jasmin-globals with chained functions --- src/rules/__tests__/no-jasmine-globals.test.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/rules/__tests__/no-jasmine-globals.test.ts b/src/rules/__tests__/no-jasmine-globals.test.ts index 254c62d30..abfc0aa4e 100644 --- a/src/rules/__tests__/no-jasmine-globals.test.ts +++ b/src/rules/__tests__/no-jasmine-globals.test.ts @@ -13,6 +13,7 @@ ruleTester.run('no-jasmine-globals', rule, { 'test("foo", function () {})', 'foo()', `require('foo')('bar')`, + '(function(){})()', 'function callback(fail) { fail() }', 'var spyOn = require("actions"); spyOn("foo")', 'function callback(pending) { pending() }',