Skip to content

Commit 644e999

Browse files
committed
⚒ fix tests on old ESLint
1 parent b57a4f9 commit 644e999

File tree

4 files changed

+113
-37
lines changed

4 files changed

+113
-37
lines changed

tests/lib/rules/file-extension-in-import.js

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,23 @@
55
"use strict"
66

77
const path = require("path")
8-
const RuleTester = require("eslint").RuleTester
8+
const { Linter, RuleTester } = require("eslint")
99
const rule = require("../../../lib/rules/file-extension-in-import")
1010

11+
const DynamicImportSupported = (() => {
12+
const config = { parserOptions: { ecmaVersion: 2020 } }
13+
const messages = new Linter().verify("import(s)", config)
14+
return messages.length === 0
15+
})()
16+
17+
if (!DynamicImportSupported) {
18+
//eslint-disable-next-line no-console
19+
console.warn(
20+
"[%s] Skip tests for 'import()'",
21+
path.basename(__filename, ".js")
22+
)
23+
}
24+
1125
function fixture(filename) {
1226
return path.resolve(
1327
__dirname,
@@ -240,20 +254,28 @@ new RuleTester({
240254
},
241255

242256
// import()
243-
{
244-
filename: fixture("test.js"),
245-
code: "function f() { import('./a') }",
246-
output: "function f() { import('./a.js') }",
247-
parserOptions: { ecmaVersion: 2020 },
248-
errors: [{ messageId: "requireExt", data: { ext: ".js" } }],
249-
},
250-
{
251-
filename: fixture("test.js"),
252-
code: "function f() { import('./a.js') }",
253-
output: "function f() { import('./a') }",
254-
options: ["never"],
255-
parserOptions: { ecmaVersion: 2020 },
256-
errors: [{ messageId: "forbidExt", data: { ext: ".js" } }],
257-
},
257+
...(DynamicImportSupported
258+
? [
259+
{
260+
filename: fixture("test.js"),
261+
code: "function f() { import('./a') }",
262+
output: "function f() { import('./a.js') }",
263+
parserOptions: { ecmaVersion: 2020 },
264+
errors: [
265+
{ messageId: "requireExt", data: { ext: ".js" } },
266+
],
267+
},
268+
{
269+
filename: fixture("test.js"),
270+
code: "function f() { import('./a.js') }",
271+
output: "function f() { import('./a') }",
272+
options: ["never"],
273+
parserOptions: { ecmaVersion: 2020 },
274+
errors: [
275+
{ messageId: "forbidExt", data: { ext: ".js" } },
276+
],
277+
},
278+
]
279+
: []),
258280
],
259281
})

tests/lib/rules/no-extraneous-import.js

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,23 @@
55
"use strict"
66

77
const path = require("path")
8-
const { RuleTester } = require("eslint")
8+
const { Linter, RuleTester } = require("eslint")
99
const rule = require("../../../lib/rules/no-extraneous-import")
1010

11+
const DynamicImportSupported = (() => {
12+
const config = { parserOptions: { ecmaVersion: 2020 } }
13+
const messages = new Linter().verify("import(s)", config)
14+
return messages.length === 0
15+
})()
16+
17+
if (!DynamicImportSupported) {
18+
//eslint-disable-next-line no-console
19+
console.warn(
20+
"[%s] Skip tests for 'import()'",
21+
path.basename(__filename, ".js")
22+
)
23+
}
24+
1125
/**
1226
* Makes a file path to a fixture.
1327
* @param {string} name - A name.
@@ -87,11 +101,15 @@ ruleTester.run("no-extraneous-import", rule, {
87101
},
88102

89103
// import()
90-
{
91-
filename: fixture("dependencies/a.js"),
92-
code: "function f() { import('bbb') }",
93-
parserOptions: { ecmaVersion: 2020 },
94-
errors: ['"bbb" is extraneous.'],
95-
},
104+
...(DynamicImportSupported
105+
? [
106+
{
107+
filename: fixture("dependencies/a.js"),
108+
code: "function f() { import('bbb') }",
109+
parserOptions: { ecmaVersion: 2020 },
110+
errors: ['"bbb" is extraneous.'],
111+
},
112+
]
113+
: []),
96114
],
97115
})

tests/lib/rules/no-missing-import.js

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,23 @@
55
"use strict"
66

77
const path = require("path")
8-
const { RuleTester } = require("eslint")
8+
const { Linter, RuleTester } = require("eslint")
99
const rule = require("../../../lib/rules/no-missing-import")
1010

11+
const DynamicImportSupported = (() => {
12+
const config = { parserOptions: { ecmaVersion: 2020 } }
13+
const messages = new Linter().verify("import(s)", config)
14+
return messages.length === 0
15+
})()
16+
17+
if (!DynamicImportSupported) {
18+
//eslint-disable-next-line no-console
19+
console.warn(
20+
"[%s] Skip tests for 'import()'",
21+
path.basename(__filename, ".js")
22+
)
23+
}
24+
1125
/**
1226
* Makes a file path to a fixture.
1327
* @param {string} name - A name.
@@ -212,11 +226,15 @@ ruleTester.run("no-missing-import", rule, {
212226
},
213227

214228
// import()
215-
{
216-
filename: fixture("test.js"),
217-
code: "function f() { import('no-exist-package-0') }",
218-
parserOptions: { ecmaVersion: 2020 },
219-
errors: ['"no-exist-package-0" is not found.'],
220-
},
229+
...(DynamicImportSupported
230+
? [
231+
{
232+
filename: fixture("test.js"),
233+
code: "function f() { import('no-exist-package-0') }",
234+
parserOptions: { ecmaVersion: 2020 },
235+
errors: ['"no-exist-package-0" is not found.'],
236+
},
237+
]
238+
: []),
221239
],
222240
})

tests/lib/rules/no-unpublished-import.js

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,23 @@
55
"use strict"
66

77
const path = require("path")
8-
const { RuleTester } = require("eslint")
8+
const { Linter, RuleTester } = require("eslint")
99
const rule = require("../../../lib/rules/no-unpublished-import")
1010

11+
const DynamicImportSupported = (() => {
12+
const config = { parserOptions: { ecmaVersion: 2020 } }
13+
const messages = new Linter().verify("import(s)", config)
14+
return messages.length === 0
15+
})()
16+
17+
if (!DynamicImportSupported) {
18+
//eslint-disable-next-line no-console
19+
console.warn(
20+
"[%s] Skip tests for 'import()'",
21+
path.basename(__filename, ".js")
22+
)
23+
}
24+
1125
/**
1226
* Makes a file path to a fixture.
1327
* @param {string} name - A name.
@@ -246,11 +260,15 @@ ruleTester.run("no-unpublished-import", rule, {
246260
},
247261

248262
// import()
249-
{
250-
filename: fixture("2/test.js"),
251-
code: "function f() { import('./ignore1.js') }",
252-
parserOptions: { ecmaVersion: 2020 },
253-
errors: ['"./ignore1.js" is not published.'],
254-
},
263+
...(DynamicImportSupported
264+
? [
265+
{
266+
filename: fixture("2/test.js"),
267+
code: "function f() { import('./ignore1.js') }",
268+
parserOptions: { ecmaVersion: 2020 },
269+
errors: ['"./ignore1.js" is not published.'],
270+
},
271+
]
272+
: []),
255273
],
256274
})

0 commit comments

Comments
 (0)