-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: improve logic to detect if testing librar imported (#239)
* refactor: check testing library imports automatically * feat: add new shared setting for testing library module * test: increase coverage for create testing library rule * refactor: extract common enhanced rule create types * docs: add jsdoc to detection helpers * docs: update old comments related to ImportDeclaration * test: check rule listener are combined properly
- Loading branch information
Showing
7 changed files
with
292 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import { createRuleTester } from './lib/test-utils'; | ||
import rule, { RULE_NAME } from './fake-rule'; | ||
|
||
const ruleTester = createRuleTester({ | ||
ecmaFeatures: { | ||
jsx: true, | ||
}, | ||
}); | ||
|
||
ruleTester.run(RULE_NAME, rule, { | ||
valid: [ | ||
{ | ||
code: ` | ||
// case: nothing related to Testing Library at all | ||
import { shallow } from 'enzyme'; | ||
const wrapper = shallow(<MyComponent />); | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
// case: render imported from other than custom module | ||
import { render } from '@somewhere/else' | ||
const utils = render(); | ||
`, | ||
settings: { | ||
'testing-library/module': 'test-utils', | ||
}, | ||
}, | ||
{ | ||
code: ` | ||
// case: prevent import which should trigger an error since it's imported | ||
// from other than custom module | ||
import { foo } from 'report-me' | ||
`, | ||
settings: { | ||
'testing-library/module': 'test-utils', | ||
}, | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: ` | ||
// case: import module forced to be reported | ||
import { foo } from 'report-me' | ||
`, | ||
errors: [{ line: 3, column: 7, messageId: 'fakeError' }], | ||
}, | ||
{ | ||
code: ` | ||
// case: render imported from any module by default (aggressive reporting) | ||
import { render } from '@somewhere/else' | ||
import { somethingElse } from 'another-module' | ||
const utils = render(); | ||
`, | ||
errors: [ | ||
{ | ||
line: 6, | ||
column: 21, | ||
messageId: 'fakeError', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: ` | ||
// case: render imported from Testing Library module | ||
import { render } from '@testing-library/react' | ||
import { somethingElse } from 'another-module' | ||
const utils = render(); | ||
`, | ||
errors: [ | ||
{ | ||
line: 6, | ||
column: 21, | ||
messageId: 'fakeError', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: ` | ||
// case: render imported from config custom module | ||
import { render } from 'test-utils' | ||
import { somethingElse } from 'another-module' | ||
const utils = render(); | ||
`, | ||
settings: { | ||
'testing-library/module': 'test-utils', | ||
}, | ||
errors: [ | ||
{ | ||
line: 6, | ||
column: 21, | ||
messageId: 'fakeError', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: ` | ||
// case: render imported from Testing Library module if | ||
// custom module setup | ||
import { render } from '@testing-library/react' | ||
import { somethingElse } from 'another-module' | ||
const utils = render(); | ||
`, | ||
settings: { | ||
'testing-library/module': 'test-utils', | ||
}, | ||
errors: [ | ||
{ | ||
line: 7, | ||
column: 21, | ||
messageId: 'fakeError', | ||
}, | ||
], | ||
}, | ||
], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* @file Fake rule to be able to test createTestingLibraryRule and | ||
* detectTestingLibraryUtils properly | ||
*/ | ||
import { TSESTree } from '@typescript-eslint/experimental-utils'; | ||
import { createTestingLibraryRule } from '../lib/create-testing-library-rule'; | ||
|
||
export const RULE_NAME = 'fake-rule'; | ||
type Options = []; | ||
type MessageIds = 'fakeError'; | ||
|
||
export default createTestingLibraryRule<Options, MessageIds>({ | ||
name: RULE_NAME, | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'Fake rule to test rule maker and detection helpers', | ||
category: 'Possible Errors', | ||
recommended: false, | ||
}, | ||
messages: { | ||
fakeError: 'fake error reported', | ||
}, | ||
fixable: null, | ||
schema: [], | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
const reportRenderIdentifier = (node: TSESTree.Identifier) => { | ||
if (node.name === 'render') { | ||
context.report({ | ||
node, | ||
messageId: 'fakeError', | ||
}); | ||
} | ||
}; | ||
|
||
const checkImportDeclaration = (node: TSESTree.ImportDeclaration) => { | ||
// This is just to check that defining an `ImportDeclaration` doesn't | ||
// override `ImportDeclaration` from `detectTestingLibraryUtils` | ||
|
||
if (node.source.value === 'report-me') { | ||
context.report({ | ||
node, | ||
messageId: 'fakeError', | ||
}); | ||
} | ||
}; | ||
|
||
return { | ||
'CallExpression Identifier': reportRenderIdentifier, | ||
ImportDeclaration: checkImportDeclaration, | ||
}; | ||
}, | ||
}); |
Oops, something went wrong.