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: resolve expect based on scope #1173

Merged
merged 16 commits into from Aug 7, 2022
Merged
Show file tree
Hide file tree
Changes from 15 commits
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
18 changes: 17 additions & 1 deletion src/rules/__tests__/no-restricted-matchers.test.ts
Expand Up @@ -70,7 +70,7 @@ ruleTester.run('no-restricted-matchers', rule, {
],
},
{
code: 'expect(a).not',
code: 'expect(a).not[x]()',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't we keep the old test?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new parser doesn't consider the old test to be a valid expect call, and we've already got expect(a).rejects as a valid test which covers this change 🤷

options: [{ not: null }],
errors: [
{
Expand Down Expand Up @@ -159,6 +159,22 @@ ruleTester.run('no-restricted-matchers', rule, {
},
],
},
{
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 Down
52 changes: 30 additions & 22 deletions src/rules/__tests__/no-standalone-expect.test.ts
Expand Up @@ -20,7 +20,6 @@ ruleTester.run('no-standalone-expect', rule, {
'it("an it", () => expect(1).toBe(1))',
'const func = function(){ expect(1).toBe(1); };',
'const func = () => expect(1).toBe(1);',
'expect.hasAssertions()',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has become an error, since it's a standalone expect.

'{}',
'it.each([1, true])("trues", value => { expect(value).toBe(true); });',
'it.each([1, true])("trues", value => { expect(value).toBe(true); }); it("an it", () => { expect(1).toBe(1) });',
Expand Down Expand Up @@ -59,27 +58,31 @@ ruleTester.run('no-standalone-expect', rule, {
],
invalid: [
{
code: "(() => {})('testing', () => expect(true))",
errors: [{ endColumn: 41, column: 29, messageId: 'unexpectedExpect' }],
code: "(() => {})('testing', () => expect(true).toBe(false))",
errors: [{ column: 29, messageId: 'unexpectedExpect' }],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why no endColumn

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eh I typically don't include it as I don't think its super valuable since we can't control how the results are used in tools & editors so it just creates more work for us without much gain, but yeah I should include it here for consistency since the other tests have it 😅

},
{
code: 'expect.hasAssertions()',
errors: [{ column: 1, messageId: 'unexpectedExpect' }],
},
{
code: dedent`
describe('scenario', () => {
const t = Math.random() ? it.only : it;
t('testing', () => expect(true));
t('testing', () => expect(true).toBe(false));
});
`,
errors: [{ endColumn: 34, column: 22, messageId: 'unexpectedExpect' }],
errors: [{ endColumn: 46, column: 22, messageId: 'unexpectedExpect' }],
},
{
code: dedent`
describe('scenario', () => {
const t = Math.random() ? it.only : it;
t('testing', () => expect(true));
t('testing', () => expect(true).toBe(false));
});
`,
options: [{ additionalTestBlockFunctions: undefined }],
errors: [{ endColumn: 34, column: 22, messageId: 'unexpectedExpect' }],
errors: [{ endColumn: 46, column: 22, messageId: 'unexpectedExpect' }],
},
{
code: dedent`
Expand All @@ -91,7 +94,7 @@ ruleTester.run('no-standalone-expect', rule, {
expect(a + b).toBe(expected);
});
`,
errors: [{ endColumn: 16, column: 3, messageId: 'unexpectedExpect' }],
errors: [{ endColumn: 31, column: 3, messageId: 'unexpectedExpect' }],
},
{
code: dedent`
Expand All @@ -104,7 +107,7 @@ ruleTester.run('no-standalone-expect', rule, {
});
`,
options: [{ additionalTestBlockFunctions: ['each'] }],
errors: [{ endColumn: 16, column: 3, messageId: 'unexpectedExpect' }],
errors: [{ endColumn: 31, column: 3, messageId: 'unexpectedExpect' }],
},
{
code: dedent`
Expand All @@ -117,43 +120,48 @@ ruleTester.run('no-standalone-expect', rule, {
});
`,
options: [{ additionalTestBlockFunctions: ['test'] }],
errors: [{ endColumn: 16, column: 3, messageId: 'unexpectedExpect' }],
errors: [{ endColumn: 31, column: 3, messageId: 'unexpectedExpect' }],
},
{
code: 'describe("a test", () => { expect(1).toBe(1); });',
errors: [{ endColumn: 37, column: 28, messageId: 'unexpectedExpect' }],
errors: [{ endColumn: 45, column: 28, messageId: 'unexpectedExpect' }],
},
{
code: 'describe("a test", () => expect(1).toBe(1));',
errors: [{ endColumn: 35, column: 26, messageId: 'unexpectedExpect' }],
errors: [{ endColumn: 43, column: 26, messageId: 'unexpectedExpect' }],
},
{
code: 'describe("a test", () => { const func = () => { expect(1).toBe(1); }; expect(1).toBe(1); });',
errors: [{ endColumn: 80, column: 71, messageId: 'unexpectedExpect' }],
errors: [{ endColumn: 88, column: 71, messageId: 'unexpectedExpect' }],
},
{
code: 'describe("a test", () => { it(() => { expect(1).toBe(1); }); expect(1).toBe(1); });',
errors: [{ endColumn: 72, column: 63, messageId: 'unexpectedExpect' }],
errors: [{ endColumn: 80, column: 63, messageId: 'unexpectedExpect' }],
},
{
code: 'expect(1).toBe(1);',
errors: [{ endColumn: 10, column: 1, messageId: 'unexpectedExpect' }],
},
{
code: 'expect(1).toBe',
errors: [{ endColumn: 10, column: 1, messageId: 'unexpectedExpect' }],
errors: [{ endColumn: 18, column: 1, messageId: 'unexpectedExpect' }],
},
{
code: '{expect(1).toBe(1)}',
errors: [{ endColumn: 11, column: 2, messageId: 'unexpectedExpect' }],
errors: [{ endColumn: 19, column: 2, messageId: 'unexpectedExpect' }],
},
{
code: 'it.each([1, true])("trues", value => { expect(value).toBe(true); }); expect(1).toBe(1);',
errors: [{ endColumn: 79, column: 70, messageId: 'unexpectedExpect' }],
errors: [{ endColumn: 87, column: 70, messageId: 'unexpectedExpect' }],
},
{
code: 'describe.each([1, true])("trues", value => { expect(value).toBe(true); });',
errors: [{ endColumn: 59, column: 46, messageId: 'unexpectedExpect' }],
errors: [{ endColumn: 70, column: 46, messageId: 'unexpectedExpect' }],
},
{
code: dedent`
import { expect as pleaseExpect } from '@jest/globals';

describe("a test", () => { pleaseExpect(1).toBe(1); });
`,
parserOptions: { sourceType: 'module' },
errors: [{ endColumn: 51, column: 28, messageId: 'unexpectedExpect' }],
},
],
});
4 changes: 4 additions & 0 deletions src/rules/__tests__/prefer-comparison-matcher.test.ts
Expand Up @@ -263,6 +263,10 @@ testComparisonOperator('<=', 'toBeLessThanOrEqual', 'toBeGreaterThan');

ruleTester.run(`prefer-to-be-comparison`, rule, {
valid: [
'expect.hasAssertions',
'expect.hasAssertions()',
'expect.assertions(1)',
'expect(true).toBe(...true)',
'expect()',
'expect({}).toStrictEqual({})',
'expect(a === b).toBe(true)',
Expand Down
8 changes: 8 additions & 0 deletions src/rules/__tests__/prefer-equality-matcher.test.ts
Expand Up @@ -32,6 +32,10 @@ const expectSuggestions = (

ruleTester.run('prefer-equality-matcher: ===', rule, {
valid: [
'expect.hasAssertions',
'expect.hasAssertions()',
'expect.assertions(1)',
'expect(true).toBe(...true)',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like a syntax error to me 😅

'expect(a == 1).toBe(true)',
'expect(1 == a).toBe(true)',
'expect(a == b).toBe(true)',
Expand Down Expand Up @@ -172,6 +176,10 @@ ruleTester.run('prefer-equality-matcher: ===', rule, {

ruleTester.run('prefer-equality-matcher: !==', rule, {
valid: [
'expect.hasAssertions',
'expect.hasAssertions()',
'expect.assertions(1)',
'expect(true).toBe(...true)',
'expect(a != 1).toBe(true)',
'expect(1 != a).toBe(true)',
'expect(a != b).toBe(true)',
Expand Down
23 changes: 23 additions & 0 deletions src/rules/__tests__/prefer-expect-resolves.test.ts
Expand Up @@ -12,6 +12,7 @@ const ruleTester = new TSESLint.RuleTester({

ruleTester.run('prefer-expect-resolves', rule, {
valid: [
'expect.hasAssertions()',
dedent`
it('passes', async () => {
await expect(someValue()).resolves.toBe(true);
Expand Down Expand Up @@ -61,5 +62,27 @@ ruleTester.run('prefer-expect-resolves', rule, {
`,
errors: [{ endColumn: 25, column: 10, messageId: 'expectResolves' }],
},
{
code: dedent`
import { expect as pleaseExpect } from '@jest/globals';

it('is true', async () => {
const myPromise = Promise.resolve(true);

pleaseExpect(await myPromise).toBe(true);
});
`,
output: dedent`
import { expect as pleaseExpect } from '@jest/globals';

it('is true', async () => {
const myPromise = Promise.resolve(true);

await pleaseExpect(myPromise).resolves.toBe(true);
});
`,
parserOptions: { sourceType: 'module' },
errors: [{ endColumn: 31, column: 16, messageId: 'expectResolves' }],
},
],
});
1 change: 1 addition & 0 deletions src/rules/__tests__/prefer-to-be.test.ts
Expand Up @@ -14,6 +14,7 @@ ruleTester.run('prefer-to-be', rule, {
'expect(null).toBeNull();',
'expect(null).not.toBeNull();',
'expect(null).toBe(1);',
'expect(null).toBe(...1);',
'expect(obj).toStrictEqual([ x, 1 ]);',
'expect(obj).toStrictEqual({ x: 1 });',
'expect(obj).not.toStrictEqual({ x: 1 });',
Expand Down
28 changes: 27 additions & 1 deletion src/rules/__tests__/prefer-to-contain.test.ts
@@ -1,10 +1,20 @@
import { TSESLint } from '@typescript-eslint/utils';
import dedent from 'dedent';
import rule from '../prefer-to-contain';
import { espreeParser } from './test-utils';

const ruleTester = new TSESLint.RuleTester();
const ruleTester = new TSESLint.RuleTester({
parser: espreeParser,
parserOptions: {
ecmaVersion: 2015,
},
});

ruleTester.run('prefer-to-contain', rule, {
valid: [
'expect.hasAssertions',
'expect.hasAssertions()',
'expect.assertions(1)',
'expect().toBe(false);',
'expect(a).toContain(b);',
"expect(a.name).toBe('b');",
Expand All @@ -24,6 +34,8 @@ ruleTester.run('prefer-to-contain', rule, {
`expect(a.test(b)).resolves.toEqual(true)`,
`expect(a.test(b)).resolves.not.toEqual(true)`,
`expect(a).not.toContain(b)`,
'expect(a.includes(...[])).toBe(true)',
'expect(a.includes(b)).toBe(...true)',
'expect(a);',
],
invalid: [
Expand Down Expand Up @@ -177,6 +189,20 @@ ruleTester.run('prefer-to-contain', rule, {
output: 'expect([{a:1}]).toContain({a:1});',
errors: [{ messageId: 'useToContain', column: 37, line: 1 }],
},
{
code: dedent`
import { expect as pleaseExpect } from '@jest/globals';

pleaseExpect([{a:1}].includes({a:1})).not.toStrictEqual(false);
`,
output: dedent`
import { expect as pleaseExpect } from '@jest/globals';

pleaseExpect([{a:1}]).toContain({a:1});
`,
parserOptions: { sourceType: 'module' },
errors: [{ messageId: 'useToContain', column: 43, line: 3 }],
},
],
});

Expand Down
2 changes: 2 additions & 0 deletions src/rules/__tests__/prefer-to-have-length.test.ts
Expand Up @@ -11,6 +11,8 @@ const ruleTester = new TSESLint.RuleTester({

ruleTester.run('prefer-to-have-length', rule, {
valid: [
'expect.hasAssertions',
'expect.hasAssertions()',
'expect(files).toHaveLength(1);',
"expect(files.name).toBe('file');",
"expect(files[`name`]).toBe('file');",
Expand Down