Book Image

TypeScript Blueprints

By : Ivo Gabe de Wolff
Book Image

TypeScript Blueprints

By: Ivo Gabe de Wolff

Overview of this book

TypeScript is the future of JavaScript. Having been designed for the development of large applications, it is now being widely incorporated in cutting-edge projects such as Angular 2. Adopting TypeScript results in more robust software - software that is more scalable and performant. It's scale and performance that lies at the heart of every project that features in this book. The lessons learned throughout this book will arm you with everything you need to build some truly amazing projects. You'll build a complete single page app with Angular 2, create a neat mobile app using NativeScript, and even build a Pac Man game with TypeScript. As if fun wasn't enough, you'll also find out how to migrate your legacy codebase from JavaScript to TypeScript. This book isn't just for developers who want to learn - it's for developers who want to develop. So dive in and get started on these TypeScript projects.
Table of Contents (16 chapters)
TypeScript Blueprints
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface

Writing unit tests


Functional written code is usually easier to test with a unit test. A unit test is an automated test that checks whether a small piece of code is working. We will use the ava test runner, which we can install using NPM:

npm install ava --save-dev
npm install ava -g

At the time of writing, ava does not have type definitions bundled. We will quickly add some types for it in lib/test/ava.d.ts:

declare module "ava" { 
  function test(name: string, run: (t: any) => void): void; 
  namespace test {} 
  export = test; 
} 

We can now write some tests for the factorial function. In lib/test/utils.ts, we write a test case. Any test file will have the following structure:

import * as test from "ava"; 
import { factorial } from "../model/utils"; 
 
test("factorial", t => { 
  t.is(factorial(0), 1); 
  t.is(factorial(1), 1); 
  t.is(factorial(2), 2); 
  t.is(factorial(3), 6); 
  t.is(factorial(4), 24); 
}); ...