-
-
Notifications
You must be signed in to change notification settings - Fork 204
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
} | ||
|
||
|
@@ -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, | ||
|
@@ -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' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it valid for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure. I suppose this line would handle either a file named There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, it is definitely possible (and allowed) to use a single |
||
); | ||
} | ||
|
||
function isTestHelperFilename(filename) { | ||
return filename.startsWith('tests/helpers/') || filename.includes('/tests/helpers/'); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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';", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We are missing relative path test case, for example:
-- 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 It seems plugin should resolve real path on disk with There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: [ | ||
{ | ||
|
There was a problem hiding this comment.
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).There was a problem hiding this comment.
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.