|
| 1 | +"use strict" |
| 2 | + |
| 3 | +const assert = require("assert") |
| 4 | +const path = require("path") |
| 5 | +const { CLIEngine } = require("eslint") |
| 6 | +const originalCwd = process.cwd() |
| 7 | + |
| 8 | +describe("node/recommended config", () => { |
| 9 | + describe("in CJS directory", () => { |
| 10 | + const root = path.resolve(__dirname, "../../fixtures/configs/cjs/") |
| 11 | + |
| 12 | + /** @type {CLIEngine} */ |
| 13 | + let engine = null |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + process.chdir(root) |
| 17 | + engine = new CLIEngine({ |
| 18 | + baseConfig: { extends: "plugin:node/recommended" }, |
| 19 | + useEslintrc: false, |
| 20 | + }) |
| 21 | + }) |
| 22 | + |
| 23 | + afterEach(() => { |
| 24 | + process.chdir(originalCwd) |
| 25 | + }) |
| 26 | + |
| 27 | + it("*.js files should be a script.", () => { |
| 28 | + const report = engine.executeOnText( |
| 29 | + "import 'foo'", |
| 30 | + path.join(root, "test.js") |
| 31 | + ) |
| 32 | + |
| 33 | + assert.deepStrictEqual(report.results[0].messages, [ |
| 34 | + { |
| 35 | + column: 1, |
| 36 | + fatal: true, |
| 37 | + line: 1, |
| 38 | + message: |
| 39 | + "Parsing error: 'import' and 'export' may appear only with 'sourceType: module'", |
| 40 | + ruleId: null, |
| 41 | + severity: 2, |
| 42 | + }, |
| 43 | + ]) |
| 44 | + }) |
| 45 | + |
| 46 | + it("*.cjs files should be a script.", () => { |
| 47 | + const report = engine.executeOnText( |
| 48 | + "import 'foo'", |
| 49 | + path.join(root, "test.cjs") |
| 50 | + ) |
| 51 | + |
| 52 | + assert.deepStrictEqual(report.results[0].messages, [ |
| 53 | + { |
| 54 | + column: 1, |
| 55 | + fatal: true, |
| 56 | + line: 1, |
| 57 | + message: |
| 58 | + "Parsing error: 'import' and 'export' may appear only with 'sourceType: module'", |
| 59 | + ruleId: null, |
| 60 | + severity: 2, |
| 61 | + }, |
| 62 | + ]) |
| 63 | + }) |
| 64 | + |
| 65 | + it("*.mjs files should be a module.", () => { |
| 66 | + const report = engine.executeOnText( |
| 67 | + "import 'foo'", |
| 68 | + path.join(root, "test.mjs") |
| 69 | + ) |
| 70 | + |
| 71 | + assert.deepStrictEqual(report.results[0].messages, [ |
| 72 | + { |
| 73 | + column: 8, |
| 74 | + endColumn: 13, |
| 75 | + endLine: 1, |
| 76 | + line: 1, |
| 77 | + message: '"foo" is not found.', |
| 78 | + nodeType: "Literal", |
| 79 | + ruleId: "node/no-missing-import", |
| 80 | + severity: 2, |
| 81 | + }, |
| 82 | + ]) |
| 83 | + }) |
| 84 | + }) |
| 85 | + |
| 86 | + describe("in ESM directory", () => { |
| 87 | + const root = path.resolve(__dirname, "../../fixtures/configs/esm/") |
| 88 | + |
| 89 | + /** @type {CLIEngine} */ |
| 90 | + let engine = null |
| 91 | + |
| 92 | + beforeEach(() => { |
| 93 | + process.chdir(root) |
| 94 | + engine = new CLIEngine({ |
| 95 | + baseConfig: { extends: "plugin:node/recommended" }, |
| 96 | + useEslintrc: false, |
| 97 | + }) |
| 98 | + }) |
| 99 | + |
| 100 | + afterEach(() => { |
| 101 | + process.chdir(originalCwd) |
| 102 | + }) |
| 103 | + |
| 104 | + it("*.js files should be a module.", () => { |
| 105 | + const report = engine.executeOnText( |
| 106 | + "import 'foo'", |
| 107 | + path.join(root, "test.js") |
| 108 | + ) |
| 109 | + |
| 110 | + assert.deepStrictEqual(report.results[0].messages, [ |
| 111 | + { |
| 112 | + column: 8, |
| 113 | + endColumn: 13, |
| 114 | + endLine: 1, |
| 115 | + line: 1, |
| 116 | + message: '"foo" is not found.', |
| 117 | + nodeType: "Literal", |
| 118 | + ruleId: "node/no-missing-import", |
| 119 | + severity: 2, |
| 120 | + }, |
| 121 | + ]) |
| 122 | + }) |
| 123 | + |
| 124 | + it("*.cjs files should be a script.", () => { |
| 125 | + const report = engine.executeOnText( |
| 126 | + "import 'foo'", |
| 127 | + path.join(root, "test.cjs") |
| 128 | + ) |
| 129 | + |
| 130 | + assert.deepStrictEqual(report.results[0].messages, [ |
| 131 | + { |
| 132 | + column: 1, |
| 133 | + fatal: true, |
| 134 | + line: 1, |
| 135 | + message: |
| 136 | + "Parsing error: 'import' and 'export' may appear only with 'sourceType: module'", |
| 137 | + ruleId: null, |
| 138 | + severity: 2, |
| 139 | + }, |
| 140 | + ]) |
| 141 | + }) |
| 142 | + |
| 143 | + it("*.mjs files should be a module.", () => { |
| 144 | + const report = engine.executeOnText( |
| 145 | + "import 'foo'", |
| 146 | + path.join(root, "test.mjs") |
| 147 | + ) |
| 148 | + |
| 149 | + assert.deepStrictEqual(report.results[0].messages, [ |
| 150 | + { |
| 151 | + column: 8, |
| 152 | + endColumn: 13, |
| 153 | + endLine: 1, |
| 154 | + line: 1, |
| 155 | + message: '"foo" is not found.', |
| 156 | + nodeType: "Literal", |
| 157 | + ruleId: "node/no-missing-import", |
| 158 | + severity: 2, |
| 159 | + }, |
| 160 | + ]) |
| 161 | + }) |
| 162 | + }) |
| 163 | +}) |
0 commit comments