How to Write Unit Tests
This document provides a detailed guide on how to configure unit testing tools (like Mocha) and code coverage tools (like NYC) in a TypeScript project. It also includes instructions for using the required dependency libraries. It is suitable for developers who need to quickly set up a testing environment.
Table of Contents
Unit Testing
Mocha Configuration
Install Dependencies
npm install --save-dev mocha @types/mocha ts-node
Configure mocha.opts
Create a mocha.opts file in the project’s root directory with the following content:
--require ts-node/register
test/**/*.test.ts
Example Code
import "mocha";
describe("Example Test", () => {
it("should pass the test", () => {
const result = 1 + 1;
if (result !== 2) {
throw new Error("Test failed");
}
});
});
Code Coverage
NYC Configuration
Install Dependencies
npm install --save-dev nyc
Configure package.json
Add the following configuration to your package.json:
{
"nyc": {
"include": [
"src/*.ts",
"src/**/*.ts"
],
"exclude": [
"typings",
"dist"
],
"extensions": [
".ts"
],
"require": [
"ts-node/register"
],
"reporter": [
"json",
"html"
],
"all": true
}
}
Run Command
Add the following script to the scripts section of your package.json:
"scripts": {
"cov": "nyc mocha"
}
Run the command:
npm run cov
Calculating Coverage with Lerna
If your project uses Lerna to manage a multi-package structure, you can calculate coverage using the following method:
nyc lerna run cov --concurrency=1
Configure NYC Subprocess Injection
Add the following to your nyc configuration:
{
"include": [
"packages/*/src/*.ts",
"packages/*/src/**/*.ts"
],
"exclude": [
"**/typings",
"**/*.d.ts",
"**/dist",
"**/src/index.ts"
],
"extensions": [
".ts"
],
"reporter": [
"json",
"html"
],
"all": true
}
Note:
nycandts-nodeonly need to be installed at the top level.
Common npm Packages
The following are commonly used npm packages and their functional descriptions:
HTTP Request Related
- whatwg-fetch
- Provides a polyfill for the
fetchAPI, for compatibility in environments that do not natively supportfetch.
- Provides a polyfill for the
- msw
- Mock Service Worker, used to intercept and mock HTTP requests.
- setupServer
- Creates a server-side mock server.
- msw/node
- Provides mock support for the Node.js environment.
Testing Tools
- @testing-library/react
- React component testing library, provides the following methods:
render: Renders a component.fireEvent: Triggers an event.waitFor: Waits for a condition to be met asynchronously.screen: Accesses DOM elements.
- React component testing library, provides the following methods:
- @testing-library/jest-dom/extend-expect
- Extends Jest’s assertion methods, for example,
toBeInTheDocument.
- Extends Jest’s assertion methods, for example,
References
- Mocha Documentation — Official Mocha test framework documentation
- Testing JavaScript — Kent C. Dodds — Comprehensive course on JavaScript testing strategies
- Istanbul / NYC Documentation — Code coverage tool documentation for JavaScript
Be the first to know when I post cool stuff
Subscribe to get my latest posts by email.
Thanks for signing up! Check your email to confirm your subscription.
Whoops, we weren't able to process your signup.