Skip to content

Commit 8099d38

Browse files
committed
refactor: move all test to vitest
1 parent bd0ad3d commit 8099d38

File tree

7 files changed

+54
-65
lines changed

7 files changed

+54
-65
lines changed

package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
"@eslint/eslintrc": "^3.2.0",
3030
"@eslint/js": "^9.19.0",
3131
"@types/debug": "^4.1.7",
32-
"@types/espree": "^10.1.0",
3332
"@types/estree": "^1.0.0",
3433
"@types/fs-extra": "^11.0.4",
3534
"@types/mocha": "^9.0.0",
@@ -73,9 +72,8 @@
7372
"lint": "eslint src test package.json",
7473
"pretest": "run-s build lint",
7574
"test": "npm run -s test:mocha",
76-
"test:mocha": "mocha --require ts-node/register \"test/*.js\" --reporter dot --timeout 60000",
77-
"test:cover": "nyc mocha \"test/*.js\" --reporter dot --timeout 60000",
78-
"test:debug": "mocha --require ts-node/register/transpile-only \"test/*.js\" --reporter dot --timeout 60000",
75+
"test:vitest": "vitest",
76+
"test:cover": "vitest --coverage",
7977
"update-fixtures": "ts-node --transpile-only scripts/update-fixtures-ast.js && ts-node --transpile-only scripts/update-fixtures-document-fragment.js",
8078
"preversion": "npm test",
8179
"version": "npm run -s build",

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export function parseForESLint(
8484
* @param options The parser options.
8585
* @returns The parsing result.
8686
*/
87-
export function parse(code: string, options: any): AST.ESLintProgram {
87+
export function parse(code: string, options?: any): AST.ESLintProgram {
8888
return parseForESLint(code, options).ast
8989
}
9090

test/define-custom-blocks-visitor.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { assert, describe, it } from "vitest"
1313
import { Linter } from "eslint"
1414
import { builtinRules } from "eslint/use-at-your-own-risk"
1515
import jsonParser from "jsonc-eslint-parser"
16+
// @ts-expect-error -- ignore
1617
import * as espree from "espree"
1718
import * as parser from "../src"
1819
import type { Program } from "estree"

test/integrations.js renamed to test/integrations.test.ts

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,38 @@
22
// Requirements
33
//------------------------------------------------------------------------------
44

5-
const assert = require("assert")
6-
const path = require("path")
7-
const fs = require("fs-extra")
8-
const cp = require("child_process")
9-
const eslintCompat = require("./lib/eslint-compat")
5+
import { assert, beforeAll, describe, it } from "vitest"
6+
import path from "path"
7+
import fs from "fs-extra"
8+
import cp from "child_process"
9+
import eslintCompat from "./lib/eslint-compat"
10+
import * as ESLintRaw from "eslint"
1011

1112
//------------------------------------------------------------------------------
1213
// Helpers
1314
//------------------------------------------------------------------------------
1415

16+
// eslint-disable-next-line no-undef
1517
const FIXTURE_DIR = path.join(__dirname, "fixtures/integrations")
1618

1719
//------------------------------------------------------------------------------
1820
// Tests
1921
//------------------------------------------------------------------------------
2022

2123
describe("Integration tests", () => {
24+
beforeAll(async () => {
25+
await import("ts-node/register")
26+
})
2227
for (const target of fs.readdirSync(FIXTURE_DIR)) {
2328
it(target, async () => {
24-
let ESLint = eslintCompat(require("eslint")).ESLint
29+
let ESLint = eslintCompat(ESLintRaw).ESLint
2530
if (fs.existsSync(path.join(FIXTURE_DIR, target, "package.json"))) {
2631
const originalCwd = process.cwd()
2732
try {
2833
process.chdir(path.join(FIXTURE_DIR, target))
2934
cp.execSync("npm i", { stdio: "inherit" })
3035
ESLint = eslintCompat(
36+
// eslint-disable-next-line @typescript-eslint/no-require-imports
3137
require(
3238
path.join(
3339
FIXTURE_DIR,
@@ -46,7 +52,7 @@ describe("Integration tests", () => {
4652
})
4753
const report = await cli.lintFiles(["**/*.vue"])
4854

49-
const outputPath = path.join(FIXTURE_DIR, target, `output.json`)
55+
const outputPath = path.join(FIXTURE_DIR, target, "output.json")
5056
const expected = JSON.parse(fs.readFileSync(outputPath, "utf8"))
5157
try {
5258
assert.deepStrictEqual(
@@ -59,7 +65,7 @@ describe("Integration tests", () => {
5965
const actualPath = path.join(
6066
FIXTURE_DIR,
6167
target,
62-
`_actual.json`,
68+
"_actual.json",
6369
)
6470
fs.writeFileSync(
6571
actualPath,
@@ -72,22 +78,18 @@ describe("Integration tests", () => {
7278
function normalizeReport(report, option = {}) {
7379
return report
7480
.filter((res) => res.messages.length)
75-
.map((res) => {
76-
return {
77-
filePath: res.filePath
78-
.replace(cwd, "")
79-
.replace(/\\/gu, "/"),
80-
messages: res.messages.map((msg) => {
81-
return {
82-
ruleId: msg.ruleId,
83-
line: msg.line,
84-
...(option.withoutMessage
85-
? {}
86-
: { message: msg.message }),
87-
}
88-
}),
89-
}
90-
})
81+
.map((res) => ({
82+
filePath: res.filePath
83+
.replace(cwd, "")
84+
.replace(/\\/gu, "/"),
85+
messages: res.messages.map((msg) => ({
86+
ruleId: msg.ruleId,
87+
line: msg.line,
88+
...(option.withoutMessage
89+
? {}
90+
: { message: msg.message }),
91+
})),
92+
}))
9193
.sort((a, b) =>
9294
a.filePath < b.filePath
9395
? -1
Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,23 @@
1-
"use strict"
1+
import type { ESLint } from "eslint"
22

3-
/**
4-
* @typedef {import('eslint')} eslint
5-
*/
6-
7-
/** @param {eslint} eslint */
8-
module.exports = function compat(eslint) {
3+
export default function compat(eslint: any) {
94
return {
105
ESLint: eslint.ESLint || getESLintClassForV6(eslint),
116
RuleTester: eslint.RuleTester,
127
Linter: eslint.Linter,
138
}
149
}
1510

16-
/** @returns {typeof eslint.ESLint} */
17-
function getESLintClassForV6(eslint) {
11+
function getESLintClassForV6(eslint: any): ESLint {
1812
class ESLintForV6 {
13+
public engine
14+
1915
static get version() {
2016
return eslint.CLIEngine.version
2117
}
2218

23-
/** @param {eslint.ESLint.Options} options */
24-
constructor(options) {
19+
// eslint-disable-next-line @typescript-eslint/explicit-member-accessibility
20+
constructor(options: ESLint.Options) {
2521
const {
2622
overrideConfig: {
2723
plugins,
@@ -40,8 +36,7 @@ function getESLintClassForV6(eslint) {
4036
plugins: pluginsMap,
4137
...otherOptions
4238
} = options || {}
43-
/** @type {eslint.CLIEngine.Options} */
44-
const newOptions = {
39+
const newOptions: CLIEngine.Options = {
4540
fix: Boolean(fix),
4641
reportUnusedDisableDirectives: reportUnusedDisableDirectives
4742
? reportUnusedDisableDirectives !== "off"
@@ -64,7 +59,7 @@ function getESLintClassForV6(eslint) {
6459
}
6560
return o
6661
},
67-
/** @type {NonNullable<eslint.CLIEngine.Options["rules"]>} */ {},
62+
{} satisfies NonNullable<CLIEngine.Options["rules"]>,
6863
)
6964
: undefined,
7065
...overrideConfig,
@@ -76,41 +71,33 @@ function getESLintClassForV6(eslint) {
7671
}
7772
}
7873

79-
/**
80-
* @param {Parameters<eslint.ESLint['lintText']>} params
81-
* @returns {ReturnType<eslint.ESLint['lintText']>}
82-
*/
83-
async lintText(...params) {
74+
async lintText(
75+
...params: Parameters<ESLint["lintText"]>
76+
): ReturnType<ESLint["lintText"]> {
8477
const result = this.engine.executeOnText(
8578
params[0],
86-
params[1].filePath,
79+
params[1]!.filePath,
8780
)
8881
return result.results
8982
}
9083

91-
/**
92-
* @param {Parameters<eslint.ESLint['lintFiles']>} params
93-
* @returns {ReturnType<eslint.ESLint['lintFiles']>}
94-
*/
95-
async lintFiles(...params) {
84+
async lintFiles(
85+
...params: Parameters<ESLint["lintFiles"]>
86+
): ReturnType<ESLint["lintFiles"]> {
9687
const result = this.engine.executeOnFiles(
9788
Array.isArray(params[0]) ? params[0] : [params[0]],
9889
)
9990
return result.results
10091
}
10192

102-
/**
103-
* @param {Parameters<eslint.ESLint['outputFixes']>} params
104-
* @returns {ReturnType<eslint.ESLint['outputFixes']>}
105-
*/
106-
static async outputFixes(...params) {
93+
static async outputFixes(
94+
...params: Parameters<ESLint["outputFixes"]>
95+
): ReturnType<ESLint["outputFixes"]> {
10796
return eslint.CLIEngine.outputFixes({
10897
results: params[0],
10998
})
11099
}
111100
}
112101

113-
/** @type {typeof eslint.ESLint} */
114-
const eslintClass = /** @type {any} */ ESLintForV6
115-
return eslintClass
102+
return ESLintForV6 as any
116103
}

test/parser-options-project.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { describe, it, assert } from "vitest"
22
import { parseForESLint } from "../src"
3+
// @ts-expect-error -- ignore
34
import * as espree from "espree"
45
import type { Linter } from "eslint"
56

vitest.config.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { defineConfig } from "vitest/config"
22

33
export default defineConfig({
44
test: {
5-
include: [
6-
"test/{parser-options,crlf,define-document-visitor,define-custom-blocks-visitor,parser-options-project,document-fragment,tokens,variables-references,ast,index}.test.ts",
7-
],
5+
reporters: "dot",
6+
include: ["test/*.test.ts"],
7+
teardownTimeout: 60000,
88
},
99
})

0 commit comments

Comments
 (0)