Skip to content

Commit

Permalink
chore: include test
Browse files Browse the repository at this point in the history
  • Loading branch information
userquin committed Jan 13, 2025
1 parent d0d65e7 commit 41d3e0d
Show file tree
Hide file tree
Showing 9 changed files with 371 additions and 0 deletions.
157 changes: 157 additions & 0 deletions test/__snapshots__/cjs-exports.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`Node10 and Node16 Default Exports Types > Mixed Declaration Types 1`] = `
[
[
"index.d.cts",
[
{
"code": "export type { A, B, C, Options }",
"end": 259,
"exports": " A, B, C, Options",
"names": [
"A",
"B",
"C",
"Options",
],
"specifier": undefined,
"start": 227,
"type": "named",
},
],
[],
],
[
"index.d.ts",
[
{
"code": "export type { A, B, C, Options }",
"end": 259,
"exports": " A, B, C, Options",
"names": [
"A",
"B",
"C",
"Options",
],
"specifier": undefined,
"start": 227,
"type": "named",
},
],
[],
],
]
`;

exports[`Node10 and Node16 Default Exports Types > Re-Export Types 1`] = `
[
[
"index.d.cts",
[],
[
{
"code": "export { A, B, CC as C, CC } from './types.cjs'",
"end": 86,
"exports": " A, B, CC as C, CC",
"names": [
"A",
"B",
"C",
"CC",
],
"specifier": "./types.cjs",
"start": 39,
"type": "named",
},
{
"code": "export { Options }",
"end": 177,
"exports": " Options",
"name": "Options",
"names": [
"Options",
],
"specifier": undefined,
"start": 159,
"type": "named",
},
],
],
[
"index.d.ts",
[],
[
{
"code": "export { A, B, CC as C, CC } from './types.js'",
"end": 84,
"exports": " A, B, CC as C, CC",
"names": [
"A",
"B",
"C",
"CC",
],
"specifier": "./types.js",
"start": 38,
"type": "named",
},
{
"code": "export { Options }",
"end": 175,
"exports": " Options",
"name": "Options",
"names": [
"Options",
],
"specifier": undefined,
"start": 157,
"type": "named",
},
],
],
[
"types.d.cts",
[
{
"code": "export type { A, B, C, C as CC, Options }",
"end": 200,
"exports": " A, B, C, C as CC, Options",
"names": [
"A",
"B",
"C",
"CC",
"Options",
],
"specifier": undefined,
"start": 159,
"type": "named",
},
],
[],
],
[
"types.d.ts",
[
{
"code": "export type { A, B, C, C as CC, Options }",
"end": 200,
"exports": " A, B, C, C as CC, Options",
"names": [
"A",
"B",
"C",
"CC",
"Options",
],
"specifier": undefined,
"start": 159,
"type": "named",
},
],
[],
],
]
`;
89 changes: 89 additions & 0 deletions test/cjs-exports.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { describe, it, expect } from "vitest";
import { build } from "../src";
import { resolve } from "pathe";
import { fileURLToPath } from "node:url";
import { readdir, readFile } from "node:fs/promises";
import { type ESMExport, findExports, findTypeExports } from "mlly";

describe("Node10 and Node16 Default Exports Types", () => {
const dtsFiles = /\.d\.(c)?ts$/;

async function readDtsFiles(
dist: string,
): Promise<[name: string, types: ESMExport[], exports: ESMExport[]][]> {
const files = await readdir(dist).then((files) =>
files.filter((f) => dtsFiles.test(f)).map((f) => [f, resolve(dist, f)]),
);
return await Promise.all(
files.map(async ([name, path]) => {
const content = await readFile(path, "utf8");
return [name, findTypeExports(content), findExports(content)];
}),
);
}

it("Mixed Declaration Types", async () => {
const root = resolve(
fileURLToPath(import.meta.url),
"../cjs-types-fixture/mixed-declarations",
);
await build(root, false);
const files = await readDtsFiles(resolve(root, "dist"));
expect(files).toHaveLength(2);
for (const [name, types, exports] of files) {
expect(exports).toHaveLength(0);
expect(types).toHaveLength(1);
expect(
types.find((e) => e.names.includes("default")),
`${name} should not have a default export`,
).toBeUndefined();
}
expect(files).toMatchSnapshot();
});

it("Re-Export Types", async () => {
const warnings: string[] = [];
const root = resolve(
fileURLToPath(import.meta.url),
"../cjs-types-fixture/reexport-types",
);
await build(root, false, {
hooks: {
"rollup:options": (_, options) => {
const _onwarn = options.onwarn;
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
options.onwarn = (warning, handler) => {
if (warning.code === "EMPTY_BUNDLE") {
warnings.push(warning.message);
return;
}
return _onwarn?.(warning, handler);
};
},
},
});
expect(warnings).toHaveLength(2);
expect(warnings[0]).toBe('Generated an empty chunk: "types".');
expect(warnings[1]).toBe('Generated an empty chunk: "types".');
const files = await readDtsFiles(resolve(root, "dist"));
expect(files).toHaveLength(4);
for (const [name, types, exports] of files) {
if (name.startsWith("types")) {
expect(exports).toHaveLength(0);
expect(types).toHaveLength(1);
expect(
types.find((e) => e.names.includes("default")),
`${name} should not have a default export`,
).toBeUndefined();
} else {
expect(exports).toHaveLength(2);
expect(types).toHaveLength(0);
expect(
exports.find((e) => e.names.includes("default")),
`${name} should not have a default export`,
).toBeUndefined();
}
}
expect(files).toMatchSnapshot();
});
});
12 changes: 12 additions & 0 deletions test/cjs-types-fixture/mixed-declarations/build.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { defineBuildConfig } from "../../../src";

export default defineBuildConfig({
entries: ["./index.ts"],
declaration: true,
clean: true,
// avoid exit code 1 on warnings
failOnWarn: false,
rollup: {
emitCJS: true,
},
});
18 changes: 18 additions & 0 deletions test/cjs-types-fixture/mixed-declarations/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export interface A {
name: string;
}
export interface B {
name: string;
}
export interface C {
name: string;
}
export interface Options {
a?: A;
b?: B;
c?: C;
}

export default function plugin(options: Options = {}): Options {
return options;
}
24 changes: 24 additions & 0 deletions test/cjs-types-fixture/mixed-declarations/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "cjs-types-mixed-declarations-fixture",
"version": "0.0.0",
"private": "true",
"type": "module",
"exports": {
".": {
"import": {
"types": "./dist/index.d.mts",
"default": "./dist/index.d.mjs"
},
"require": {
"types": "./dist/index.d.cts",
"default": "./dist/index.d.cjs"
}
}
},
"main": "./dist/index.cjs",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"files": [
"dist"
]
}
12 changes: 12 additions & 0 deletions test/cjs-types-fixture/reexport-types/build.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { defineBuildConfig } from "../../../src";

export default defineBuildConfig({
entries: ["./index.ts", "./types.ts"],
declaration: true,
clean: true,
// avoid exit code 1 on warnings
failOnWarn: false,
rollup: {
emitCJS: true,
},
});
7 changes: 7 additions & 0 deletions test/cjs-types-fixture/reexport-types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { Options } from "./types";

export type * from "./types";

export default function plugin(options: Options = {}): Options {
return options;
}
37 changes: 37 additions & 0 deletions test/cjs-types-fixture/reexport-types/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "cjs-types-reexport-types-fixture",
"version": "0.0.0",
"private": "true",
"type": "module",
"exports": {
".": {
"import": {
"types": "./dist/index.d.mts",
"default": "./dist/index.d.mjs"
},
"require": {
"types": "./dist/index.d.cts",
"default": "./dist/index.d.cjs"
}
},
"./types": {
"types": {
"import": "./dist/types.d.mts",
"require": "./dist/types.d.cts"
}
}
},
"main": "./dist/index.cjs",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"typesVersions": {
"*": {
"types": [
"./dist/types.d.ts"
]
}
},
"files": [
"dist"
]
}
15 changes: 15 additions & 0 deletions test/cjs-types-fixture/reexport-types/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export interface A {
name: string;
}
export interface B {
name: string;
}
export interface C {
name: string;
}
export type { C as CC };
export interface Options {
a?: A;
b?: B;
c?: C;
}

0 comments on commit 41d3e0d

Please sign in to comment.