Skip to content

Commit 295ca9a

Browse files
folkeG-Rath
authored andcommitted
feat(expect-expect): support glob patterns for assertFunctionNames (#509)
1 parent 7338362 commit 295ca9a

File tree

5 files changed

+38
-5
lines changed

5 files changed

+38
-5
lines changed

docs/rules/expect-expect.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ it('should work with callbacks/async', () => {
4242

4343
### `assertFunctionNames`
4444

45-
This array option whitelists the assertion function names to look for.
45+
This array option whitelists the assertion function names to look for. Function
46+
names can be a glob pattern like `request.*.expect` (see
47+
[micromatch](https://github.com/micromatch/micromatch) for syntax)
4648

4749
Examples of **incorrect** code for the `{ "assertFunctionNames": ["expect"] }`
4850
option:
@@ -78,10 +80,10 @@ test('returns sum', () =>
7880

7981
Examples of **correct** code for working with the HTTP assertions library
8082
[SuperTest](https://www.npmjs.com/package/supertest) with the
81-
`{ "assertFunctionNames": ["expect", "request.get.expect"] }` option:
83+
`{ "assertFunctionNames": ["expect", "request.*.expect"] }` option:
8284

8385
```js
84-
/* eslint jest/expect-expect: ["error", { "assertFunctionNames": ["expect", "request.get.expect"] }] */
86+
/* eslint jest/expect-expect: ["error", { "assertFunctionNames": ["expect", "request.*.expect"] }] */
8587
const request = require('supertest');
8688
const express = require('express');
8789

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
"typecheck": "tsc -p ."
3838
},
3939
"dependencies": {
40-
"@typescript-eslint/experimental-utils": "^2.5.0"
40+
"@typescript-eslint/experimental-utils": "^2.5.0",
41+
"micromatch": "^4.0.2"
4142
},
4243
"devDependencies": {
4344
"@babel/cli": "^7.4.4",
@@ -50,6 +51,7 @@
5051
"@semantic-release/git": "^7.0.17",
5152
"@types/eslint": "^6.1.3",
5253
"@types/jest": "^24.0.15",
54+
"@types/micromatch": "^4.0.0",
5355
"@types/node": "^12.6.6",
5456
"@typescript-eslint/eslint-plugin": "^2.5.0",
5557
"@typescript-eslint/parser": "^2.5.0",

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

+16
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,22 @@ ruleTester.run('expect-expect', rule, {
5454
});`,
5555
options: [{ assertFunctionNames: ['tester.foo.expect'] }],
5656
},
57+
{
58+
code: `test('wildcard chained function', () => {
59+
class Foo {
60+
expect(k) {
61+
return k;
62+
}
63+
}
64+
let tester = {
65+
foo: function() {
66+
return new Foo()
67+
}
68+
}
69+
tester.foo().expect(123);
70+
});`,
71+
options: [{ assertFunctionNames: ['tester.*.expect'] }],
72+
},
5773
{
5874
code: `test('verifies recursive expect method call', () => {
5975
class Foo {

src/rules/expect-expect.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
AST_NODE_TYPES,
88
TSESTree,
99
} from '@typescript-eslint/experimental-utils';
10+
import micromatch from 'micromatch';
1011
import {
1112
TestCaseName,
1213
createRule,
@@ -74,7 +75,7 @@ export default createRule<
7475
const name = getNodeName(node.callee);
7576
if (name === TestCaseName.it || name === TestCaseName.test) {
7677
unchecked.push(node);
77-
} else if (name && assertFunctionNames.includes(name)) {
78+
} else if (name && micromatch.isMatch(name, assertFunctionNames)) {
7879
// Return early in case of nested `it` statements.
7980
checkCallExpressionUsed(context.getAncestors());
8081
}

yarn.lock

+12
Original file line numberDiff line numberDiff line change
@@ -1221,6 +1221,11 @@
12211221
dependencies:
12221222
"@babel/types" "^7.3.0"
12231223

1224+
"@types/braces@*":
1225+
version "3.0.0"
1226+
resolved "https://registry.yarnpkg.com/@types/braces/-/braces-3.0.0.tgz#7da1c0d44ff1c7eb660a36ec078ea61ba7eb42cb"
1227+
integrity sha512-TbH79tcyi9FHwbyboOKeRachRq63mSuWYXOflsNO9ZyE5ClQ/JaozNKl+aWUq87qPNsXasXxi2AbgfwIJ+8GQw==
1228+
12241229
"@types/color-name@^1.1.1":
12251230
version "1.1.1"
12261231
resolved "https://registry.yarnpkg.com/@types/color-name/-/color-name-1.1.1.tgz#1c1261bbeaa10a8055bbc5d8ab84b7b2afc846a0"
@@ -1290,6 +1295,13 @@
12901295
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.4.tgz#38fd73ddfd9b55abb1e1b2ed578cb55bd7b7d339"
12911296
integrity sha512-8+KAKzEvSUdeo+kmqnKrqgeE+LcA0tjYWFY7RPProVYwnqDjukzO+3b6dLD56rYX5TdWejnEOLJYOIeh4CXKuA==
12921297

1298+
"@types/micromatch@^4.0.0":
1299+
version "4.0.0"
1300+
resolved "https://registry.yarnpkg.com/@types/micromatch/-/micromatch-4.0.0.tgz#c57e0b11518c930465ce923f44342e1bb4bef309"
1301+
integrity sha512-bavSCssCRRlbUI639WG0Y30AOowkI5CdxyyrC5eVbsb0BJIbgS5ROfwlwDYHsOmgS59iYlre9sstIA5wfVNKBA==
1302+
dependencies:
1303+
"@types/braces" "*"
1304+
12931305
"@types/minimatch@*":
12941306
version "3.0.3"
12951307
resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.3.tgz#3dca0e3f33b200fc7d1139c0cd96c1268cadfd9d"

0 commit comments

Comments
 (0)