Skip to content

Keekuun/nodejs-vitest-demo

Repository files navigation

Welcome to nodejs-vitest-demo 👋

Version Documentation Blog: https://blog.zkkysqs.top https://blog.zkkysqs.top

Node.js complete test environment using TypeScript, Prisma, PostgreSQL and Vitest.

nodejs-vitest-demo

learn from here

Use prisma

pnpm add prisma  -D
npx prisma init
npx prisma generate

Install PostgreSQL with docker

docker pull bitnami/postgresql

docker run --name node-test-env-pg -e POSTGRESQL_USERNAME=docker -e POSTGRESQL_PASSWORD=docker -e POSTGRESQL_DATABASE=node-test-env -p 5432:5432 -d bitnami/postgresql

then update .env file:

DATABASE_URL="postgresql://docker:docker@localhost:5432/node-test-env?schema=public"

then, migrate to the database:

npx prisma migrate dev

Install deps

# use typescript
pnpm i typescript @types/node tsx tsup -D

# generate `tsconfig.json`
npx tsc --init

Use Vitest

pnpm i vitest vite-tsconfig-paths -D

create vitest.config.ts and sample.spec.ts, then just test:

 npx vitest run

Update scripts

{
  ...
  "scripts": {
    "start:dev": "tsx watch src/index.ts", // change the path to match your project initialization file
    "start": "node build/server.js",
    "build": "tsup src --out-dir build",
    "test": "vitest run",
    "test:watch": "vitest", // watching for file changes and re-run
    "test:coverage": "vitest run --coverage" // test coverage report
  }
  ...
}

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published