Skip to content

Commit 3815bf5

Browse files
committed
Add test cases
1 parent 539a7e8 commit 3815bf5

7 files changed

Lines changed: 96 additions & 1 deletion

File tree

lib/esbuild-plugin-react18-css-example/src/client/star-me/star-me.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as React from "react";
2+
import styles from "./star-me.module.css";
23

34
interface StarMeProps extends React.HTMLAttributes<HTMLButtonElement> {
45
gitHubUrl: string;
@@ -15,7 +16,12 @@ export function StarMe({ gitHubUrl, onClick, children, ...props }: StarMeProps)
1516
onClick?.(e);
1617
};
1718
return (
18-
<button data-testid="star-me-h1" onClick={starMe} type="button" {...props}>
19+
<button
20+
data-testid="star-me-h1"
21+
onClick={starMe}
22+
type="button"
23+
{...props}
24+
className={styles.starMe}>
1925
{children || "Star Me"}
2026
</button>
2127
);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test-build*
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import fs from "node:fs";
2+
import path from "node:path";
3+
import { describe, test, beforeAll, afterAll } from "vitest";
4+
import esbuild from "esbuild";
5+
import cssPlugin from "../src";
6+
import glob from "tiny-glob";
7+
8+
describe("Test plugin with esbuild", async () => {
9+
const exampleBuildDir = path.resolve(process.cwd(), "test-build1");
10+
11+
beforeAll(async () => {
12+
await esbuild.build({
13+
format: "cjs",
14+
target: "es2019",
15+
sourcemap: false,
16+
bundle: true,
17+
minify: true,
18+
plugins: [cssPlugin()],
19+
entryPoints: await glob("../esbuild-plugin-react18-css-example/src/**/*.*"),
20+
external: ["react", "react-dom"],
21+
outdir: "./test-build1",
22+
});
23+
});
24+
25+
test(`Test CSS Class Hash`, ({ expect }) => {
26+
const text = fs.readFileSync(path.resolve(exampleBuildDir, "server", "index.js"), "utf-8");
27+
expect(/{fork:["'][^"']*fork[^"']*["']/.test(text)).toBe(true);
28+
});
29+
30+
test("Should contain -moz-", ({ expect }) => {
31+
const test = fs.readFileSync(path.resolve(exampleBuildDir, "server", "index.css"), "utf-8");
32+
expect(/-moz-/.test(test)).toBe(true);
33+
});
34+
});
35+
36+
describe("Test plugin with esbuild and options", async () => {
37+
const exampleBuildDir = path.resolve(process.cwd(), "test-build");
38+
39+
beforeAll(async () => {
40+
await esbuild.build({
41+
format: "cjs",
42+
target: "es2019",
43+
sourcemap: false,
44+
bundle: true,
45+
minify: true,
46+
plugins: [
47+
cssPlugin({
48+
skipAutoPrefixer: true,
49+
generateScopedName: "[name]__[local]___[hash:base64:5]",
50+
}),
51+
],
52+
entryPoints: await glob("../esbuild-plugin-react18-css-example/src/**/*.*"),
53+
publicPath: "https://my.domain/static/",
54+
external: ["react", "react-dom"],
55+
outdir: "./test-build",
56+
});
57+
});
58+
59+
test(`Test CSS Class Hash`, ({ expect }) => {
60+
const text = fs.readFileSync(path.resolve(exampleBuildDir, "server", "index.js"), "utf-8");
61+
expect(/{fork:["'][^"']*fork[^"']*["']/.test(text)).toBe(true);
62+
});
63+
64+
test("Should not contain -moz-", ({ expect }) => {
65+
const test = fs.readFileSync(path.resolve(exampleBuildDir, "index.css"), "utf-8");
66+
expect(/-moz-/.test(test)).toBe(false);
67+
});
68+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import fs from "node:fs";
2+
import path from "node:path";
3+
import { describe, test } from "vitest";
4+
5+
/** testing tsup example - make sure it is build before running this test suit */
6+
describe("Test plugin with tsup", () => {
7+
const exampleBuildDir = path.resolve(
8+
process.cwd(),
9+
"..",
10+
"esbuild-plugin-react18-css-example",
11+
"dist",
12+
);
13+
test(`Test CSS Class Hash`, ({ expect }) => {
14+
const text = fs.readFileSync(path.resolve(exampleBuildDir, "server", "index.js"), "utf-8");
15+
expect(/{fork:["'][^"']*fork-me__fork[^"']*["']/.test(text)).toBe(true);
16+
});
17+
});

lib/esbuild-plugin-react18-css/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"@types/node": "^20.11.22",
4040
"@vitest/coverage-v8": "^1.3.1",
4141
"esbuild": "^0.20.2",
42+
"tiny-glob": "^0.2.9",
4243
"tsup": "^8.0.2",
4344
"typescript": "^5.4.2",
4445
"vitest": "^1.3.1"

lib/esbuild-plugin-react18-css/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ const cssPlugin: (options?: CSSModulePluginOptions) => Plugin = (options = {}) =
127127
name: "esbuild-plugin-react18-css-" + uuid(),
128128
setup(build): void {
129129
const write = build.initialOptions.write;
130+
build.initialOptions.write = false;
130131
if (!options.generateScopedName) {
131132
const globalPrefix = options.globalPrefix ?? "";
132133
options.generateScopedName = (name, filename) =>

lib/esbuild-plugin-react18-css/vitest.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { defineConfig } from "vitest/config";
44
export default defineConfig({
55
test: {
66
globals: true,
7+
exclude: ["node_modules", "test-build", "test-build1"],
78
coverage: {
89
include: ["src/**"],
910
reporter: ["text", "json", "clover", "html"],

0 commit comments

Comments
 (0)