Setup Node.js Project with Express, TypeScript and Mocha
This post is about setting up Node.js with Express, TypeScript and Mocha for unit testing. I've used this setup for my personal blog backend.
Install TypeScript, Express and Mocha
I deceided to use the following tech stack:
First create a new project directory, initialize npm and install all dependencies.
mkdir my_project && cd my_project
npm init
...
npm install --save express
npm install --save-dev mocha ts-mocha supertest typescript ts-mocha @types/node @types/express @types/mocha @types/supertest
After installing all dependencies add a basic project structure.
/build
/node_modules
/src
/test
package.json
Setup TypeScript
Then initialize TypeScript with the following npm command.
node_modules/.bin/tsc --init
This will create a tsconfig.json
in your project root. I needed to change some settings, to make TypeScript work in my environment.
I'm using Node.js 10.x 15.x so I set ES2018
as target
. If you like got lost on all the JS version like es5, es6, ES2018 here you get help.
//tsconfig.json
{
"compilerOptions":{
"lib": ["es2018"],
"target": "es2018",
"module": "commonjs", //commonjs was the only setting that worked with mocha...
"outDir": "./build", //compiled js files
"rootDir": "./src" //here are the .ts files
},
"include": [
"src/**/*"
],
"exclude": [
"node_modules",
"test",
"build"
]
}
Now lets add some scripts to package.json
. The ts-node
command can run TypeScript directly with node without transpiling to js
. This is pretty useful during development but should not be used in production.
//package.json
"scripts": {
"dev": "ts-node src/app.ts",
"build": "tsc",
"start": "node build/app.js"
}
Setup Mocha for Testing
The typescript source files need to be transpiled before they can be used with mocha. There is a small wrapper package called ts-mocha
which does the job.
For Test Driven Development it will be convenient to have a watcher script that runs the tests automaticaly when something has changed.
//package.json
"scripts":{
...
"test": "ts-mocha test/**/*.spec.ts",
"watch": "npm test -- --watch-extensions ts --watch-files src,test --watch"
}
Now npm run watch
runs our tests everytime we change a source or test file.
Create First Spec
To test if everything works I've created a simple class with a static method and the corresponding spec file.
//./src/hello.ts
export class Hello{
static say(str: String): String {
return str;
}
}
//./test/hello.spec.ts
import {Hello} from '../src/hello';
import {strict as assert} from 'assert';
describe('Hello', () => {
it('says hello', () => {
assert.equal(Hello.say('hello'), 'hello');
});
});
Now lets run the test by calling npm test
which is defined in the package.json
. The output looks something like this.
> ts-mocha test/**/*.spec.ts
Hello
✓ says hello
1 passing (4ms)
What I've Learned So Far
Setting up TypeScript and Mocha is pretty straight forward. My next post will be about using this project setup to build a simple JSON backend with express. Soon™