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
3 changes: 2 additions & 1 deletion docs/rules/use-test.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Translations: [Français](https://github.com/avajs/ava-docs/blob/main/fr_FR/related/eslint-plugin-ava/docs/rules/use-test.md)

The convention is to import AVA and assign it to a variable named `test`. Most rules in `eslint-plugin-ava` are based on that assumption.
In a TypeScript file (`.ts` or `.tsx`) AVA can be assigned to a variable named `anyTest` in order to define the types of `t.context` (see [Typing t.context](https://github.com/avajs/ava/blob/main/docs/recipes/typescript.md#typing-tcontext)).
In a TypeScript file (`.ts` or `.tsx`) AVA can be assigned to a variable named `anyTest` in order to define the types of `t.context` (see [Typing t.context](https://github.com/avajs/ava/blob/main/docs/recipes/typescript.md#typing-tcontext)). Type-only imports (`import type ... from 'ava'`) are not linted.

### Fail

Expand All @@ -28,6 +28,7 @@ const test = require('foo');

```ts
import anyTest from 'ava';
import type {TestInterface} from 'ava';

const test = anyTest as TestInterface<{foo: string}>;
```
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"resolve-from": "^5.0.0"
},
"devDependencies": {
"@typescript-eslint/parser": "^5.9.0",
"ava": "^3.15.0",
"c8": "^7.7.3",
"chalk": "^4.1.1",
Expand Down
2 changes: 1 addition & 1 deletion rules/use-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const create = context => {
const isTypeScript = ext === '.ts' || ext === '.tsx';

return {
ImportDeclaration: node => {
'ImportDeclaration[importKind!="type"]': node => {
if (node.source.value === 'ava') {
const {name} = node.specifiers[0].local;
if (name !== 'test' && (!isTypeScript || name !== 'anyTest')) {
Expand Down
20 changes: 18 additions & 2 deletions test/use-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ const ruleTester = avaRuleTester(test, {
},
});

const typescriptRuleTester = avaRuleTester(test, {
parser: require.resolve('@typescript-eslint/parser'),
});

const errors = [{}];

ruleTester.run('use-test', rule, {
const commonTestCases = {
valid: [
{code: 'var test = require(\'ava\');', filename: 'file.js'},
{code: 'let test = require(\'ava\');', filename: 'file.js'},
Expand Down Expand Up @@ -124,4 +128,16 @@ ruleTester.run('use-test', rule, {
filename: 'file.tsx',
},
],
});
};

const typescriptTestCases = {
valid: [
{code: 'import type {Macro} from \'ava\';', filename: 'file.ts'},
{code: 'import type {Macro} from \'ava\';', filename: 'file.tsx'},
],
invalid: [],
};

ruleTester.run('use-test', rule, commonTestCases);
typescriptRuleTester.run('use-test', rule, commonTestCases);
typescriptRuleTester.run('use-test', rule, typescriptTestCases);