Skip to content

Commit

Permalink
feat: Support test.expect style
Browse files Browse the repository at this point in the history
Fixes #297
  • Loading branch information
mskelton committed Jun 28, 2024
1 parent 149ddc2 commit 6a84431
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/rules/expect-expect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ runRuleTester('expect-expect', rule, {
'["bar"]();',
'testing("will test something eventually", () => {})',
'test("should pass", () => expect(true).toBeDefined())',
'test("should pass", () => test.expect(true).toBeDefined())',
'test.slow("should pass", () => expect(true).toBeDefined())',
'test.skip("should pass", () => expect(true).toBeDefined())',
// Config methods
Expand Down
7 changes: 5 additions & 2 deletions src/utils/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ export function getRawValue(node: ESTree.Node) {
return node.type === 'Literal' ? node.raw : undefined
}

export function isIdentifier(node: ESTree.Node, name?: string | RegExp) {
export function isIdentifier(
node: ESTree.Node | undefined,
name?: string | RegExp,
) {
return (
node.type === 'Identifier' &&
node?.type === 'Identifier' &&
(!name ||
(typeof name === 'string' ? node.name === name : name.test(node.name)))
)
Expand Down
26 changes: 26 additions & 0 deletions src/utils/parseFnCall.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,32 @@ runRuleTester('expect', rule, {
`,
errors: [{ column: 1, line: 3, messageId: 'modifier-unknown' }],
},
{
code: 'test.expect(x).toBe(y);',
errors: [
{
column: 1,
data: expectedParsedFnCallResultData({
args: ['x'],
group: 'expect',
head: {
local: 'test',
node: 'test',
original: null,
},
matcher: 'toBe',
matcherArgs: ['y'],
matcherName: 'toBe',
members: ['toBe'],
modifiers: [],
name: 'expect',
type: 'expect',
}),
line: 1,
messageId: 'details',
},
],
},
],
valid: [],
})
Expand Down
15 changes: 11 additions & 4 deletions src/utils/parseFnCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,10 +329,6 @@ function parse(
let name = resolved.original ?? resolved.local
const links = [name, ...rest.map((link) => getStringValue(link))]

if (name !== 'expect' && !VALID_CHAINS.has(links.join('.'))) {
return null
}

// To support Playwright's convention of `test.describe`, `test.beforeEach`,
// etc. we need to test the second link in the chain to find the true type.
if (name === 'test' && links.length > 1) {
Expand All @@ -344,6 +340,10 @@ function parse(
}
}

if (name !== 'expect' && !VALID_CHAINS.has(links.join('.'))) {
return null
}

const parsedFnCall: Omit<ParsedFnCall, 'group' | 'type'> = {
head: { ...resolved, node: first },
// every member node must have a member expression as their parent
Expand All @@ -355,6 +355,13 @@ function parse(
const group = determinePlaywrightFnGroup(name)

if (group === 'expect') {
// If using `test.expect` style, the `rest` array will start with `expect`
// and we need to remove it to ensure the chain accurately represents the
// `expect` call chain.
if (isIdentifier(rest[0], 'expect')) {
parsedFnCall.members.shift()
}

const result = parseExpectCall(parsedFnCall)

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

0 comments on commit 6a84431

Please sign in to comment.