Skip to content

Commit c383cf6

Browse files
GMartignysindresorhus
authored andcommitted
Fix no-import-test-files rule incorrectly reporting module names ending in .js (#226)
1 parent 521d009 commit c383cf6

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

rules/no-import-test-files.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ const pkgUp = require('pkg-up');
55
const multimatch = require('multimatch');
66
const util = require('../util');
77

8+
const externalModuleRegExp = /^\w/;
9+
function isExternalModule(name) {
10+
return externalModuleRegExp.test(name);
11+
}
12+
813
function isTestFile(files, rootDir, sourceFile, importedFile) {
914
const absoluteImportedPath = path.resolve(path.dirname(sourceFile), importedFile);
1015
const relativePath = path.relative(rootDir, absoluteImportedPath);
@@ -23,8 +28,12 @@ function getProjectInfo() {
2328

2429
function createImportValidator(context, files, projectInfo, filename) {
2530
return (node, importPath) => {
26-
const isImportingTestFile = isTestFile(files, projectInfo.rootDir, filename, importPath);
31+
const isImportingExternalModule = isExternalModule(importPath);
32+
if (isImportingExternalModule) {
33+
return;
34+
}
2735

36+
const isImportingTestFile = isTestFile(files, projectInfo.rootDir, filename, importPath);
2837
if (isImportingTestFile) {
2938
context.report({
3039
node,

test/no-import-test-files.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,22 @@ const rootDir = path.dirname(__dirname);
1717
const toPath = subPath => path.join(rootDir, subPath);
1818

1919
util.getAvaConfig = () => ({
20-
files: ['lib/*.test.js']
20+
files: [
21+
'lib/*.test.js',
22+
'test/**/*.js'
23+
]
2124
});
2225

2326
ruleTester.run('no-import-test-files', rule, {
2427
valid: [
28+
'import test from \'ava\';',
29+
'const test = require(\'ava\');',
30+
'console.log()',
31+
'const value = require(somePath);',
32+
'var highlight = require(\'highlight.js\')',
2533
{
26-
code: 'import test from \'ava\';'
27-
},
28-
{
29-
code: 'const test = require(\'ava\');'
30-
},
31-
{
32-
code: 'console.log()'
33-
},
34-
{
35-
code: 'const value = require(somePath);'
34+
code: 'var highlight = require(\'highlight.js\')',
35+
filename: toPath('test/index.js')
3636
}
3737
],
3838
invalid: [
@@ -45,6 +45,11 @@ ruleTester.run('no-import-test-files', rule, {
4545
code: 'import test from \'./foo.test.js\';',
4646
filename: toPath('lib/foo.js'),
4747
errors: [{message: 'Test files should not be imported'}]
48+
},
49+
{
50+
code: 'import test from \'./bar.js\';',
51+
filename: toPath('test/foo.js'),
52+
errors: [{message: 'Test files should not be imported'}]
4853
}
4954
]
5055
});

0 commit comments

Comments
 (0)