react/vitest-hook
Template for a react-based vitest test suite file for testing a react hook.
Usage
npx @lukasbach/scripts react/vitest-hook
You can call the script directly if you have installed it globally:
npm i -g @lukasbach/scripts
ldo react/vitest-hook
There is a default shortcut for this script: ldo vith
You can customize shortcuts with ldo edit-shortcuts
.
Arguments
[0]
: Name of the tested hook
Options
-v
,--verbose
: Verbose logging
You can also omit options, and will be asked for them interactively.
Add --yes
to skip all confirmations.
Script source
/** Template for a react-based vitest test suite file for testing a react hook. */
const testName = await ask.text("_", "Name of the tested hook", "useHook");
const pascal = utils.changeCase.pascalCase(testName);
const kebab = utils.changeCase.kebabCase(testName);
const content = utils.noindent(`
import { describe, expect, it } from "vitest";
import { renderHook } from "@testing-library/react";
import { ${pascal} } from "./${kebab}";
describe("${kebab}", () => {
it("should render", () => {
const { result } = renderHook(() => ${pascal}()));
expect(result.current.value).toBe("hello");
});
});`);
await fs.writeFile(path.join(process.cwd(), `${kebab}.spec.tsx`), content);