Clone this project to jump-start a TS project. It contains tools and configurations for TDD best practices, so that your environment can give you instant feedback on your work.
- typescript: used to transpile your code into JS.
- ts-node: typeScript execution engine and REPL for Node.js.
- jest: unit testing
- eslint: code style enforcement
- prettier: code formatting enforcement
- typedoc: automatic doc generation
- husky: git hooks
yarn start
- run your application and watch for changestest
- run all teststest-watch
- jest interactive terminallint
- check for style errors and warningsclean
- delete./dist
folderbuild
- transpile into./dist
folderbuild-watch
- watch for changes and transpile into./dist
folderbuild-all
- check for style errors, delete./dist
and build
- Download/clone this repo
yarn
yarn prepare
- Go to
package.json
and fill out thename
,description
andauthor
git init
git add .
git commit -m "Scaffolding complete"
- Add a remote repository
This is a walkthrough of the available tools.
- Open a terminal window and run
yarn test-watch
. - All green? Good.
- Open
src/index.test.ts
- At the end, write the following test:
describe('When multiplying two numbers', () => {
it('should return the product', () => {
expect(multiply(3, 5)).toBe(15);
});
});
- You can immediately see that
multiply(3,5)
is hightlighted as an error. - Save the file anyway.
- Now your test fail with:
src/index.test.ts:11:12 - error TS2304: Cannot find name 'multiply'.
- Now open
src/index.ts
and write an implementation:
export const multiply = (a: number, b: number): number => a * b;
- Import your new function into the .test file
- import { sum } from './index';
+ import { sum, multiply } from './index';
- Save both files and watch the test pass.
- Run
yarn lint
in your terminal. If nothing happens, you are good. - Run
echo "crappyCode(a, b) => a /b" >> src/index.ts
- Run
yarn lint
. You'll see at least an error. (also in thetest-watch
terminal) - Fix it like so:
export const divide = (a: any, b: any) => a / b;
- Run
yarn lint
again. You'll see it complains with a warning. Let's fix it:
export const divide = (a: number, b: number) => a / b;
- Run
echo "export const subtract = (a: number, b: number) => { return a - b };
" >> src/index.ts
-
Try to commit this to the repo. If you followed the "Installation" section, it should fail.
-
Replace with:
export const subtract = (a: number, b: number) => a - b;
- Commit (if you want, if not just run
yarn lint
). It should not produce an error.
- Run
yarn start
- Write:
const a = 6;
const b = 3;
console.log(sum(a, b));
- Save and watch the terminal output. It should print
9
- Make a small change:
-console.log(sum(a, b));
+console.log(multiply(a, b));
- Save and watch the terminal output. It should print
18
- Run
yarn docs
- Open
docs/index.html
in a web browser. Explore your new documentation. - In
src/index.ts
add:
/**
* Calculates the square root of a number.
*
* @param x the number to calculate the root of.
* @returns the square root if `x` is non-negative or `NaN` if `x` is negative.
*/
export const sqrt = (x: number): number => Math.sqrt(x);
- Run
yarn docs
and reload your web browser. Explore the new function.
- Just replace
index.ts
andindex.test.ts
and do as you please.