Skip to content

Commit d0804ec

Browse files
committed
feat: move document-fragment to vitest
1 parent 1918ef5 commit d0804ec

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

test/document-fragment.js renamed to test/document-fragment.test.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
"use strict"
2-
31
//------------------------------------------------------------------------------
42
// Requirements
53
//------------------------------------------------------------------------------
64

7-
const assert = require("assert")
8-
const fs = require("fs")
9-
const path = require("path")
10-
const parser = require("../src")
5+
import type { VDocumentFragment } from "../src/ast"
6+
import fs from "fs"
7+
import path from "path"
8+
import { describe, it, assert } from "vitest"
9+
import * as parser from "../src"
1110

1211
//------------------------------------------------------------------------------
1312
// Helpers
1413
//------------------------------------------------------------------------------
1514

15+
// eslint-disable-next-line no-undef
1616
const ROOT = path.join(__dirname, "fixtures/document-fragment")
1717
const TARGETS = fs.readdirSync(ROOT)
1818
const PARSER_OPTIONS = {
@@ -26,11 +26,11 @@ const PARSER_OPTIONS = {
2626

2727
/**
2828
* Remove `parent` proeprties from the given AST.
29-
* @param {string} key The key.
30-
* @param {any} value The value of the key.
31-
* @returns {any} The value of the key to output.
29+
* @param key The key.
30+
* @param value The value of the key.
31+
* @returns The value of the key to output.
3232
*/
33-
function replacer(key, value) {
33+
function replacer(key: string, value: any): any {
3434
if (key === "parent") {
3535
return undefined
3636
}
@@ -60,14 +60,15 @@ describe("services.getDocumentFragment", () => {
6060
path.join(ROOT, `${name}/parser-options.js`),
6161
].find((fp) => fs.existsSync(fp))
6262
const source = fs.readFileSync(sourcePath, "utf8")
63+
// eslint-disable-next-line @typescript-eslint/no-require-imports
6364
const parserOptions = optionsPath ? require(optionsPath) : {}
6465
const options = {
6566
filePath: sourcePath,
6667
...PARSER_OPTIONS,
6768
...parserOptions,
6869
}
6970
const result = parser.parseForESLint(source, options)
70-
const actual = result.services.getDocumentFragment()
71+
const actual = result.services!.getDocumentFragment()
7172

7273
describe(`'test/fixtures/document-fragment/${name}/${sourceFileName}'`, () => {
7374
it("should be parsed to valid document fragment.", () => {
@@ -86,7 +87,7 @@ describe("services.getDocumentFragment", () => {
8687
it("should have correct range.", () => {
8788
const resultPath = path.join(ROOT, `${name}/token-ranges.json`)
8889
const expectedText = fs.readFileSync(resultPath, "utf8")
89-
const tokens = getAllTokens(actual).map((t) =>
90+
const tokens = getAllTokens(actual!).map((t) =>
9091
source.slice(t.range[0], t.range[1]),
9192
)
9293
const actualText = JSON.stringify(tokens, null, 4)
@@ -97,7 +98,7 @@ describe("services.getDocumentFragment", () => {
9798
}
9899
})
99100

100-
function getAllTokens(fgAst) {
101+
function getAllTokens(fgAst: VDocumentFragment) {
101102
const tokenArrays = [fgAst.tokens, fgAst.comments]
102103

103104
return Array.prototype.concat.apply([], tokenArrays)

vitest.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { defineConfig } from "vitest/config"
33
export default defineConfig({
44
test: {
55
include: [
6-
"test/{parser-options,crlf,define-document-visitor,define-custom-blocks-visitor,parser-options-project}.test.ts",
6+
"test/{parser-options,crlf,define-document-visitor,define-custom-blocks-visitor,parser-options-project,document-fragment}.test.ts",
77
],
88
},
99
})

0 commit comments

Comments
 (0)