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

feat: vitest integration #42

Merged
merged 2 commits into from
May 14, 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ A custom-built CLI for creating and managing SolidJS apps and projects.
- [ ] PostCSS
- [x] UnoCSS
- [ ] Vanilla Extract
- [ ] Vitest
- [x] Vitest
- [ ] Tauri
- [ ] Playwright
- [ ] Utilities
Expand Down
59 changes: 59 additions & 0 deletions packages/commands/src/lib/integrations.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { insertAfter, insertAtBeginning } from "@solid-cli/utils/fs";
import { readFile, writeFile } from "fs/promises";
import { fileExists, validateFilePath } from "./utils/helpers";
import { $ } from "execa";
import { getRunnerCommand, detectPackageManager } from "@solid-cli/utils/package-manager";
Expand Down Expand Up @@ -108,4 +109,62 @@ export const integrations = {
await insertAtBeginning(path, `import "solid-devtools";\n`);
},
},
"vitest": {
installs: [
"vitest",
"jsdom",
"@solidjs/testing-library",
"@testing-library/user-event",
"@testing-library/jest-dom",
],
additionalConfig: async () => {
try {
p.log.info("Adding test script to package.json");
const packageJsonString = await readFile("package.json", "utf8");
const packageJson = JSON.parse(packageJsonString);
if (!/\bvitest\b/.test(packageJson.scripts.test || "")) {
packageJson.scripts.test = "vitest";
await writeFile("package.json", JSON.stringify(packageJson, null, 2) + "\n", "utf8");
}
const hasTs = fileExists("tsconfig.json");
if (hasTs) {
p.log.info("Adding testing types to tsconfig.json");
const tsConfigString = await readFile("tsconfig.json", "utf8");
const tsConfig = JSON.parse(tsConfigString);
if (!tsConfig.compilerOptions) {
tsConfig.compilerOptions = {};
}
tsConfig.compilerOptions.types = [
...new Set([...(tsConfig.compilerOptions.types || []), "vite/client", "@testing-library/jest-dom"]),
];
await writeFile("tsconfig.json", JSON.stringify(tsConfig, null, 2) + "\n", "utf8");
}
if (
packageJson.dependencies["@solidjs/start"] &&
["ts", "mjs", "cjs", "js"].every(
(suffix) => !fileExists(`vite.config.${suffix}`) && !fileExists(`vitest.config.${suffix}`),
)
) {
const suffix = hasTs ? "ts" : "mjs";
p.log.info(`Adding vitest.config.${suffix}`);
await writeFile(
`vitest.config.${suffix}`,
`import solid from "vite-plugin-solid";
import { defineConfig } from "vitest/config";

export default defineConfig({
plugins: [solid()],
resolve: {
conditions: ["development", "browser"],
},
});
`,
"utf-8",
);
}
} catch (err) {
console.error(err);
}
},
},
};