0.2.1 • Published 1 year ago
typed-jest v0.2.1
typed-jest
An all-in-one test framework supporting TypeScript out-of-box.
Features
- 100% compatible with Jest. Use
typed-jestjust like you would usejest. - Out-of-the-box TypeScript support.
- All in one. The best practice to use jest in TypeScript (CJS) project.
- No implicit globals.
- Sensible defaults adhering to best practices. Most projects work seamlessly with zero configuration.
- Integrated supertest in it.
Why
This is an all-in-one test framework designed for legacy CommonJS TypesScript project, supporting experimentalDecorators and emitDecoratorMetadata. For modern ESM project, you should consider to use Vitest.
Usage
- Uninstall
jest,@types/jest, andts-jestif they are already installed in your project.
pnpm remove jest @types/jest ts-jest- Install
typed-jest.
pnpm add -D typed-jest- Create an
app.tsfile.
import fs from "node:fs";
import express, { type Express } from "express";
const json = fs.readFileSync("package.json", "utf-8");
const app: Express = express();
app.get("/pkg", function (_req, res) {
console.log("package.json", json);
res.status(200).json(JSON.parse(json));
});
export default app;- Create an
app.spec.tsfile.
import { afterEach, beforeEach, describe, expect, it, jest } from "typed-jest";
import supertest from "typed-jest/supertest";
import app from "./app";
jest.mock("node:fs", () => ({
readFileSync: () => JSON.stringify({ foo: "bar" }),
}));
describe("app", () => {
beforeEach(() => {
console.log("========beforeEach running========");
});
afterEach(() => {
console.log("========afterEach running========");
});
it("should be 2", () => {
expect(1 + 1).toBe(2);
});
it("should success", async () => {
const response = await supertest(app).get("/pkg");
expect(response.status).toEqual(200);
expect(response.body).toStrictEqual({ foo: "bar" });
});
});- Run
pnpm jestto execute tests.
Note: For the projects using
npmas its package manager, please usenpx typed-jestinstead.
Configuration
- Add more CLI configurations after the
pnpm jestcommand. For example, runpnpm jest --coverageto collect test coverage. Runpnpm jest -hfor more CLI options information. - Add a
jest.config.jsfile in the root of your project. Consult the official Jest documentation for more information.
How it works
If you don't include CLI options when running pnpm jest, we will append some sensible defaults:
--transform='{"^.+\\\\.tsx?$":"ts-jest"}': This option transforms TypeScript files, so you can support TypeScript projects without additional configurations or installations.--passWithNoTests: This option prevents the CLI from producing errors if no tests are found. You can override this default bypnpm jest --passWithNoTests=false.--collectCoverageFrom='**/src/**/*.{js,mjs,cjs,jsx,ts,mts,cts,tsx}': This option specifies the location for collecting test coverage. Override it as needed:pnpm jest --collectCoverageFrom='**/lib/**/*.js'
License
MIT
0.2.1
1 year ago
0.2.0
2 years ago
0.2.0-beta.1
2 years ago
0.2.0-beta.0
2 years ago
0.1.3
2 years ago
0.1.3-beta.0
2 years ago
0.1.2
2 years ago
0.1.2-beta.0
2 years ago
0.1.1
2 years ago
0.1.0
2 years ago
0.0.0
3 years ago