Skip to content

Commit

Permalink
feat: resolve expect based on scope (#1173)
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath authored Aug 7, 2022
1 parent baa76cf commit aa4be21
Show file tree
Hide file tree
Showing 35 changed files with 1,068 additions and 916 deletions.
18 changes: 17 additions & 1 deletion src/rules/__tests__/no-restricted-matchers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ ruleTester.run('no-restricted-matchers', rule, {
],
},
{
code: 'expect(a).not',
code: 'expect(a).not[x]()',
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
Original file line number Diff line number Diff line change
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()',
'{}',
'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: [{ endColumn: 53, column: 29, messageId: 'unexpectedExpect' }],
},
{
code: 'expect.hasAssertions()',
errors: [{ endColumn: 23, 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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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)',
'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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Loading

0 comments on commit aa4be21

Please sign in to comment.