Skip to content

Commit 6a84431

Browse files
committed
feat: Support test.expect style
Fixes #297
1 parent 149ddc2 commit 6a84431

File tree

4 files changed

+43
-6
lines changed

4 files changed

+43
-6
lines changed

src/rules/expect-expect.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ runRuleTester('expect-expect', rule, {
4646
'["bar"]();',
4747
'testing("will test something eventually", () => {})',
4848
'test("should pass", () => expect(true).toBeDefined())',
49+
'test("should pass", () => test.expect(true).toBeDefined())',
4950
'test.slow("should pass", () => expect(true).toBeDefined())',
5051
'test.skip("should pass", () => expect(true).toBeDefined())',
5152
// Config methods

src/utils/ast.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@ export function getRawValue(node: ESTree.Node) {
1919
return node.type === 'Literal' ? node.raw : undefined
2020
}
2121

22-
export function isIdentifier(node: ESTree.Node, name?: string | RegExp) {
22+
export function isIdentifier(
23+
node: ESTree.Node | undefined,
24+
name?: string | RegExp,
25+
) {
2326
return (
24-
node.type === 'Identifier' &&
27+
node?.type === 'Identifier' &&
2528
(!name ||
2629
(typeof name === 'string' ? node.name === name : name.test(node.name)))
2730
)

src/utils/parseFnCall.test.ts

+26
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,32 @@ runRuleTester('expect', rule, {
464464
`,
465465
errors: [{ column: 1, line: 3, messageId: 'modifier-unknown' }],
466466
},
467+
{
468+
code: 'test.expect(x).toBe(y);',
469+
errors: [
470+
{
471+
column: 1,
472+
data: expectedParsedFnCallResultData({
473+
args: ['x'],
474+
group: 'expect',
475+
head: {
476+
local: 'test',
477+
node: 'test',
478+
original: null,
479+
},
480+
matcher: 'toBe',
481+
matcherArgs: ['y'],
482+
matcherName: 'toBe',
483+
members: ['toBe'],
484+
modifiers: [],
485+
name: 'expect',
486+
type: 'expect',
487+
}),
488+
line: 1,
489+
messageId: 'details',
490+
},
491+
],
492+
},
467493
],
468494
valid: [],
469495
})

src/utils/parseFnCall.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -329,10 +329,6 @@ function parse(
329329
let name = resolved.original ?? resolved.local
330330
const links = [name, ...rest.map((link) => getStringValue(link))]
331331

332-
if (name !== 'expect' && !VALID_CHAINS.has(links.join('.'))) {
333-
return null
334-
}
335-
336332
// To support Playwright's convention of `test.describe`, `test.beforeEach`,
337333
// etc. we need to test the second link in the chain to find the true type.
338334
if (name === 'test' && links.length > 1) {
@@ -344,6 +340,10 @@ function parse(
344340
}
345341
}
346342

343+
if (name !== 'expect' && !VALID_CHAINS.has(links.join('.'))) {
344+
return null
345+
}
346+
347347
const parsedFnCall: Omit<ParsedFnCall, 'group' | 'type'> = {
348348
head: { ...resolved, node: first },
349349
// every member node must have a member expression as their parent
@@ -355,6 +355,13 @@ function parse(
355355
const group = determinePlaywrightFnGroup(name)
356356

357357
if (group === 'expect') {
358+
// If using `test.expect` style, the `rest` array will start with `expect`
359+
// and we need to remove it to ensure the chain accurately represents the
360+
// `expect` call chain.
361+
if (isIdentifier(rest[0], 'expect')) {
362+
parsedFnCall.members.shift()
363+
}
364+
358365
const result = parseExpectCall(parsedFnCall)
359366

360367
// If the `expect` call chain is not valid, only report on the topmost node

0 commit comments

Comments
 (0)