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

What sort of matchers can I use on my expect calls?

const earth = { round: true }
const globe = { round: true }
const fish = { round: false }
const egg = undefined
const age = 43
const max = 83
const names = ['asad', 'mladen', 'ben', 'roger', 'ardit', 'alexis']

describe('the behaviour of the Jasmine matchers', () => {

  it('should implement toBe correctly', () => {
    expect(earth.round).toBe(true)
    expect(earth).not.toBe(globe)
  })    

  it('should implement toEqual correctly', () => {
    expect(earth).toEqual(globe)
    expect(earth).not.toEqual(fish)  
  })

  it('should implement toBeDefined correctly', () => {
    expect(earth).toBeDefined()
    expect(egg).not.toBeDefined()  
  })

  it('should implement toBeTruthy correctly', () => {
    expect(earth).toBeTruthy()
    expect(egg).not.toBeTruthy()  
  })

  it('should implement toBeFalsey correctly', () => {
    expect(earth).not.toBeFalsy()
    expect(egg).toBeFalsy() 
  })

  it('should implement toBeGreaterThan correctly', () => {
    expect(max).toBeGreaterThan(age)
    expect(age).not.toBeGreaterThan(max)
  })

  it('should implement toBeLessThan correctly', () => {
    expect(age).toBeLessThan(max)
    expect(max).not.toBeLessThan(age)
  })

  it('should implement toContain correctly', () => {
    expect(names).toContain('ben')
    expect(names).not.toContain('robin')
  })

  it('should implement toBeCloseTo correctly', () => {
    expect(3.1415).toBeCloseTo(3.14, 2)
    expect(3.1415).not.toBeCloseTo(3.15, 2)    
  })

  it('should implement jasmine.any() correctly', () => {
    expect([]).toEqual(jasmine.any(Array))
    expect(() => {}).toEqual(jasmine.any(Function))
  })

})
PreviousHow can I create a simple test suite?NextHow can I run code before and after each it block?

Last updated 5 years ago

Was this helpful?