Recently I wanted to do some quick coding but spent more time getting a project setup with what I consider the absolute basics to code: type-checking and unit tests.
Current state-of-the-art / flavor-of-the-month tooling is:
- mise for managing tooling and node versions
- Node v22:
mise install node@22 && mise use --global node@22
- pnpm instead of
npm
mise install pnpm && mise use --global pnpm
Setting up a starter project template with:
- Node without-DOM libraries
- TypeScript for sanity
- Jest for unit testing
Structure conventions:
src/
all source filesfile.ts
and test filesfile.test.ts
withindist/
output*.js
files within. Start file isdist/index.js
pnpm init
git init
mkdir dist src # dist=output, src=all files in here
Edit .gitignore
node_modules
dist
coverage
Install basics, TypeScript, and Jest:
pnpm install --save-dev nodemon rimraf \
typescript ts-node @types/node \
jest ts-jest @types/jest
Edit package.json
{
"main": "dist/index.js",
"scripts": {
"build": "rimraf dist/ && tsc",
"start": "node dist/index.js",
"dev": "nodemon --watch 'src/**' --ext 'ts,json' --ignore 'src/**/*.test.ts' --exec 'ts-node --files --compilerHost --transpilerOnly src/index.ts'",
"test": "jest",
}
}
Mostly self explanatory: dev
for development. build
and start
for production. test
for testing.
The nodemon mouthful in dev
is optimizing for speed.
Initialize: pnpm tsc --init
Edit tsconfig.json
to configure it to work with Node and our Structure Conventions
{
"include": ["src/**/*.ts"],
"exclude": ["src/**/*.test.ts"],
"compilerOptions": {
"target": "es2022",
"module": "Node16",
"rootDir": "./src",
"outDir": "./dist",
"lib": ["es2023"],
}
}
These choices are:
- Look for source in
src
but don't output unit test files. "target": "es2022"
, a target well supported by Node v18+"module": "Node16"
, to use the modernimport
and keeping Node compatibility- Sticking with the Structure Conventions of source in
src/
and output indist/
"lib": ["es2023"]
, to exclude the DOM libraries likedocument
- tsconfig documentation
Initialize: pnpm jest --init
Answer the init questionnaire:
- Typescript? Yes
- Test Environment? Node
- Coverage? Yes
- Provider? v8
- Auto clear mocks? Yes
Edit jest.config.ts for TypeScript compatability
"preset": "ts-jest"
HAPPY HACKING!!!
- Prettier for formatting
- ESLint for linting
- husky for pre-commit hooks and thus enforce formatting and linting