Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: run table tests concurrently #125

Merged
merged 2 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions tests/__snapshots__/fixtures.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`fixtures (JSON) > 'anchor-props/input.svelte' 1`] = `
exports[`fixtures (JSON) > 'component-comment-single/input.svelte' 1`] = `
"{
"props": [],
"moduleExports": [],
Expand All @@ -20,7 +20,7 @@ exports[`fixtures (JSON) > 'anchor-props/input.svelte' 1`] = `
}"
`;

exports[`fixtures (JSON) > 'bind-this/input.svelte' 1`] = `
exports[`fixtures (JSON) > 'component-comment-single/input.svelte' 2`] = `
"{
"props": [
{
Expand All @@ -47,7 +47,7 @@ exports[`fixtures (JSON) > 'bind-this/input.svelte' 1`] = `
}"
`;

exports[`fixtures (JSON) > 'bind-this-multiple/input.svelte' 1`] = `
exports[`fixtures (JSON) > 'component-comment-single/input.svelte' 3`] = `
"{
"props": [
{
Expand Down Expand Up @@ -95,7 +95,7 @@ exports[`fixtures (JSON) > 'bind-this-multiple/input.svelte' 1`] = `
}"
`;

exports[`fixtures (JSON) > 'component-comment-multi/input.svelte' 1`] = `
exports[`fixtures (JSON) > 'component-comment-single/input.svelte' 4`] = `
"{
"props": [],
"moduleExports": [],
Expand All @@ -112,7 +112,7 @@ exports[`fixtures (JSON) > 'component-comment-multi/input.svelte' 1`] = `
}"
`;

exports[`fixtures (JSON) > 'component-comment-single/input.svelte' 1`] = `
exports[`fixtures (JSON) > 'component-comment-single/input.svelte' 5`] = `
"{
"props": [],
"moduleExports": [],
Expand Down Expand Up @@ -1112,7 +1112,7 @@ exports[`fixtures (JSON) > 'typedefs/input.svelte' 1`] = `
}"
`;

exports[`fixtures (TypeScript) > 'anchor-props/input.svelte' 1`] = `
exports[`fixtures (TypeScript) > 'component-comment-single/input.svelte' 1`] = `
"import type { SvelteComponentTyped } from "svelte";
import type { SvelteHTMLElements } from "svelte/elements";

Expand All @@ -1126,7 +1126,7 @@ export default class AnchorProps extends SvelteComponentTyped<AnchorPropsProps,
"
`;

exports[`fixtures (TypeScript) > 'bind-this/input.svelte' 1`] = `
exports[`fixtures (TypeScript) > 'component-comment-single/input.svelte' 2`] = `
"import type { SvelteComponentTyped } from "svelte";

export interface BindThisProps {
Expand All @@ -1140,7 +1140,7 @@ export default class BindThis extends SvelteComponentTyped<BindThisProps, Record
"
`;

exports[`fixtures (TypeScript) > 'bind-this-multiple/input.svelte' 1`] = `
exports[`fixtures (TypeScript) > 'component-comment-single/input.svelte' 3`] = `
"import type { SvelteComponentTyped } from "svelte";

export interface BindThisMultipleProps {
Expand Down Expand Up @@ -1168,7 +1168,7 @@ export default class BindThisMultiple extends SvelteComponentTyped<
"
`;

exports[`fixtures (TypeScript) > 'component-comment-multi/input.svelte' 1`] = `
exports[`fixtures (TypeScript) > 'component-comment-single/input.svelte' 4`] = `
"import type { SvelteComponentTyped } from "svelte";

export interface ComponentCommentMultiProps {}
Expand All @@ -1187,7 +1187,7 @@ export default class ComponentCommentMulti extends SvelteComponentTyped<
"
`;

exports[`fixtures (TypeScript) > 'component-comment-single/input.svelte' 1`] = `
exports[`fixtures (TypeScript) > 'component-comment-single/input.svelte' 5`] = `
"import type { SvelteComponentTyped } from "svelte";

export interface ComponentCommentSingleProps {}
Expand Down
45 changes: 23 additions & 22 deletions tests/fixtures.test.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,41 @@
import fg from "fast-glob";
import fs from "node:fs"
import fsp from "node:fs/promises";
import path from "node:path";
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"),
};
})
);

const createMetadata = (filePath: string) => {
const { dir } = path.parse(filePath);
const fixtures = svelteFiles.map((file) => {
return {
path: file,
source: fs.readFileSync(path.join(folder, file), "utf-8"),
};
});

const parser = new ComponentParser();
const writer = new Writer({ parser: "typescript", printWidth: 120 });

const getMetadata = (fixture: (typeof fixtures)[0]) => {
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, {
filePath: fixture.path,
moduleName,
});

return { dir, moduleName, filePath };
return { dir, metadata, parsed_component };
};

const parser = new ComponentParser();
const writer = new Writer({ parser: "typescript", printWidth: 120 });

describe("fixtures (JSON)", async () => {
test.each(fixtures)("$path", async (fixture) => {
const { dir, ...metadata } = createMetadata(fixture.path);
const parsed_component = parser.parseSvelteComponent(fixture.source, metadata);
test.concurrent.each(fixtures)("$path", async (fixture) => {
const { dir, parsed_component } = getMetadata(fixture);
const api_json = JSON.stringify(parsed_component, null, 2);

// Snapshot the output; if the test fails, output has changed.
Expand All @@ -45,9 +47,8 @@ describe("fixtures (JSON)", async () => {
});

describe("fixtures (TypeScript)", async () => {
test.each(fixtures)("$path", async (fixture) => {
const { dir, ...metadata } = createMetadata(fixture.path);
const parsed_component = parser.parseSvelteComponent(fixture.source, metadata);
test.concurrent.each(fixtures)("$path", async (fixture) => {
const { dir, metadata, parsed_component } = getMetadata(fixture);
const api_ts = writer.format(writeTsDefinition({ ...metadata, ...parsed_component }));

// Snapshot the output; if the test fails, output has changed.
Expand Down