-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: use table test to snapshot fixtures
- Loading branch information
Showing
92 changed files
with
1,847 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import fg from "fast-glob"; | ||
import fsp from "node:fs/promises"; | ||
import path from "node:path"; | ||
import { describe, expect, test } from "vitest"; | ||
import ComponentParser from "../src/ComponentParser"; | ||
import Writer from "../src/writer/Writer"; | ||
import { writeTsDefinition } from "../src/writer/writer-ts-definitions"; | ||
|
||
const folder = path.join(process.cwd(), "tests", "fixtures"); | ||
|
||
const svelteFiles = fg.sync(["**/*.svelte"], { cwd: folder }); | ||
const fixtures = await Promise.all( | ||
svelteFiles.map(async (file) => { | ||
return { | ||
path: file, | ||
source: await fsp.readFile(path.join(folder, file), "utf-8"), | ||
}; | ||
}) | ||
); | ||
|
||
describe("fixtures", async () => { | ||
const parser = new ComponentParser(); | ||
const writer = new Writer({ parser: "typescript", printWidth: 120 }); | ||
|
||
test.each(fixtures)("$path", async (fixture) => { | ||
const { dir } = path.parse(fixture.path); | ||
const moduleName = dir | ||
.split("-") | ||
.map((s) => s.charAt(0).toUpperCase() + s.slice(1)) | ||
.join(""); | ||
const metadata = { moduleName, filePath: fixture.path }; | ||
const parsed_component = parser.parseSvelteComponent(fixture.source, metadata); | ||
|
||
const api_json = JSON.stringify(parsed_component, null, 2); | ||
const api_ts = writer.format(writeTsDefinition({ ...metadata, ...parsed_component })); | ||
|
||
// Snapshot the output; if the test fails, output has changed. | ||
expect(api_json).toMatchSnapshot(); | ||
expect(api_ts).toMatchSnapshot(); | ||
|
||
// Still write to disk to manually assert types as needed. | ||
await fsp.writeFile(path.join(folder, "output.json"), api_json); | ||
await fsp.writeFile(path.join(folder, "output.d.ts"), api_ts); | ||
}); | ||
}); |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import type { SvelteComponentTyped } from "svelte"; | ||
|
||
export interface MyTypedef { | ||
[key: string]: boolean; | ||
} | ||
|
||
export type MyTypedefArray = MyTypedef[]; | ||
|
||
export interface TypedefsProps { | ||
/** | ||
* @default { ["1"]: true } | ||
*/ | ||
prop1?: MyTypedef; | ||
|
||
/** | ||
* @default [] | ||
*/ | ||
prop2?: MyTypedefArray; | ||
} | ||
|
||
export default class Typedefs extends SvelteComponentTyped< | ||
TypedefsProps, | ||
Record<string, any>, | ||
{ default: { prop1: MyTypedef; prop2: MyTypedefArray } } | ||
> {} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
{ | ||
"props": [ | ||
{ | ||
"name": "prop1", | ||
"kind": "let", | ||
"type": "MyTypedef", | ||
"value": "{ [\"1\"]: true }", | ||
"isFunction": false, | ||
"isFunctionDeclaration": false, | ||
"isRequired": false, | ||
"constant": false, | ||
"reactive": false | ||
}, | ||
{ | ||
"name": "prop2", | ||
"kind": "let", | ||
"type": "MyTypedefArray", | ||
"value": "[]", | ||
"isFunction": false, | ||
"isFunctionDeclaration": false, | ||
"isRequired": false, | ||
"constant": false, | ||
"reactive": false | ||
} | ||
], | ||
"moduleExports": [], | ||
"slots": [ | ||
{ | ||
"name": "__default__", | ||
"default": true, | ||
"slot_props": "{ prop1: MyTypedef, prop2: MyTypedefArray }" | ||
} | ||
], | ||
"events": [], | ||
"typedefs": [ | ||
{ | ||
"type": "{ [key: string]: boolean; }", | ||
"name": "MyTypedef", | ||
"ts": "interface MyTypedef { [key: string]: boolean; }" | ||
}, | ||
{ | ||
"type": "MyTypedef[]", | ||
"name": "MyTypedefArray", | ||
"ts": "type MyTypedefArray = MyTypedef[]" | ||
} | ||
] | ||
} |