A Totally Fresh Project
mkdir TypeScript-TDD-Starter
Use Node v10 (For This Tutorial)
nvm i v10
nvm use v10
Make It An Npm Project
npm init
Make It A TypeScript Project
npm i typescript -g
tsc --init
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"esModuleInterop": true,
"outDir": "dist",
"sourceMap": true
}
}
{
"name": "typescript-tdd-starter",
"version": "0.0.1",
"description": "",
"main": "index.js",
"scripts": {
"start": "./node_modules/ts-mocha/bin/ts-mocha -p ./tsconfig.json ./**/*.test.ts -w --watch-extensions ts",
"test": "nyc mocha '**/*.test.ts'"
},
"author": "Jim",
"license": "MIT",
"nyc": {
"extension": [
".ts",
".tsx"
],
"require": [
"ts-node/register"
],
"reporter": [
"text-summary",
"html"
],
"all": true,
"sourceMap": true,
"instrument": true
}
}
Install Things!
npm i chai -D npm i mocha -D npm i @types/chai -D npm i @types/mocha -D npm i ts-mocha -D npm i typescript -D npm i nyc -D
Make A Basic TypeScript Class With Some Calculation Function
export class ChallengeProblemSolver { solveProblem( input : Array<number> ) { return 4; } }
Make A Test For This Class And Function
import { ChallengeProblemSolver } from './ChallengeProblemSolver'; import { expect } from 'chai'; describe('ChallengeProblemSolver.solveProblem', () => { it('should return 4', () => { const challengeProblemSolver = new ChallengeProblemSolver(); expect(challengeProblemSolver.solveProblem([1, 2, 3])).to.equal(4) }) })
Two Ways To Run It- TDD Mode & Code Coverage Mode
./node_modules/ts-mocha/bin/ts-mocha -p ./tsconfig.json ./**/*.test.ts -w --watch-extensions ts
nyc mocha '**/*.test.ts'
RSS Feed