Skip to content

Commit

Permalink
Merge pull request #911 from bmish/no-test-import-export-helpers-fix-2
Browse files Browse the repository at this point in the history
Update `no-test-import-export` rule to allow importing from anything under `tests/helpers` path (when using relative path)
  • Loading branch information
bmish committed Aug 13, 2020
2 parents 9334f3a + 2998e44 commit 50bdf60
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
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))
) {
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

0 comments on commit 50bdf60

Please sign in to comment.