Skip to content

Commit

Permalink
feat(prefer-expect-assertions): provide suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Jun 20, 2020
1 parent 2eaed2b commit bad88a0
Show file tree
Hide file tree
Showing 2 changed files with 304 additions and 42 deletions.
195 changes: 180 additions & 15 deletions src/rules/__tests__/prefer-expect-assertions.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { TSESLint } from '@typescript-eslint/experimental-utils';
import dedent from 'dedent';
import resolveFrom from 'resolve-from';
import rule from '../prefer-expect-assertions';

Expand All @@ -13,35 +14,196 @@ ruleTester.run('prefer-expect-assertions', rule, {
invalid: [
{
code: 'it("it1", () => {})',
errors: [{ messageId: 'haveExpectAssertions' }],
errors: [
{
messageId: 'haveExpectAssertions',
column: 1,
line: 1,
suggestions: [
{
messageId: 'suggestAddingHasAssertions',
output: 'it("it1", () => {expect.hasAssertions();})',
},
{
messageId: 'suggestAddingAssertions',
output: 'it("it1", () => {expect.assertions();})',
},
],
},
],
},
{
code: 'it("it1", () => { foo()})',
errors: [{ messageId: 'haveExpectAssertions' }],
errors: [
{
messageId: 'haveExpectAssertions',
column: 1,
line: 1,
suggestions: [
{
messageId: 'suggestAddingHasAssertions',
output: 'it("it1", () => { expect.hasAssertions();foo()})',
},
{
messageId: 'suggestAddingAssertions',
output: 'it("it1", () => { expect.assertions();foo()})',
},
],
},
],
},
{
code:
'it("it1", function() {' +
'\n\t\t\tsomeFunctionToDo();' +
'\n\t\t\tsomeFunctionToDo2();\n' +
'\t\t\t})',
errors: [{ messageId: 'haveExpectAssertions' }],
code: dedent`
it("it1", function() {
someFunctionToDo();
someFunctionToDo2();
})
`,
errors: [
{
messageId: 'haveExpectAssertions',
column: 1,
line: 1,
suggestions: [
{
messageId: 'suggestAddingHasAssertions',
output: dedent`
it("it1", function() {
expect.hasAssertions();someFunctionToDo();
someFunctionToDo2();
})
`,
},
{
messageId: 'suggestAddingAssertions',
output: dedent`
it("it1", function() {
expect.assertions();someFunctionToDo();
someFunctionToDo2();
})
`,
},
],
},
],
},
{
code: 'it("it1", function() {var a = 2;})',
errors: [{ messageId: 'haveExpectAssertions' }],
errors: [
{
messageId: 'haveExpectAssertions',
column: 1,
line: 1,
suggestions: [
{
messageId: 'suggestAddingHasAssertions',
output:
'it("it1", function() {expect.hasAssertions();var a = 2;})',
},
{
messageId: 'suggestAddingAssertions',
output: 'it("it1", function() {expect.assertions();var a = 2;})',
},
],
},
],
},
{
code: 'it("it1", function() {expect.assertions();})',
errors: [{ messageId: 'haveExpectAssertions' }],
errors: [
{
messageId: 'assertionsRequiresOneArgument',
column: 30,
line: 1,
suggestions: [],
},
],
},
{
code: 'it("it1", function() {expect.assertions(1,2);})',
errors: [{ messageId: 'haveExpectAssertions' }],
errors: [
{
messageId: 'assertionsRequiresOneArgument',
column: 43,
line: 1,
suggestions: [
{
messageId: 'suggestRemovingExtraArguments',
output: 'it("it1", function() {expect.assertions(1);})',
},
],
},
],
},
{
code: 'it("it1", function() {expect.assertions("1");})',
errors: [{ messageId: 'haveExpectAssertions' }],
errors: [
{
messageId: 'assertionsRequiresNumberArgument',
column: 41,
line: 1,
suggestions: [],
},
],
},
{
code: 'it("it1", function() {expect.hasAssertions("1");})',
errors: [
{
messageId: 'hasAssertionsTakesNoArguments',
column: 30,
line: 1,
suggestions: [
{
messageId: 'suggestRemovingExtraArguments',
output: 'it("it1", function() {expect.hasAssertions();})',
},
],
},
],
},
{
code: 'it("it1", function() {expect.hasAssertions("1", "2");})',
errors: [
{
messageId: 'hasAssertionsTakesNoArguments',
column: 30,
line: 1,
suggestions: [
{
messageId: 'suggestRemovingExtraArguments',
output: 'it("it1", function() {expect.hasAssertions();})',
},
],
},
],
},
{
code: dedent`
it("it1", function() {
expect.hasAssertions(() => {
someFunctionToDo();
someFunctionToDo2();
});
})
`,
errors: [
{
messageId: 'hasAssertionsTakesNoArguments',
column: 10,
line: 2,
suggestions: [
{
messageId: 'suggestRemovingExtraArguments',
output: dedent`
it("it1", function() {
expect.hasAssertions();
})
`,
},
],
},
],
},
],

Expand All @@ -52,9 +214,12 @@ ruleTester.run('prefer-expect-assertions', rule, {
'test("it1", function() {expect.assertions(0);})',
'test("it1", function() {expect.hasAssertions();})',
'it("it1", function() {expect.assertions(0);})',
'it("it1", function() {\n\t\t\texpect.assertions(1);' +
'\n\t\t\texpect(someValue).toBe(true)\n' +
'\t\t\t})',
`
it("it1", function() {
expect.assertions(1);
expect(someValue).toBe(true)
})
`,
'test("it1")',
'itHappensToStartWithIt("foo", function() {})',
'testSomething("bar", function() {})',
Expand Down
Loading

0 comments on commit bad88a0

Please sign in to comment.