Skip to content

Commit

Permalink
feat(expect-expect): support chained function names (#471) (#508)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Folke Lemaitre authored and G-Rath committed Jan 10, 2020
1 parent ac7fa48 commit beb1aec
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
21 changes: 21 additions & 0 deletions docs/rules/expect-expect.md
Expand Up @@ -75,3 +75,24 @@ test('returns sum', () =>
.run();
);
```

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", "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);
});
});
```
46 changes: 46 additions & 0 deletions src/rules/__tests__/expect-expect.test.ts
Expand Up @@ -27,6 +27,52 @@ 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 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", () => {',
Expand Down
1 change: 1 addition & 0 deletions src/rules/__tests__/no-jasmine-globals.test.ts
Expand Up @@ -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() }',
Expand Down
4 changes: 4 additions & 0 deletions src/rules/utils.ts
Expand Up @@ -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;
Expand Down

0 comments on commit beb1aec

Please sign in to comment.