Skip to Content
Learning TypeScript 2.x - Second Edition
book

Learning TypeScript 2.x - Second Edition

by Remo H. Jansen
April 2018
Beginner content levelBeginner
536 pages
13h 21m
English
Packt Publishing
Content preview from Learning TypeScript 2.x - Second Edition

Testing asynchronous code

The MathDemo class also includes an asynchronous version of the same method:

public powAsync(base: number, exponent: number) { 
  return new Promise<number>((resolve) => { 
    setTimeout( 
      () => { 
        const result = this.pow(base, exponent); 
        resolve(result); 
      }, 
      0 
    ); 
  }); 
} 

If we try to test this method, and we don't wait for its result, our test will be useless. However, if we wait for the result, using the Promise.then method, our test will also fail, unless we pass a callback (named done in the example) function to the test-case handler:

it("Should return the correct numeric value for pow", (done) => { const math = new MathDemo(); math.powAsync(2, 3).then((result: number) => { const expected = 8; expect(result).to.be.a("number"); ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Mastering TypeScript - Fourth Edition

Mastering TypeScript - Fourth Edition

Nathan Rozentals
Learning TypeScript

Learning TypeScript

Josh Goldberg
TypeScript for Beginners

TypeScript for Beginners

Bharath Thippireddy

Publisher Resources

ISBN: 9781788391474Supplemental Content