Skip to content

Commit 68470e0

Browse files
committed
fix: no-test-import-export rule should allow importing from anything under tests/helpers path
1 parent aa7b664 commit 68470e0

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

docs/rules/no-test-import-export.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,14 @@ function beforeEachSetup() {}
6767

6868
export default { beforeEachSetup };
6969
```
70+
71+
```javascript
72+
// Any imports from `tests/helpers` are allowed.
73+
import { setupApplicationTest } from 'tests/helpers/setup-application-test';
74+
```
75+
76+
```javascript
77+
// Any exports from `tests/helpers` are allowed.
78+
// tests/helpers/setup-application-test.js
79+
export default function setupApplicationTest() {}
80+
```

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ module.exports = {
2828

2929
create: function create(context) {
3030
const noExports = function (node) {
31-
if (!emberUtils.isTestFile(context.getFilename())) {
31+
if (
32+
!emberUtils.isTestFile(context.getFilename()) ||
33+
isTestHelperFilename(context.getFilename())
34+
) {
3235
return;
3336
}
3437

@@ -42,7 +45,7 @@ module.exports = {
4245
ImportDeclaration(node) {
4346
const importSource = node.source.value;
4447

45-
if (importSource.endsWith('-test')) {
48+
if (importSource.endsWith('-test') && !isTestHelperImportSource(importSource)) {
4649
context.report({
4750
message: NO_IMPORT_MESSAGE,
4851
node,
@@ -58,3 +61,16 @@ module.exports = {
5861
};
5962
},
6063
};
64+
65+
function isTestHelperImportSource(importSource) {
66+
return (
67+
importSource.startsWith('tests/helpers/') ||
68+
importSource.includes('/tests/helpers/') ||
69+
importSource.endsWith('/tests/helpers') ||
70+
importSource === 'tests/helpers'
71+
);
72+
}
73+
74+
function isTestHelperFilename(filename) {
75+
return filename.startsWith('tests/helpers/') || filename.includes('/tests/helpers/');
76+
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,26 @@ ruleTester.run('no-test-file-importing', rule, {
2424
export function beforeEachSetup(){};
2525
`,
2626
},
27+
28+
// Exporting from files in tests/helpers is allowed.
29+
{
30+
filename: 'tests/helpers/setup-application-test.js',
31+
code: 'export default function setupApplicationTest(){};',
32+
},
33+
{
34+
filename: 'tests/helpers/index.js',
35+
code: 'export function setupApplicationTest(){};',
36+
},
37+
{
38+
filename: 'my-app-name/tests/helpers/setup-application-test.js',
39+
code: 'export default function setupApplicationTest(){};',
40+
},
41+
42+
// Importing anything from tests/helpers is allowed.
43+
"import setupApplicationTest from 'tests/helpers/setup-application-test.js';",
44+
"import { setupApplicationTest } from 'tests/helpers';",
45+
"import setupApplicationTest from 'my-app-name/tests/helpers/setup-application-test.js';",
46+
"import { setupApplicationTest } from 'my-app-name/tests/helpers';",
2747
],
2848
invalid: [
2949
{

0 commit comments

Comments
 (0)