-
Notifications
You must be signed in to change notification settings - Fork 240
feat: resolve expect
based on scope
#1173
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
Changes from all commits
d94889c
13ad62c
be6c9ba
bf3f040
db349f8
1925b65
f7f30cd
e97533e
6f82836
08d0615
4d616ca
f258c58
b8fd7db
7d4d4b6
016ba2b
fc73465
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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()', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This has become an error, since it's a standalone |
||
'{}', | ||
'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) });', | ||
|
@@ -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` | ||
|
@@ -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` | ||
|
@@ -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` | ||
|
@@ -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' }], | ||
}, | ||
], | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,10 @@ const expectSuggestions = ( | |
|
||
ruleTester.run('prefer-equality-matcher: ===', rule, { | ||
valid: [ | ||
'expect.hasAssertions', | ||
'expect.hasAssertions()', | ||
'expect.assertions(1)', | ||
'expect(true).toBe(...true)', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)', | ||
|
@@ -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)', | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 avalid
test which covers this change 🤷