Skip to content

Commit

Permalink
feat(expect-expect): support chained function names (jest-community#471)
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Jan 10, 2020
1 parent 6f314e1 commit f850cbf
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
20 changes: 20 additions & 0 deletions docs/rules/expect-expect.md
Expand Up @@ -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);
});
```
27 changes: 27 additions & 0 deletions src/rules/__tests__/expect-expect.test.ts
Expand Up @@ -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", () => {',
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 f850cbf

Please sign in to comment.