Skip to content

Commit

Permalink
fix: support all variations of describe, it, & test (#792)
Browse files Browse the repository at this point in the history
* refactor(utils): support matching all variations of `describe` & `test`

* test(consistent-test-it): add case for `describe.only.each`

* test(require-top-level-describe): add cases for modifiers and `each`

* test(no-conditional-expect): add cases for `.each` with template literal

* test(no-export): add cases for `.each`

* test(no-if): add cases for `.each` calls

* test(no-test-return-statement): add cases for `.each`

* test(prefer-hooks-on-top): add cases for `.each`

* test(valid-title): add cases for `.each`

* test(utils): deduplicate code

* test(utils): refactor to deduplicate code
  • Loading branch information
G-Rath committed Apr 5, 2021
1 parent d68093b commit 0968b55
Show file tree
Hide file tree
Showing 28 changed files with 708 additions and 77 deletions.
26 changes: 26 additions & 0 deletions src/rules/__tests__/consistent-test-it.test.ts
Expand Up @@ -213,6 +213,32 @@ ruleTester.run('consistent-test-it with fn=test', rule, {
},
],
},
{
code: dedent`
describe.only.each()("%s", () => {
test("is valid, but should not be", () => {});
it("is not valid, but should be", () => {});
});
`,
output: dedent`
describe.only.each()("%s", () => {
it("is valid, but should not be", () => {});
it("is not valid, but should be", () => {});
});
`,
options: [{ fn: TestCaseName.test, withinDescribe: TestCaseName.it }],
errors: [
{
messageId: 'consistentMethodWithinDescribe',
data: {
testKeywordWithinDescribe: TestCaseName.it,
oppositeTestKeyword: TestCaseName.test,
},
},
],
},
{
code: 'describe("suite", () => { it("foo") })',
output: 'describe("suite", () => { test("foo") })',
Expand Down
4 changes: 4 additions & 0 deletions src/rules/__tests__/lowercase-name.test.ts
Expand Up @@ -52,6 +52,10 @@ ruleTester.run('lowercase-name', rule, {
'describe(function () {})',
'describe(``)',
'describe("")',
dedent`
describe.each()(1);
describe.each()(2);
`,
'describe(42)',
{
code: 'describe(42)',
Expand Down
65 changes: 65 additions & 0 deletions src/rules/__tests__/no-conditional-expect.test.ts
Expand Up @@ -143,6 +143,14 @@ ruleTester.run('logical conditions', rule, {
`,
errors: [{ messageId: 'conditionalExpect' }],
},
{
code: `
it.each\`\`('foo', () => {
something || expect(something).toHaveBeenCalled();
})
`,
errors: [{ messageId: 'conditionalExpect' }],
},
{
code: `
function getValue() {
Expand Down Expand Up @@ -202,6 +210,14 @@ ruleTester.run('conditional conditions', rule, {
`,
errors: [{ messageId: 'conditionalExpect' }],
},
{
code: `
it.each\`\`('foo', () => {
something ? noop() : expect(something).toHaveBeenCalled();
})
`,
errors: [{ messageId: 'conditionalExpect' }],
},
{
code: `
function getValue() {
Expand Down Expand Up @@ -275,6 +291,19 @@ ruleTester.run('switch conditions', rule, {
`,
errors: [{ messageId: 'conditionalExpect' }],
},
{
code: `
it.each\`\`('foo', () => {
switch(something) {
case 'value':
expect(something).toHaveBeenCalled();
default:
break;
}
})
`,
errors: [{ messageId: 'conditionalExpect' }],
},
{
code: `
function getValue() {
Expand Down Expand Up @@ -343,6 +372,18 @@ ruleTester.run('if conditions', rule, {
`,
errors: [{ messageId: 'conditionalExpect' }],
},
{
code: `
it.each\`\`('foo', () => {
if(!doSomething) {
// do nothing
} else {
expect(something).toHaveBeenCalled();
}
})
`,
errors: [{ messageId: 'conditionalExpect' }],
},
{
code: `
function getValue() {
Expand Down Expand Up @@ -438,6 +479,30 @@ ruleTester.run('catch conditions', rule, {
`,
errors: [{ messageId: 'conditionalExpect' }],
},
{
code: `
it.each\`\`('foo', () => {
try {
} catch (err) {
expect(err).toMatch('Error');
}
})
`,
errors: [{ messageId: 'conditionalExpect' }],
},
{
code: `
it.skip.each\`\`('foo', () => {
try {
} catch (err) {
expect(err).toMatch('Error');
}
})
`,
errors: [{ messageId: 'conditionalExpect' }],
},
{
code: `
function getValue() {
Expand Down
36 changes: 35 additions & 1 deletion src/rules/__tests__/no-export.test.ts
@@ -1,4 +1,5 @@
import { TSESLint } from '@typescript-eslint/experimental-utils';
import dedent from 'dedent';
import resolveFrom from 'resolve-from';
import rule from '../no-export';

Expand All @@ -23,7 +24,40 @@ ruleTester.run('no-export', rule, {
invalid: [
{
code:
'export const myThing = "invalid"; test("a test", () => { expect(1).toBe(1);});',
'export const myThing = "invalid"; test("a test", () => { expect(1).toBe(1);});',
parserOptions: { sourceType: 'module' },
errors: [{ endColumn: 34, column: 1, messageId: 'unexpectedExport' }],
},
{
code: dedent`
export const myThing = 'invalid';
test.each()('my code', () => {
expect(1).toBe(1);
});
`,
parserOptions: { sourceType: 'module' },
errors: [{ endColumn: 34, column: 1, messageId: 'unexpectedExport' }],
},
{
code: dedent`
export const myThing = 'invalid';
test.each\`\`('my code', () => {
expect(1).toBe(1);
});
`,
parserOptions: { sourceType: 'module' },
errors: [{ endColumn: 34, column: 1, messageId: 'unexpectedExport' }],
},
{
code: dedent`
export const myThing = 'invalid';
test.only.each\`\`('my code', () => {
expect(1).toBe(1);
});
`,
parserOptions: { sourceType: 'module' },
errors: [{ endColumn: 34, column: 1, messageId: 'unexpectedExport' }],
},
Expand Down
28 changes: 28 additions & 0 deletions src/rules/__tests__/no-if.test.ts
Expand Up @@ -812,6 +812,34 @@ ruleTester.run('if statements', rule, {
},
],
},
{
code: dedent`
it.each\`\`('foo', () => {
callExpression()
if ('bar') {}
})
`,
errors: [
{
data: { condition: 'if' },
messageId: 'conditionalInTest',
},
],
},
{
code: dedent`
it.only.each\`\`('foo', () => {
callExpression()
if ('bar') {}
})
`,
errors: [
{
data: { condition: 'if' },
messageId: 'conditionalInTest',
},
],
},
{
code: dedent`
describe('valid', () => {
Expand Down
24 changes: 24 additions & 0 deletions src/rules/__tests__/no-test-return-statement.test.ts
Expand Up @@ -52,6 +52,30 @@ ruleTester.run('no-test-return-statement', rule, {
`,
errors: [{ messageId: 'noReturnValue', column: 3, line: 2 }],
},
{
code: dedent`
it.skip("one", function () {
return expect(1).toBe(1);
});
`,
errors: [{ messageId: 'noReturnValue', column: 3, line: 2 }],
},
{
code: dedent`
it.each\`\`("one", function () {
return expect(1).toBe(1);
});
`,
errors: [{ messageId: 'noReturnValue', column: 3, line: 2 }],
},
{
code: dedent`
it.only.each\`\`("one", function () {
return expect(1).toBe(1);
});
`,
errors: [{ messageId: 'noReturnValue', column: 3, line: 2 }],
},
{
code: dedent`
it("one", myTest);
Expand Down
42 changes: 42 additions & 0 deletions src/rules/__tests__/prefer-hooks-on-top.test.ts
Expand Up @@ -45,6 +45,48 @@ ruleTester.run('basic describe block', rule, {
},
],
},
{
code: dedent`
describe('foo', () => {
beforeEach(() => {});
test.each\`\`('bar', () => {
someFn();
});
beforeAll(() => {});
test.only('bar', () => {
someFn();
});
});
`,
errors: [
{
messageId: 'noHookOnTop',
column: 3,
line: 6,
},
],
},
{
code: dedent`
describe('foo', () => {
beforeEach(() => {});
test.only.each\`\`('bar', () => {
someFn();
});
beforeAll(() => {});
test.only('bar', () => {
someFn();
});
});
`,
errors: [
{
messageId: 'noHookOnTop',
column: 3,
line: 6,
},
],
},
],
});

Expand Down
17 changes: 17 additions & 0 deletions src/rules/__tests__/require-top-level-describe.test.ts
Expand Up @@ -12,6 +12,7 @@ const ruleTester = new TSESLint.RuleTester({

ruleTester.run('require-top-level-describe', rule, {
valid: [
'it.each()',
'describe("test suite", () => { test("my test") });',
'describe("test suite", () => { it("my test") });',
dedent`
Expand Down Expand Up @@ -89,9 +90,25 @@ ruleTester.run('require-top-level-describe', rule, {
`,
errors: [{ messageId: 'unexpectedHook' }],
},
{
code: "it.skip('test', () => {});",
errors: [{ messageId: 'unexpectedTestCase' }],
},
{
code: "it.each([1, 2, 3])('%n', () => {});",
errors: [{ messageId: 'unexpectedTestCase' }],
},
{
code: "it.skip.each([1, 2, 3])('%n', () => {});",
errors: [{ messageId: 'unexpectedTestCase' }],
},
{
code: "it.skip.each``('%n', () => {});",
errors: [{ messageId: 'unexpectedTestCase' }],
},
{
code: "it.each``('%n', () => {});",
errors: [{ messageId: 'unexpectedTestCase' }],
},
],
});

0 comments on commit 0968b55

Please sign in to comment.