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

[Tests] no-unresolved: add tests for import() #2012

Merged
merged 1 commit into from May 13, 2021
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -36,6 +36,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
- [Docs] [`no-named-as-default`]: add semicolon ([#1897], thanks [@bicstone])
- [Docs] `no-extraneous-dependencies`: correct peerDependencies option default to `true` ([#1993], thanks [@dwardu])
- [Docs] `order`: Document options required to match ordering example ([#1992], thanks [@silviogutierrez])
- [Tests] `no-unresolved`: add tests for `import()` ([#2012], thanks [@davidbonnet])

## [2.22.1] - 2020-09-27
### Fixed
Expand Down Expand Up @@ -769,6 +770,7 @@ for info on changes for earlier releases.
[#2026]: https://github.com/benmosher/eslint-plugin-import/pull/2026
[#2022]: https://github.com/benmosher/eslint-plugin-import/pull/2022
[#2021]: https://github.com/benmosher/eslint-plugin-import/pull/2021
[#2012]: https://github.com/benmosher/eslint-plugin-import/pull/2012
[#1997]: https://github.com/benmosher/eslint-plugin-import/pull/1997
[#1993]: https://github.com/benmosher/eslint-plugin-import/pull/1993
[#1985]: https://github.com/benmosher/eslint-plugin-import/pull/1985
Expand Down Expand Up @@ -1360,3 +1362,4 @@ for info on changes for earlier releases.
[@lilling]: https://github.com/lilling
[@silviogutierrez]: https://github.com/silviogutierrez
[@aladdin-add]: https://github.com/aladdin-add
[@davidbonnet]: https://github.com/davidbonnet
30 changes: 23 additions & 7 deletions tests/src/rules/no-unresolved.js
Expand Up @@ -22,7 +22,7 @@ function runResolverTests(resolver) {
}

ruleTester.run(`no-unresolved (${resolver})`, rule, {
valid: [
valid: [].concat(
test({ code: 'import "./malformed.js"' }),

rest({ code: 'import foo from "./bar";' }),
Expand All @@ -32,6 +32,12 @@ function runResolverTests(resolver) {
rest({ code: "import('fs');",
parser: require.resolve('babel-eslint') }),

// check with eslint parser
testVersion('>= 7', () => rest({
code: "import('fs');",
parserOptions: { ecmaVersion: 2021 },
})) || [],

rest({ code: 'import * as foo from "a"' }),

rest({ code: 'export { foo } from "./bar"' }),
Expand Down Expand Up @@ -83,9 +89,9 @@ function runResolverTests(resolver) {
options: [{ commonjs: true }] }),
rest({ code: 'require(foo)',
options: [{ commonjs: true }] }),
],
),

invalid: [
invalid: [].concat(
Copy link
Contributor

Choose a reason for hiding this comment

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

just curious why the [].concat() is needed. :)

Copy link
Member

Choose a reason for hiding this comment

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

because RuleTester requires an array of objects, so there's no way to conditionally provide an ignoreable value, so we have to use [].concat() and have the conditional helper return an empty array, since concat flatMaps over arrays.

rest({
code: 'import reallyfake from "./reallyfake/module"',
settings: { 'import/ignore': ['^\\./fake/'] },
Expand Down Expand Up @@ -117,9 +123,9 @@ function runResolverTests(resolver) {
}] }),
rest({
code: "import('in-alternate-root').then(function({DEEP}){});",
errors: [{ message: 'Unable to resolve path to ' +
"module 'in-alternate-root'.",
type: 'Literal',
errors: [{
message: 'Unable to resolve path to module \'in-alternate-root\'.',
type: 'Literal',
}],
parser: require.resolve('babel-eslint') }),

Expand All @@ -130,6 +136,16 @@ function runResolverTests(resolver) {
errors: ["Unable to resolve path to module './does-not-exist'."],
}),

// check with eslint parser
testVersion('>= 7', () => rest({
code: "import('in-alternate-root').then(function({DEEP}){});",
errors: [{
message: 'Unable to resolve path to module \'in-alternate-root\'.',
type: 'Literal',
}],
parserOptions: { ecmaVersion: 2021 },
})) || [],

// export symmetry proposal
rest({ code: 'export * as bar from "./does-not-exist"',
parser: require.resolve('babel-eslint'),
Expand Down Expand Up @@ -186,7 +202,7 @@ function runResolverTests(resolver) {
type: 'Literal',
}],
}),
],
),
});

ruleTester.run(`issue #333 (${resolver})`, rule, {
Expand Down