-
Notifications
You must be signed in to change notification settings - Fork 49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add no-import-test-file
rule
#193
Add no-import-test-file
rule
#193
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR @HenriBeck! I suppose this needs #192 for ava.config.js
support?
Could you add documentation for the new rule?
@avajs/core should this be a recommended rule?
(Also, I'm not really familiar with how ESLint plugins work, so if anybody else could have a look at this PR that'd be great.)
rules/no-import-test-files.js
Outdated
const absoluteImportedPath = path.resolve(path.dirname(sourceFile), importedFile); | ||
const relativePath = path.relative(rootDir, absoluteImportedPath); | ||
|
||
console.log(sourceFile, importedFile, absoluteImportedPath, relativePath); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you remove this line?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ohh, yeah totally forgot that line.
rules/no-import-test-files.js
Outdated
if (isImportingTestFile) { | ||
context.report({ | ||
node, | ||
message: `You are importing a test file`, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't seem like the most helpful message, but I'm not really familiar with how this shows up in linter reports. @sindresorhus?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you have a better commit message, feel free to suggest it.
It shows up as the message in the linter at the specific line.
rules/no-import-test-files.js
Outdated
return {}; | ||
} | ||
|
||
const packageInfo = getPackageInfo(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you rename this to projectInfo
/ getProjectInfo()
etc? That better aligns with how things are named in AVA itself.
- Renamed function getPackageInfo to getProjectInfo Signed-off-by: Henri Beck <[email protected]>
Signed-off-by: Henri Beck <[email protected]>
No, this PR doesn't necessarily need #192. Personally, I would put that into the recommended config, because I think this is a pretty clear anti-pattern. |
Yes, this should be a recommended rule. |
This rule needs to be added to the readme. |
rules/no-import-test-files.js
Outdated
'test/**/*.js', | ||
'**/__tests__/**/*.js', | ||
'**/*.test.js' | ||
]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be moved to util
as it's also defined in no-ignored-test-files
(the one there should be moved too)
docs/rules/no-import-test-files.md
Outdated
@@ -0,0 +1,60 @@ | |||
# Ensure no tests are imported anywhere |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tests
=> test files
docs/rules/no-import-test-files.md
Outdated
# Ensure no tests are imported anywhere | ||
|
||
This rule will verify that you don't import any test files. It will consider the root of the project to be the closest folder containing a `package.json` file, and will not do anything if it can't find one. Test files in `node_modules` will not be linted as they are ignored by ESLint. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The rule should mention why it's bad to import test files, like described in #141
docs/rules/no-import-test-files.md
Outdated
|
||
This rule will verify that you don't import any test files. It will consider the root of the project to be the closest folder containing a `package.json` file, and will not do anything if it can't find one. Test files in `node_modules` will not be linted as they are ignored by ESLint. | ||
|
||
Note that this rule will not be able to warn correctly if you use AVA by specifying the files in the command line ( `ava "lib/**/*.test.js"` ). Prefer configuring AVA as described in the link above. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What link?
docs/rules/no-import-test-files.md
Outdated
// Invalid because *.test.js is considered as a test file. | ||
import tests from './index.test.js'; | ||
|
||
// File: src/index.js |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's confusing that you have multiple file examples in the same code block. They should be separate code blocks.
rules/no-import-test-files.js
Outdated
if (isImportingTestFile) { | ||
context.report({ | ||
node, | ||
message: `You are importing a test file`, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test files should not be imported
rules/no-import-test-files.js
Outdated
} | ||
}, | ||
CallExpression: node => { | ||
if (node.callee.type !== 'Identifier' || node.callee.name !== 'require') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's clearer to write it like this:
if (!(node.callee.type === 'Identifier' && node.callee.name === 'require')) {
Same with the below.
rules/no-import-test-files.js
Outdated
node, | ||
message: `You are importing a test file`, | ||
}); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Line 50 to 57 is almost duplicated with the CallExpression
one. Just make an abstracted method that accepts the value
.
rules/no-import-test-files.js
Outdated
files: { | ||
anyOf: [ | ||
{type: 'array'}, | ||
{type: 'string'} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
string
should not be allowed
Signed-off-by: Henri Beck <[email protected]>
Signed-off-by: Henri Beck <[email protected]>
Signed-off-by: Henri Beck <[email protected]>
Signed-off-by: Henri Beck <[email protected]>
Signed-off-by: Henri Beck <[email protected]>
Signed-off-by: Henri Beck <[email protected]>
Signed-off-by: Henri Beck <[email protected]>
readme.md
Outdated
@@ -74,6 +75,7 @@ The rules will only activate in test files. | |||
- [no-duplicate-modifiers](docs/rules/no-duplicate-modifiers.md) - Ensure tests do not have duplicate modifiers. | |||
- [no-identical-title](docs/rules/no-identical-title.md) - Ensure no tests have the same title. | |||
- [no-ignored-test-files](docs/rules/no-ignored-test-files.md) - Ensure no tests are written in ignored files. | |||
- [no-import-test-files](docs/rules/no-import-test-files.md) - Ensure no tests files are imported anywhere. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tests
=> test
index.js
Outdated
@@ -37,7 +37,8 @@ module.exports = { | |||
'ava/use-t-well': 'error', | |||
'ava/use-t': 'error', | |||
'ava/use-test': 'error', | |||
'ava/use-true-false': 'error' | |||
'ava/use-true-false': 'error', | |||
'ava/no-import-test-files': 'error', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to be in the same position as placed in the readme.
Signed-off-by: Henri Beck <[email protected]>
Signed-off-by: Henri Beck <[email protected]>
Signed-off-by: Henri Beck <[email protected]>
befdb58
to
6a6706e
Compare
Nice work again, @HenriBeck 🙌 |
Thanks @HenriBeck! If you're up for another challenge, we should support custom extensions with this rule. See #201. |
Will check it out next week. |
Fixes #141
Created a rule that forbids test files in any form.