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 (when using relative path) #911

Merged
merged 1 commit into from Aug 13, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/rules/no-test-import-export.js
Expand Up @@ -4,6 +4,7 @@

'use strict';

const { dirname, resolve } = require('path');
const emberUtils = require('../utils/ember');

const NO_IMPORT_MESSAGE =
Expand Down Expand Up @@ -45,7 +46,10 @@ module.exports = {
ImportDeclaration(node) {
const importSource = node.source.value;

if (importSource.endsWith('-test') && !isTestHelperImportSource(importSource)) {
if (
importSource.endsWith('-test') &&
!isTestHelperImportSource(resolve(dirname(context.getFilename()), importSource))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ember makes some of the import paths difficult to resolve, since it does things like drop the /addon or /app. I'm not sure you're able to import things from the dummy app in your tests, but if you can this might not resolve correctly.

Copy link
Member Author

Choose a reason for hiding this comment

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

That's true. I'm not sure how we could handle that, but fortunately it should be very unlikely for that to affect us with test helper imports.

) {
context.report({
message: NO_IMPORT_MESSAGE,
node,
Expand Down
21 changes: 19 additions & 2 deletions tests/lib/rules/no-test-import-export.js
Expand Up @@ -40,10 +40,20 @@ ruleTester.run('no-test-file-importing', rule, {
},

// Importing anything from tests/helpers is allowed.
"import setupApplicationTest from 'tests/helpers/setup-application-test.js';",
"import setupApplicationTest from 'tests/helpers/setup-application-test';",
"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/setup-application-test';",
"import { setupApplicationTest } from 'my-app-name/tests/helpers';",

// Importing anything from test/helpers is allowed (using relative path)
{
filename: 'my-app-name/tests/helpers/foo.js',
code: "import setupApplicationTest from './setup-application-test';",
},
{
filename: 'my-app-name/tests/helpers/nested/foo.js',
code: "import setupApplicationTest from '../setup-application-test';",
},
],
invalid: [
{
Expand Down Expand Up @@ -91,6 +101,13 @@ ruleTester.run('no-test-file-importing', rule, {
},
],
},
{
// Importing from a test file outside test/helpers is disallowed.
filename: 'my-app-name/tests/helpers/foo.js',
code: "import testModule from '../../test-dir/another-test';",
output: null,
errors: [{ message: NO_IMPORT_MESSAGE }],
},
{
filename: 'tests/some-test.js',
code: `
Expand Down