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(no-restricted-matchers): match based on start of chain, requiring each permutation to be set #1218

Merged
merged 1 commit into from
Aug 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions docs/rules/no-restricted-matchers.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ alternatives.
Bans are expressed in the form of a map, with the value being either a string
message to be shown, or `null` if the default rule message should be used.

Both matchers, modifiers, and chains of the two are checked, allowing for
specific variations of a matcher to be banned if desired.
Bans are checked against the start of the `expect` chain - this means that to
ban a specific matcher entirely you must specify all six permutations, but
allows you to ban modifiers as well.

By default, this map is empty, meaning no matchers or modifiers are banned.

Expand All @@ -22,7 +23,12 @@ For example:
{
"toBeFalsy": null,
"resolves": "Use `expect(await promise)` instead.",
"not.toHaveBeenCalledWith": null
"toHaveBeenCalledWith": null,
"not.toHaveBeenCalledWith": null,
"resolves.toHaveBeenCalledWith": null,
"rejects.toHaveBeenCalledWith": null,
"resolves.not.toHaveBeenCalledWith": null,
"rejects.not.toHaveBeenCalledWith": null
}
]
}
Expand All @@ -32,15 +38,18 @@ Examples of **incorrect** code for this rule with the above configuration

```js
it('is false', () => {
// if this has a modifer (i.e. `not.toBeFalsy`), it would be considered fine
expect(a).toBeFalsy();
});

it('resolves', async () => {
// all uses of this modifier are disallowed, regardless of matcher
await expect(myPromise()).resolves.toBe(true);
});

describe('when an error happens', () => {
it('does not upload the file', async () => {
// all uses of this matcher are disallowed
expect(uploadFileMock).not.toHaveBeenCalledWith('file.name');
});
});
Expand Down
83 changes: 36 additions & 47 deletions src/rules/__tests__/no-restricted-matchers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,26 @@ ruleTester.run('no-restricted-matchers', rule, {
code: 'expect(a)["toBe"](b)',
options: [{ 'not.toBe': null }],
},
{
code: 'expect(a).resolves.not.toBe(b)',
options: [{ not: null }],
},
{
code: 'expect(a).resolves.not.toBe(b)',
options: [{ 'not.toBe': null }],
},
{
code: "expect(uploadFileMock).resolves.toHaveBeenCalledWith('file.name')",
options: [
{ 'not.toHaveBeenCalledWith': 'Use not.toHaveBeenCalled instead' },
],
},
{
code: "expect(uploadFileMock).resolves.not.toHaveBeenCalledWith('file.name')",
options: [
{ 'not.toHaveBeenCalledWith': 'Use not.toHaveBeenCalled instead' },
],
},
],
invalid: [
{
Expand All @@ -47,7 +67,7 @@ ruleTester.run('no-restricted-matchers', rule, {
messageId: 'restrictedChain',
data: {
message: null,
chain: 'toBe',
restriction: 'toBe',
},
column: 11,
line: 1,
Expand All @@ -62,7 +82,7 @@ ruleTester.run('no-restricted-matchers', rule, {
messageId: 'restrictedChain',
data: {
message: null,
chain: 'toBe',
restriction: 'toBe',
},
column: 11,
line: 1,
Expand All @@ -77,7 +97,7 @@ ruleTester.run('no-restricted-matchers', rule, {
messageId: 'restrictedChain',
data: {
message: null,
chain: 'not',
restriction: 'not',
},
column: 11,
line: 1,
Expand All @@ -92,7 +112,7 @@ ruleTester.run('no-restricted-matchers', rule, {
messageId: 'restrictedChain',
data: {
message: null,
chain: 'not',
restriction: 'not',
},
column: 11,
line: 1,
Expand All @@ -107,28 +127,13 @@ ruleTester.run('no-restricted-matchers', rule, {
messageId: 'restrictedChain',
data: {
message: null,
chain: 'resolves',
restriction: 'resolves',
},
column: 11,
line: 1,
},
],
},
{
code: 'expect(a).resolves.not.toBe(b)',
options: [{ not: null }],
errors: [
{
messageId: 'restrictedChain',
data: {
message: null,
chain: 'not',
},
column: 20,
line: 1,
},
],
},
{
code: 'expect(a).resolves.not.toBe(b)',
options: [{ resolves: null }],
Expand All @@ -137,7 +142,7 @@ ruleTester.run('no-restricted-matchers', rule, {
messageId: 'restrictedChain',
data: {
message: null,
chain: 'resolves',
restriction: 'resolves',
},
column: 11,
line: 1,
Expand All @@ -152,29 +157,13 @@ ruleTester.run('no-restricted-matchers', rule, {
messageId: 'restrictedChain',
data: {
message: null,
chain: 'resolves.not',
restriction: 'resolves.not',
},
column: 11,
line: 1,
},
],
},
{
code: 'expect(a).resolves.not.toBe(b)',
options: [{ 'not.toBe': null }],
errors: [
{
messageId: 'restrictedChain',
data: {
message: null,
chain: 'not.toBe',
},
endColumn: 28,
column: 20,
line: 1,
},
],
},
{
code: 'expect(a).not.toBe(b)',
options: [{ 'not.toBe': null }],
Expand All @@ -183,7 +172,7 @@ ruleTester.run('no-restricted-matchers', rule, {
messageId: 'restrictedChain',
data: {
message: null,
chain: 'not.toBe',
restriction: 'not.toBe',
},
endColumn: 19,
column: 11,
Expand All @@ -199,7 +188,7 @@ ruleTester.run('no-restricted-matchers', rule, {
messageId: 'restrictedChain',
data: {
message: null,
chain: 'resolves.not.toBe',
restriction: 'resolves.not.toBe',
},
endColumn: 28,
column: 11,
Expand All @@ -215,7 +204,7 @@ ruleTester.run('no-restricted-matchers', rule, {
messageId: 'restrictedChainWithMessage',
data: {
message: 'Prefer `toStrictEqual` instead',
chain: 'toBe',
restriction: 'toBe',
},
column: 11,
line: 1,
Expand All @@ -234,25 +223,25 @@ ruleTester.run('no-restricted-matchers', rule, {
messageId: 'restrictedChainWithMessage',
data: {
message: 'Use `expect(await promise)` instead.',
chain: 'resolves',
restriction: 'resolves',
},
endColumn: 52,
endColumn: 57,
column: 44,
},
],
},
{
code: 'expect(Promise.resolve({})).rejects.toBeFalsy()',
options: [{ toBeFalsy: null }],
options: [{ 'rejects.toBeFalsy': null }],
errors: [
{
messageId: 'restrictedChain',
data: {
message: null,
chain: 'toBeFalsy',
restriction: 'rejects.toBeFalsy',
},
endColumn: 46,
column: 37,
column: 29,
},
],
},
Expand All @@ -266,7 +255,7 @@ ruleTester.run('no-restricted-matchers', rule, {
messageId: 'restrictedChainWithMessage',
data: {
message: 'Use not.toHaveBeenCalled instead',
chain: 'not.toHaveBeenCalledWith',
restriction: 'not.toHaveBeenCalledWith',
},
endColumn: 48,
column: 24,
Expand Down
29 changes: 9 additions & 20 deletions src/rules/no-restricted-matchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default createRule<
},
],
messages: {
restrictedChain: 'Use of `{{ chain }}` is disallowed',
restrictedChain: 'Use of `{{ restriction }}` is disallowed',
restrictedChainWithMessage: '{{ message }}',
},
},
Expand All @@ -35,31 +35,20 @@ export default createRule<
return;
}

const permutations = [jestFnCall.members];

if (jestFnCall.members.length > 2) {
permutations.push([jestFnCall.members[0], jestFnCall.members[1]]);
permutations.push([jestFnCall.members[1], jestFnCall.members[2]]);
}

if (jestFnCall.members.length > 1) {
permutations.push(...jestFnCall.members.map(nod => [nod]));
}

for (const permutation of permutations) {
const chain = permutation.map(nod => getAccessorValue(nod)).join('.');

if (chain in restrictedChains) {
const message = restrictedChains[chain];
const chain = jestFnCall.members
.map(nod => getAccessorValue(nod))
.join('.');

for (const [restriction, message] of Object.entries(restrictedChains)) {
if (chain.startsWith(restriction)) {
context.report({
messageId: message
? 'restrictedChainWithMessage'
: 'restrictedChain',
data: { message, chain },
data: { message, restriction },
loc: {
start: permutation[0].loc.start,
end: permutation[permutation.length - 1].loc.end,
start: jestFnCall.members[0].loc.start,
end: jestFnCall.members[jestFnCall.members.length - 1].loc.end,
},
});

Expand Down