Skip to content

Commit

Permalink
test: use table test to snapshot fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
metonym committed Apr 6, 2024
1 parent 50d1952 commit 221c930
Show file tree
Hide file tree
Showing 92 changed files with 1,847 additions and 0 deletions.
1,730 changes: 1,730 additions & 0 deletions tests/__snapshots__/fixtures.test.ts.snap

Large diffs are not rendered by default.

45 changes: 45 additions & 0 deletions tests/fixtures.test.ts
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.
25 changes: 25 additions & 0 deletions tests/fixtures/output.d.ts
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.
47 changes: 47 additions & 0 deletions tests/fixtures/typedefs/output.json
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[]"
}
]
}

0 comments on commit 221c930

Please sign in to comment.