Skip to content

Commit

Permalink
fix: no-test-import-export rule should allow importing from anything …
Browse files Browse the repository at this point in the history
…under tests/helpers path
  • Loading branch information
bmish committed Jul 19, 2020
1 parent aa7b664 commit 68470e0
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
11 changes: 11 additions & 0 deletions docs/rules/no-test-import-export.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,14 @@ function beforeEachSetup() {}

export default { beforeEachSetup };
```

```javascript
// Any imports from `tests/helpers` are allowed.
import { setupApplicationTest } from 'tests/helpers/setup-application-test';
```

```javascript
// Any exports from `tests/helpers` are allowed.
// tests/helpers/setup-application-test.js
export default function setupApplicationTest() {}
```
20 changes: 18 additions & 2 deletions lib/rules/no-test-import-export.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ module.exports = {

create: function create(context) {
const noExports = function (node) {
if (!emberUtils.isTestFile(context.getFilename())) {
if (
!emberUtils.isTestFile(context.getFilename()) ||
isTestHelperFilename(context.getFilename())
) {
return;
}

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

if (importSource.endsWith('-test')) {
if (importSource.endsWith('-test') && !isTestHelperImportSource(importSource)) {
context.report({
message: NO_IMPORT_MESSAGE,
node,
Expand All @@ -58,3 +61,16 @@ module.exports = {
};
},
};

function isTestHelperImportSource(importSource) {
return (
importSource.startsWith('tests/helpers/') ||
importSource.includes('/tests/helpers/') ||
importSource.endsWith('/tests/helpers') ||
importSource === 'tests/helpers'
);
}

function isTestHelperFilename(filename) {
return filename.startsWith('tests/helpers/') || filename.includes('/tests/helpers/');
}
20 changes: 20 additions & 0 deletions tests/lib/rules/no-test-import-export.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,26 @@ ruleTester.run('no-test-file-importing', rule, {
export function beforeEachSetup(){};
`,
},

// Exporting from files in tests/helpers is allowed.
{
filename: 'tests/helpers/setup-application-test.js',
code: 'export default function setupApplicationTest(){};',
},
{
filename: 'tests/helpers/index.js',
code: 'export function setupApplicationTest(){};',
},
{
filename: 'my-app-name/tests/helpers/setup-application-test.js',
code: 'export default function setupApplicationTest(){};',
},

// Importing anything from tests/helpers is allowed.
"import setupApplicationTest from 'tests/helpers/setup-application-test.js';",
"import { setupApplicationTest } from 'tests/helpers';",
"import setupApplicationTest from 'my-app-name/tests/helpers/setup-application-test.js';",
"import { setupApplicationTest } from 'my-app-name/tests/helpers';",
],
invalid: [
{
Expand Down

0 comments on commit 68470e0

Please sign in to comment.