Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(expect-expect): support chained function names (#471) #508

Merged
merged 3 commits into from Jan 10, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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', () => {
folke marked this conversation as resolved.
Show resolved Hide resolved
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