Testing Recipes
  • Introduction
  • Cypress
    • How to navigate to a particular path
    • How to get hold of the currently focused element
    • How to confirm an element has a specific class
    • How to run just one of several tests
    • How to get hold of a specific element
    • How to type into an input field
    • How to confirm text value of input field
    • How to confirm list has expected number of items
    • How to stub a GET network request
    • How to wait for XHR response
    • How to create a single command from multiple commands
    • How to stub a POST network request
    • How to test for XHR failures
    • How to interact with hidden elements
    • How to create aliases for DOM elements
    • How to create a data-driven approach to testing
    • What are the different ways in which Cypress can be run
  • Jest
    • How can I test if a function was called?
    • How can I test if a curried function was called?
  • Architecture
    • How can I make my tests more declarative?
    • How can I make my tests more readable?
    • How can I speed up the writing of tests?
  • Enzyme
    • How can I set and get state in my component?
    • How can I simulate a click on a rendered component?
    • How can I simulate a change to an input field?
    • How can I check if a sub-component was rendered?
    • How can I invoke a method on my component?
    • How can I check to see if an event handler was invoked?
    • How can I set props and update a rendered component?
    • How can I mock the global window object in a test?
    • How can I mock functions used by the component I want to test?
  • Jasmine Recipes
    • How can I create a simple test suite?
    • What sort of matchers can I use on my expect calls?
    • How can I run code before and after each it block?
    • How can I mark tests as not having been implemented yet?
    • How can I spy on a method to check that it was called properly?
    • How can I test asynchronous code?
Powered by GitBook
On this page

Was this helpful?

  1. Jasmine Recipes

How can I test asynchronous code?

describe('how to test function with setTimeout', () => {
  let sample = null
  beforeEach(() => {
    sample = jasmine.createSpy('sampleFunction')
    jasmine.clock().install()
  })
  afterEach(() => {
    jasmine.clock().uninstall()
  })
  it('is only invoked after 1000 milliseconds', () => {
    setTimeout(sample, 1000)
    jasmine.clock.tick(999)
    expect(sample).not.toHaveBeenCalled()
    jasmine.clock.tick(1)
    expect(sample).toHaveBeenCalled()
  })
})
describe('how to test function with setInterval', () => {
  let dummy = null
  beforeEach(() => {
    dummy = jasmine.createSpy('dummy')
    jasmine.clock().install()
  })
  afterEach(() => {
    jasmine.clock().uninstall()      
  })    
  it('checks to see number of times function is called', () => {
    setInterval(dummy, 1000)
    jasmine.clock().tick(999)
    expect(dummy.calls.count()).toBe(0)
    jasmine.clock().tick(1000)
    expect(dummy.calls.count()).toBe(1)
    jasmine.clock().tick(1)
    expect(dummy.calls.count()).toBe(2)
  })
})
// You can pass a `done` callback to any of the following:
// `beforeAll`, `afterAll`, `beforeEach`, `afterEach`, `it`
// The `done` MUST be called when the async work is done or 
// the test will timeout with an error.
const getLink = (name) => `https://api.github.com/users/${name}`
const getJson = response => response.json()
const getInfo = (name) => fetch(getLink(name)).then(getJson)

describe('the getInfo function', () => {
  it('gets the correct information', (done) => {
    getUserInfo('debugme').then((data) => {
      expect(data.name).toBe('debugme')
      done()
    })
  })
})
PreviousHow can I spy on a method to check that it was called properly?

Last updated 5 years ago

Was this helpful?