Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/rules/prefer-called-exactly-once-with.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,14 @@ export default createEslintRule<Options, MESSAGE_IDS>({
firstCallExpression.parent.range[0],
firstCallExpression.range[0],
)
const replacement = `${indentation}${expectedText}.toHaveBeenCalledExactlyOnceWith(${argsText})`
const typesParam = targetArgNode.callExpression.typeArguments
? sourceCode.text.slice(
targetArgNode.callExpression.typeArguments.range[0],
targetArgNode.callExpression.typeArguments.range[1],
)
: ''

const replacement = `${indentation}${expectedText}.toHaveBeenCalledExactlyOnceWith${typesParam}(${argsText})`

const lineStart = sourceCode.getIndexFromLoc({
line: secondCallExpression.parent.loc.start.line,
Expand Down
143 changes: 143 additions & 0 deletions tests/prefer-called-exactly-once-with.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,146 @@ ruleTester.run(RULE_NAME, rule, {
},
],
})

ruleTester.run('prefer-called-exactly-once-with: typescript edition', rule, {
valid: [
'expect(fn).toHaveBeenCalledExactlyOnceWith<[{ id: number }]>()',
'expect(fn).toHaveBeenCalledExactlyOnceWith<[{ id: number }]>({id: 1})',
],
invalid: [
{
code: `
expect(x).toHaveBeenCalledOnce();
expect(x).toHaveBeenCalledWith<[string]>('hoge');
`,
errors: [
{
messageId: 'preferCalledExactlyOnceWith',
column: 17,
line: 3,
},
],
output: `
expect(x).toHaveBeenCalledExactlyOnceWith<[string]>('hoge');
`,
},
{
code: `
expect(x).toHaveBeenCalledWith<[string]>('hoge');
expect(x).toHaveBeenCalledOnce();
`,
errors: [
{
messageId: 'preferCalledExactlyOnceWith',
column: 17,
line: 3,
},
],
output: `
expect(x).toHaveBeenCalledExactlyOnceWith<[string]>('hoge');
`,
},
{
code: `
expect(x).toHaveBeenCalledOnce<[number]>();
expect(x).toHaveBeenCalledWith<[string]>('hoge');
`,
errors: [
{
messageId: 'preferCalledExactlyOnceWith',
column: 17,
line: 3,
},
],
output: `
expect(x).toHaveBeenCalledExactlyOnceWith<[string]>('hoge');
`,
},
{
code: `
expect(x).toHaveBeenCalledOnce();
expect(x).toHaveBeenCalledWith<
[
{
id: number
}
]
>('hoge');
`,
errors: [
{
messageId: 'preferCalledExactlyOnceWith',
column: 17,
line: 3,
},
],
output: `
expect(x).toHaveBeenCalledExactlyOnceWith<
[
{
id: number
}
]
>('hoge');
`,
},
{
code: `
expect(x).toHaveBeenCalledWith<[string, number]>('hoge', 123);
expect(x).toHaveBeenCalledOnce();
`,
errors: [
{
messageId: 'preferCalledExactlyOnceWith',
column: 17,
line: 3,
},
],
output: `
expect(x).toHaveBeenCalledExactlyOnceWith<[string, number]>('hoge', 123);
`,
},
{
code: `
expect(x).toHaveBeenCalledWith<[string, number]>('hoge', 123);
expect(x).toHaveBeenCalledOnce();
expect(y).toHaveBeenCalledWith('foo', 456);
expect(y).toHaveBeenCalledOnce();
`,
errors: [
{
messageId: 'preferCalledExactlyOnceWith',
column: 17,
line: 3,
},
{
messageId: 'preferCalledExactlyOnceWith',
column: 17,
line: 5,
},
],
output: `
expect(x).toHaveBeenCalledExactlyOnceWith<[string, number]>('hoge', 123);
expect(y).toHaveBeenCalledExactlyOnceWith('foo', 456);
`,
},
{
code: `
expect(x).toHaveBeenCalledOnce();
y.mockClear();
expect(x).toHaveBeenCalledWith<[string]>('hoge');
`,
errors: [
{
messageId: 'preferCalledExactlyOnceWith',
column: 17,
line: 4,
},
],
output: `
expect(x).toHaveBeenCalledExactlyOnceWith<[string]>('hoge');
y.mockClear();
`,
},
],
})
Loading