Skip to content

Commit beb1aec

Browse files
Folke LemaitreG-Rath
Folke Lemaitre
authored andcommitted
feat(expect-expect): support chained function names (#471) (#508)
* feat(expect-expect): support chained function names (#471) * feat(expect-expect): updated docs with an example for supertest * fix: coverage for no-jasmin-globals with chained functions
1 parent ac7fa48 commit beb1aec

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

docs/rules/expect-expect.md

+21
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,24 @@ test('returns sum', () =>
7575
.run();
7676
);
7777
```
78+
79+
Examples of **correct** code for working with the HTTP assertions library
80+
[SuperTest](https://www.npmjs.com/package/supertest) with the
81+
`{ "assertFunctionNames": ["expect", "request.get.expect"] }` option:
82+
83+
```js
84+
/* eslint jest/expect-expect: ["error", { "assertFunctionNames": ["expect", "request.get.expect"] }] */
85+
const request = require('supertest');
86+
const express = require('express');
87+
88+
const app = express();
89+
90+
describe('GET /user', function() {
91+
it('responds with json', function(done) {
92+
request(app)
93+
.get('/user')
94+
.expect('Content-Type', /json/)
95+
.expect(200, done);
96+
});
97+
});
98+
```

src/rules/__tests__/expect-expect.test.ts

+46
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,52 @@ ruleTester.run('expect-expect', rule, {
2727
code: 'it("should return undefined",() => expectSaga(mySaga).returns());',
2828
options: [{ assertFunctionNames: ['expectSaga'] }],
2929
},
30+
{
31+
code: `test('verifies expect method call', () => {
32+
class Foo {
33+
expect(k) {
34+
return k;
35+
}
36+
}
37+
new Foo().expect(123);
38+
});`,
39+
options: [{ assertFunctionNames: ['Foo.expect'] }],
40+
},
41+
{
42+
code: `test('verifies deep expect method call', () => {
43+
class Foo {
44+
expect(k) {
45+
return k;
46+
}
47+
}
48+
let tester = {
49+
foo: function() {
50+
return new Foo()
51+
}
52+
}
53+
tester.foo().expect(123);
54+
});`,
55+
options: [{ assertFunctionNames: ['tester.foo.expect'] }],
56+
},
57+
{
58+
code: `test('verifies recursive expect method call', () => {
59+
class Foo {
60+
expect(k) {
61+
return this;
62+
}
63+
bar() {
64+
return this;
65+
}
66+
}
67+
let tester = {
68+
foo: function() {
69+
return new Foo()
70+
}
71+
}
72+
tester.foo().bar().expect(456);
73+
});`,
74+
options: [{ assertFunctionNames: ['tester.foo.bar.expect'] }],
75+
},
3076
{
3177
code: [
3278
'test("verifies the function call", () => {',

src/rules/__tests__/no-jasmine-globals.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ ruleTester.run('no-jasmine-globals', rule, {
1313
'test("foo", function () {})',
1414
'foo()',
1515
`require('foo')('bar')`,
16+
'(function(){})()',
1617
'function callback(fail) { fail() }',
1718
'var spyOn = require("actions"); spyOn("foo")',
1819
'function callback(pending) { pending() }',

src/rules/utils.ts

+4
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,10 @@ export function getNodeName(node: TSESTree.Node): string | null {
614614
break;
615615
case AST_NODE_TYPES.MemberExpression:
616616
return joinNames(getNodeName(node.object), getNodeName(node.property));
617+
case AST_NODE_TYPES.NewExpression:
618+
return getNodeName(node.callee);
619+
case AST_NODE_TYPES.CallExpression:
620+
return getNodeName(node.callee);
617621
}
618622

619623
return null;

0 commit comments

Comments
 (0)