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

Update no-test-import-export rule to allow importing from anything under tests/helpers path #895

Merged
merged 1 commit into from
Jul 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)) {
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, I'm not 100% sure this is correct. We shouldn't be importing from tests/helpers/something-test.js (e.g. I think the first condition was fine, any file ending with -test is itself a test file not a helper file).

Copy link
Member

Choose a reason for hiding this comment

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

I see why it was done. If you literally write a module like tests/helpers/some-verb-test.js it would still error.

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'
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is it valid for tests/helpers to be a single file instead of a directory?

Copy link
Member Author

@bmish bmish Jul 20, 2020

Choose a reason for hiding this comment

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

Not sure. I suppose this line would handle either a file named tests/helpers.js or a file named tests/helpers/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.

Is it valid for tests/helpers to be a single file instead of a directory?

Yes, it is definitely possible (and allowed) to use a single tests/helpers.js file or a tests/helpers/index.js file.

);
}

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';",
Copy link
Contributor

@raido raido Aug 12, 2020

Choose a reason for hiding this comment

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

We are missing relative path test case, for example:

../../helpers/setup-application-test.js

--

I upgraded eslint-plugin-ember to v8.10.1 and my ESLint still complains about imports from helpers folder, because relative import paths are being used.

I can workaround by changing to absolute path (TypeScript app), like myapp/tests/helpers/.

It seems plugin should resolve real path on disk with path.resolve or something to determine truthiness in isTestHelperFilename method.

Copy link
Member Author

Choose a reason for hiding this comment

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

This should fix it: #911

"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