---
name: javascript-unit-test-generator
description: "A complete test file for your JavaScript or TypeScript function in the framework you use: one describe block per behaviour, happy path, edge cases, error throwing, nullish inputs, and async handling, with mocks and fixtures that match the real signature. Use when a JavaScript or TypeScript function or module needs a runnable unit test file in Jest, Vitest, Mocha, or Jasmine covering edge cases, errors, and async behaviour. Do not use for end-to-end or browser tests, or test suites in other languages. Related topics: coding, javascript, testing."
---

# JavaScript Unit Test Generator

A complete test file for your JavaScript or TypeScript function in the framework you use: one describe block per behaviour, happy path, edge cases, error throwing, nullish inputs, and async handling, with mocks and fixtures that match the real signature.

## When to use

Use this skill when a JavaScript or TypeScript function or module needs a runnable unit test file in Jest, Vitest, Mocha, or Jasmine covering edge cases, errors, and async behaviour. Do not use it for end-to-end or browser tests, or test suites in other languages.

## Workflow

1. Ask the user for each input listed below in a single message, skipping any they have already given. Where options are listed, offer them, but accept other answers too.
2. Extract the contract from the code: inputs, outputs, side effects, thrown errors, and branches, so every test maps to a behaviour.
3. Write one describe per public function and one it per behaviour, each named as a readable sentence when descriptive naming is on.
4. Cover each selected scenario with specific assertions: boundaries, thrown errors by type or message, null and undefined, awaited promises and rejections.
5. Mock only the named external dependencies with the framework's API, reset between tests, and use fake timers for time-dependent code.
6. Define fixtures once at the top when mock data is on and output a single file that runs under the framework's default configuration.

## Inputs

- **Code to test** (longer free text)
- **Testing framework** (choose one: Jest, Vitest, Mocha/Chai, Jasmine, Node test runner)
- **Language** (choose one: JavaScript, TypeScript)
- **Module system** (choose one: ES modules, CommonJS)
- **External dependencies to mock** (free text, e.g. "fetch or a database client or none")
- **Scenarios to cover** (choose any: Happy path (success), Edge cases, Error throwing, Null/undefined inputs, Async/Promises, Timers and dates)
- **Include mock data** (choose one: Yes, No)
- **Descriptive describe and it blocks** (choose one: Yes, No)

## Output

One fenced code block containing the complete test file with imports, fixtures, and describe blocks, followed by the command to run it.

## Prompt template

Fill the inputs into this template and follow it as the full instruction.

### JavaScript Unit Test Generator

Write the unit tests for my code so every behaviour is covered and the file runs as is.

#### Code
- Code to test: {{Code to test}}
- Testing framework: {{Testing framework}}
- Language: {{Language}}
- Module system: {{Module system}}
- External dependencies to mock: {{External dependencies to mock}}

#### Coverage
- Scenarios to cover: {{Scenarios to cover}}
- Include mock data: {{Include mock data}}
- Descriptive describe and it blocks: {{Descriptive describe and it blocks}}

Read the code for its contract before writing tests: inputs, outputs, side effects, thrown errors, and the branches inside it. Write one describe block per public function and one it block per behaviour, named as a sentence a reader can understand without the code (returns an empty list when the input is empty). Cover the selected scenarios: boundary values and empty inputs for edge cases, a test per thrown error that asserts the message or type, explicit null and undefined calls, and async tests that await the promise and assert rejection where relevant. Mock only the external dependencies named, with the framework's own mocking API, and reset mocks between tests. When mock data is on, define fixtures once at the top of the file. Use fake timers for time-dependent code. Import the code under test from a realistic path and make every assertion specific (toEqual with a full value, not toBeTruthy). Output a single test file that runs with the framework's default configuration.

## Pitfalls

- Asserting toBeTruthy or toBeDefined where a full equality check would catch a wrong value.
- Mocking the internals of the function under test, which makes the test pass regardless of the implementation.
- Forgetting to await an async expectation, so the test passes before the promise rejects.
- Sharing mutable fixtures between tests without resetting, which makes test order change the results.
