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: upgrade vitest #120

Merged
merged 6 commits into from
Apr 6, 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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
"@types/node": "^20.4.9",
"@types/prettier": "^2.7.3",
"prettier-plugin-svelte": "^2.8.0",
"vite": "^3.2.3",
"vitest": "^0.34.1"
"vite": "^5.2.8",
"vitest": "^1.4.0"
},
"bin": {
"sveld": "cli.js"
Expand Down
16 changes: 15 additions & 1 deletion tests/Writer.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,33 @@
import { describe, test, expect } from "vitest";
import { beforeEach, describe, expect, test, vi } from "vitest";
import Writer from "../src/writer/Writer";

describe("Writer", () => {
beforeEach(() => {
// Suppress Prettier console errors
global.console.error = vi.fn();
});

test("TypeScript", () => {
const consoleError = vi.spyOn(console, "error");
const writer = new Writer({ parser: "typescript", printWidth: 120 });

expect(writer.format("interface I {a:boolean}")).toEqual("interface I {\n a: boolean;\n}\n");
expect(consoleError).toHaveBeenCalledTimes(0);
// Invalid JSON should emit Prettier parsing error
expect(writer.format("a:boolean}")).toEqual("a:boolean}");
expect(consoleError).toHaveBeenCalledTimes(1);
});

test("JSON", () => {
const consoleError = vi.spyOn(console, "error");
const writer = new Writer({ parser: "json", printWidth: 80 });

expect(writer.format("{a:null}")).toEqual('{ "a": null }\n');

expect(consoleError).toHaveBeenCalledTimes(0);
// Invalid JSON should emit Prettier parsing error
expect(writer.format("a:null")).toEqual("a:null");
expect(consoleError).toHaveBeenCalledTimes(1);
});

test("Markdown", () => {
Expand Down
8 changes: 4 additions & 4 deletions tests/__snapshots__/writer-ts-definitions.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

exports[`writerTsDefinition > "default" module name 1`] = `
"
import type { SvelteComponentTyped } from \\"svelte\\";
import type { SvelteComponentTyped } from "svelte";



Expand All @@ -23,7 +23,7 @@ exports[`writerTsDefinition > "default" module name 1`] = `

exports[`writerTsDefinition > writeTsDefinition 1`] = `
"
import type { SvelteComponentTyped } from \\"svelte\\";
import type { SvelteComponentTyped } from "svelte";



Expand All @@ -36,7 +36,7 @@ exports[`writerTsDefinition > writeTsDefinition 1`] = `
propBool?: boolean;

/**
* @default \\"\\"
* @default ""
*/
propString?: string;

Expand All @@ -46,7 +46,7 @@ exports[`writerTsDefinition > writeTsDefinition 1`] = `
name?: string;

/**
* @default \\"\\" + Math.random().toString(36)
* @default "" + Math.random().toString(36)
*/
id?: string;

Expand Down
42 changes: 16 additions & 26 deletions tests/create-exports.test.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
import { describe, test, expect } from "vitest";
import { describe, expect, test } from "vitest";
import { convertSvelteExt, createExports, removeSvelteExt } from "../src/create-exports";

describe("createExports", () => {
test("single default export", () => {
const source = { default: { source: "./Component.svelte", default: true } };

expect(createExports(source, new Map())).toMatchInlineSnapshot(
'"export { default } from \\"./Component.svelte\\";"'
);
expect(createExports(source, new Map())).toEqual('export { default } from "./Component.svelte";');
});

test("single default export (declaration)", () => {
const source = { Component: { source: "./Component.svelte", default: true } };

expect(createExports(source, new Map())).toMatchInlineSnapshot(
'"export { default } from \\"./Component.svelte\\";"'
expect(createExports(source, new Map())).toEqual(
'export { default } from "./Component.svelte";'
);
});

test("single named export", () => {
const source = { Component: { source: "./Component.svelte", default: false } };

expect(createExports(source, new Map())).toMatchInlineSnapshot(
'"export { default as Component } from \\"./Component.svelte\\";"'
expect(createExports(source, new Map())).toEqual(
'export { default as Component } from "./Component.svelte";'
);
});

Expand All @@ -32,10 +30,8 @@ describe("createExports", () => {
Component2: { source: "./Component2.svelte", default: false },
};

expect(createExports(source, new Map())).toMatchInlineSnapshot(`
"export { default as Component } from \\"./Component.svelte\\";
export { default as Component2 } from \\"./Component2.svelte\\";"
`);
expect(createExports(source, new Map())).toEqual(`export { default as Component } from "./Component.svelte";
export { default as Component2 } from "./Component2.svelte";`);
});

test("multiple named exports with a default export", () => {
Expand All @@ -45,11 +41,9 @@ describe("createExports", () => {
default: { source: "./Component2.svelte", default: true },
};

expect(createExports(source, new Map())).toMatchInlineSnapshot(`
"export { default as Component } from \\"./Component.svelte\\";
export { default as Component2 } from \\"./Component2.svelte\\";
export { default } from \\"./Component2.svelte\\";"
`);
expect(createExports(source, new Map())).toEqual(`export { default as Component } from "./Component.svelte";
export { default as Component2 } from "./Component2.svelte";
export { default } from "./Component2.svelte";`);
});

test("multiple named exports with a default export (declaration)", () => {
Expand All @@ -59,20 +53,16 @@ describe("createExports", () => {
Component3: { source: "./Component3.svelte", default: true },
};

expect(createExports(source, new Map())).toMatchInlineSnapshot(`
"export { default as Component } from \\"./Component.svelte\\";
export { default as Component2 } from \\"./Component2.svelte\\";
export { default } from \\"./Component3.svelte\\";"
`);
expect(createExports(source, new Map())).toEqual(`export { default as Component } from "./Component.svelte";
export { default as Component2 } from "./Component2.svelte";
export { default } from "./Component3.svelte";`);
});

test("mixed exports", () => {
const source = { Component: { source: "./Component.svelte", default: true, mixed: true } };

expect(createExports(source, new Map())).toMatchInlineSnapshot(`
"export { default as Component } from \\"./Component.svelte\\";
export { default } from \\"./Component.svelte\\";"
`);
expect(createExports(source, new Map())).toEqual(`export { default as Component } from "./Component.svelte";
export { default } from "./Component.svelte";`);
});

test("removeSvelteExt", () => {
Expand Down
31 changes: 0 additions & 31 deletions tests/index.test.ts

This file was deleted.

5 changes: 3 additions & 2 deletions tests/test-snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const TS_DEF_FILE = "output.d.ts";
const input_files = await fsp.readdir(SNAPSHOTS_FOLDER);
const parser = new ComponentParser({ verbose: true });

for await (const file of input_files) {
const promises = input_files.map(async (file) => {
const input_path = path.join(SNAPSHOTS_FOLDER, file, INPUT_FILE);
const output_path = path.join(SNAPSHOTS_FOLDER, file, OUTPUT_FILE);
const ts_def_path = path.join(SNAPSHOTS_FOLDER, file, TS_DEF_FILE);
Expand All @@ -31,7 +31,8 @@ const TS_DEF_FILE = "output.d.ts";

await fsp.writeFile(output_path, JSON.stringify(parsed_component, null, 2));
await writer.write(ts_def_path, writeTsDefinition(component_api));
}
});

await Promise.all(promises);
parser.cleanup();
})();
Loading