Skip to content
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

Merged
merged 15 commits into from
Jul 14, 2018

Conversation

HenriBeck
Copy link
Contributor

@HenriBeck HenriBeck commented Jun 1, 2018

Fixes #141

Created a rule that forbids test files in any form.

Copy link
Member

@novemberborn novemberborn left a 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.)

const absoluteImportedPath = path.resolve(path.dirname(sourceFile), importedFile);
const relativePath = path.relative(rootDir, absoluteImportedPath);

console.log(sourceFile, importedFile, absoluteImportedPath, relativePath);
Copy link
Member

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?

Copy link
Contributor Author

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.

if (isImportingTestFile) {
context.report({
node,
message: `You are importing a test file`,
Copy link
Member

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?

Copy link
Contributor Author

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.

return {};
}

const packageInfo = getPackageInfo();
Copy link
Member

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.

Henri Beck added 2 commits June 16, 2018 11:01
- Renamed function getPackageInfo to getProjectInfo

Signed-off-by: Henri Beck <[email protected]>
Signed-off-by: Henri Beck <[email protected]>
@HenriBeck
Copy link
Contributor Author

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.

@sindresorhus sindresorhus changed the title Added no-import-test-file Add no-import-test-file rule Jun 24, 2018
@sindresorhus
Copy link
Member

@avajs/core should this be a recommended rule?

Yes, this should be a recommended rule.

@sindresorhus
Copy link
Member

This rule needs to be added to the readme.

'test/**/*.js',
'**/__tests__/**/*.js',
'**/*.test.js'
];
Copy link
Member

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)

@@ -0,0 +1,60 @@
# Ensure no tests are imported anywhere
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tests => test files

# 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.

Copy link
Member

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


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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What link?

// Invalid because *.test.js is considered as a test file.
import tests from './index.test.js';

// File: src/index.js
Copy link
Member

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.

if (isImportingTestFile) {
context.report({
node,
message: `You are importing a test file`,
Copy link
Member

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

}
},
CallExpression: node => {
if (node.callee.type !== 'Identifier' || node.callee.name !== 'require') {
Copy link
Member

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.

node,
message: `You are importing a test file`,
});
}
Copy link
Member

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.

files: {
anyOf: [
{type: 'array'},
{type: 'string'}
Copy link
Member

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

Henri Beck added 7 commits June 26, 2018 10:08
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.
Copy link
Member

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',
Copy link
Member

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.

Henri Beck and others added 4 commits June 27, 2018 09:00
Signed-off-by: Henri Beck <[email protected]>
Signed-off-by: Henri Beck <[email protected]>
@sindresorhus sindresorhus force-pushed the master branch 2 times, most recently from befdb58 to 6a6706e Compare July 14, 2018 18:27
@sindresorhus sindresorhus merged commit 0134e94 into avajs:master Jul 14, 2018
@sindresorhus
Copy link
Member

Nice work again, @HenriBeck 🙌

@HenriBeck HenriBeck deleted the add-no-import-test-files-rule branch July 14, 2018 18:35
@novemberborn
Copy link
Member

Thanks @HenriBeck! If you're up for another challenge, we should support custom extensions with this rule. See #201.

@HenriBeck
Copy link
Contributor Author

Will check it out next week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants