Skip to content

Commit

Permalink
feat: vitest (#92)
Browse files Browse the repository at this point in the history
* fear: vitest

* fix: vitest

* fix: coverage

* fix: coverage

* fix: coverage

* fix: tests

* fix: tests
  • Loading branch information
gairal authored Aug 10, 2024
1 parent 1ab446d commit f121876
Show file tree
Hide file tree
Showing 18 changed files with 55 additions and 47 deletions.
4 changes: 1 addition & 3 deletions .gcloudignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ test/

.env
.env.example
.eslintignore
.eslintrc.js
.gcloudignore
.git
.gitignore
jest.config.js
vitest.config.js
README.md
yarn-error.log
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@
/scripts/import/*.json

.env
.eslintcache
yarn-error.log
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# [frank.gairal.rocks](https://frank.gairal.rocks) RESTful API ![CI status](https://github.com/gairal/frank-bo-node/actions/workflows/ci.yml/badge.svg)

> A REST API built with [TypeScript](https://www.typescriptlang.org/), [Koa](https://koajs.com/),
> [Jest](https://jestjs.io/), [bun](https://bun.sh/), [biome](https://biomejs.dev/) and ❤️
> [Vitest](https://vitest.dev), [bun](https://bun.sh/), [biome](https://biomejs.dev/) and ❤️
## TOC

Expand Down Expand Up @@ -33,8 +33,8 @@ bun start
- `bun dev`: launches a ts-node-dev watcher
- `bun lint`: lints the whole sources and tests with eslint
- `bun start`: runs the project in production mode
- `bun test-cov`: runs Jest tests with coverage
- `bun run test`: runs Jest tests
- `bun test-cov`: runs vitest tests with coverage
- `bun run test`: runs vitest tests

## To-do

Expand Down
2 changes: 1 addition & 1 deletion biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
}
},
"javascript": {
"globals": ["afterEach", "describe", "expect", "it", "test", "jest"]
"globals": ["afterEach", "describe", "expect", "it", "test"]
},
"overrides": [
{
Expand Down
Binary file modified bun.lockb
Binary file not shown.
15 changes: 0 additions & 15 deletions jest.config.js

This file was deleted.

13 changes: 6 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,25 @@
"lint": "biome check",
"prepare": "husky",
"start": "node dist",
"test": "LOG_LEVEL=error jest",
"test-cov": "bun run test --collectCoverage"
"test": "LOG_LEVEL=error vitest",
"test-cov": "bun run test --coverage"
},
"devDependencies": {
"@biomejs/biome": "^1.8.3",
"@tsconfig/node20": "^20.1.4",
"@types/jest": "^29.5.12",
"@types/koa": "^2.15.0",
"@types/koa-logger": "^3.1.5",
"@types/koa__cors": "^5.0.0",
"@types/koa__router": "^12.0.4",
"@types/node": "^22.1.0",
"@types/supertest": "^6.0.2",
"firestore-jest-mock": "^0.25.0",
"@vitest/coverage-v8": "^2.0.5",
"firestore-vitest-mock": "^0.6.0",
"husky": "^9.1.4",
"jest": "^29.7.0",
"lint-staged": "^15.2.8",
"supertest": "^7.0.0",
"ts-jest": "^29.2.3",
"typescript": "^5.5.4"
"typescript": "^5.5.4",
"vitest": "^2.0.5"
},
"dependencies": {
"@google-cloud/firestore": "^7.9.0",
Expand Down
7 changes: 4 additions & 3 deletions src/__tests__/error.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import request from "supertest";

import { test, vi } from "vitest";
import { app } from "../app";
// biome-ignore lint/style/noNamespaceImport: exception
import * as educationModel from "../model/education";
Expand All @@ -14,9 +15,9 @@ describe("error", () => {
});

test("returns a 500 status on error", async () => {
jest
.spyOn(educationModel, "getAll")
.mockRejectedValueOnce(new Error("NETWORK_ERROR"));
vi.spyOn(educationModel, "getAll").mockRejectedValueOnce(
new Error("NETWORK_ERROR"),
);

const { body, status } = await subject("/educations");

Expand Down
8 changes: 4 additions & 4 deletions src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { vi } from "vitest";
import { app } from "../app";
import { PORT } from "../env";
import "../index";

const mockListen = jest.fn();
const mockListen = vi.fn();

app.listen = mockListen;
vi.mock("../app", () => ({ app: { listen: vi.fn() } }));

afterEach(() => {
mockListen.mockClear();
});

test("server works", () => {
// eslint-disable-next-line global-require
require("..");
expect(app.listen).toHaveBeenCalledTimes(1);
expect(app.listen).toHaveBeenCalledWith(PORT, expect.any(Function));
});
1 change: 1 addition & 0 deletions src/routes/__tests__/work.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import request from "supertest";

import { test } from "vitest";
import { app } from "../../app";

const subject = () => request(app.callback()).get("/works");
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/education.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Timestamp } from "@google-cloud/firestore";
import { type DocumentData, Timestamp } from "@google-cloud/firestore";

import type { FirestoreEducation } from "../../src/types/education";

export const educationFixture = (): FirestoreEducation[] => [
export const educationFixture = (): (FirestoreEducation & DocumentData)[] => [
{
dateIn: Timestamp.fromDate(new Date("2005-09-01")),
dateOut: Timestamp.fromDate(new Date("2008-07-31")),
Expand Down
3 changes: 2 additions & 1 deletion test/fixtures/interest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { DocumentData } from "@google-cloud/firestore";
import type { FirestoreInterest } from "../../src/types/interest";

export const interestFixture = (): FirestoreInterest[] => [
export const interestFixture = (): (FirestoreInterest & DocumentData)[] => [
{
description:
"China, Vietnam, Thaïland, Laos, Cambodia, Malaysia, Indonesia, India, Iceland, Ecuador, USA, Brazil, Spain, and more...",
Expand Down
3 changes: 2 additions & 1 deletion test/fixtures/skill.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { DocumentData } from "@google-cloud/firestore";
import type { FirestoreSkillByCategory } from "../../src/types/skill";

export const skillFixture = (): FirestoreSkillByCategory[] => [
export const skillFixture = (): (FirestoreSkillByCategory & DocumentData)[] => [
{
id: "server-administration",
label: "Server administration",
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/travel.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { GeoPoint } from "@google-cloud/firestore";
import { type DocumentData, GeoPoint } from "@google-cloud/firestore";

import type { FirestoreTravel } from "../../src/types/travel";

export const travelFixture = (): FirestoreTravel[] => [
export const travelFixture = (): (FirestoreTravel & DocumentData)[] => [
{
coordinates: new GeoPoint(52.3702, 4.89517),
id: "amsterdam",
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/work.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Timestamp } from "@google-cloud/firestore";
import { type DocumentData, Timestamp } from "@google-cloud/firestore";

import type { FirestoreWork } from "../../src/types/work";

export const workFixture = (): FirestoreWork[] => [
export const workFixture = (): (FirestoreWork & DocumentData)[] => [
{
achievements: [
{
Expand Down
2 changes: 1 addition & 1 deletion test/setupAfterEnv.ts → test/setup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mockGoogleCloudFirestore } from "firestore-jest-mock";
import { mockGoogleCloudFirestore } from "firestore-vitest-mock";

import { educationFixture } from "./fixtures/education";
import { interestFixture } from "./fixtures/interest";
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"extends": "@tsconfig/node20/tsconfig.json",
"compilerOptions": {
"forceConsistentCasingInFileNames": true,
"outDir": "./dist"
"outDir": "./dist",
"types": ["vitest/globals"]
},
"exclude": [
"./__mocks__",
Expand Down
22 changes: 22 additions & 0 deletions vitest.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { defineConfig } from "vitest/config";

// biome-ignore lint/style/noDefaultExport: exception
export default defineConfig({
test: {
coverage: {
reporter: ["text", "html", "json-summary", "json"],
reportOnFailure: true,
thresholds: {
lines: 80,
branches: 80,
functions: 60,
statements: 80,
},
include: ["src/**"],
exclude: ["src/**/*.d.ts"],
},
globals: true,
include: ["src/**/*.test.ts"],
setupFiles: ["./test/setup.ts"],
},
});

0 comments on commit f121876

Please sign in to comment.