Skip to content

Commit 96b46bb

Browse files
Merge pull request #2549 from johanrd/issue/1218-test-file-false-positive
[BUGFIX]: `no-test-import-export` false positive on non-test files ending in -test
2 parents fcabcaa + d697f65 commit 96b46bb

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

lib/rules/no-test-import-export.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ module.exports = {
3131
const noExports = function (node) {
3232
if (
3333
!emberUtils.isTestFile(context.getFilename()) ||
34+
!isInTestDirectory(context.getFilename()) ||
3435
isTestHelperFilename(context.getFilename())
3536
) {
3637
return;
@@ -67,6 +68,15 @@ module.exports = {
6768
},
6869
};
6970

71+
function isInTestDirectory(filename) {
72+
const filenameParts = path.normalize(filename).split(path.sep);
73+
const testDirIndex = filenameParts.findIndex((part) => part === 'tests' || part === 'test');
74+
// tests/ or test/ must be a top-level directory (index 0) or directly under
75+
// a project name (index 1, e.g. my-app/tests/). Deeper nesting like
76+
// app/components/tests/ is not a real test directory.
77+
return testDirIndex >= 0 && testDirIndex <= 1;
78+
}
79+
7080
function isTestHelperFilename(filename) {
7181
const filenameParts = path.normalize(filename).split(path.sep);
7282
for (let i = 0; i < filenameParts.length - 1; ++i) {

tests/lib/rules/no-test-import-export.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,17 @@ ruleTester.run('no-test-import-export', rule, {
5858
// Package imports ending in -test should not be flagged
5959
"import { expectTypeOf } from '@glint/type-test';",
6060
"import something from 'some-package-test';",
61+
62+
// Files ending in -test outside tests/ directory should not be treated as test files (#1218)
63+
{
64+
filename: 'app/components/student-test.js',
65+
code: 'export function setup() {}',
66+
},
67+
// A tests/ folder inside app/ is not a real test directory
68+
{
69+
filename: 'app/components/tests/foo-test.js',
70+
code: 'export function setup() {}',
71+
},
6172
],
6273
invalid: [
6374
{

0 commit comments

Comments
 (0)