Skip to content

Instantly share code, notes, and snippets.

@VPAbraham
Created September 9, 2019 03:17
Show Gist options
  • Save VPAbraham/022819ee6a94a386dbdc1cb8ab1bd370 to your computer and use it in GitHub Desktop.
Save VPAbraham/022819ee6a94a386dbdc1cb8ab1bd370 to your computer and use it in GitHub Desktop.
Chai Spies

What we have learned about testing so far:

  • input & outputs
  • methods on classes
  • data manipulation
  • test environment is the terminal

What we have not been able to test so far:

  • DOM manipulation
  • interactions that rely on the browser

Spies:

  • "spy" on methods to ensure they were called
    • how many times the method was called
    • arguments it was called with

Spies is a package that chai puts out. Can be installed with NPM by setting it as a dev dependency by doing this in terminal npm install chai-spies --save-dev

const array = [1, 2, 3];

chai.spy.on(array, 'push');

// or multiple spies
chai.spy.on(array, ['push', 'pop']);
chai.spy.on(objectToSpyOn, methods, behaviorOfOurMethods)

Chai Spies Audit:

  1. What do we use spies for?

To simulate a browser environment in the terminal for use in testing as the terminal doesn't recognize certain tech.

  1. What's the difference between spies and mocks? Mocks make a false version of a tech like localStorage for testing purposes while spies uses an 'empty' version of these things with no real functionality. It rather just tests that it is there.

  2. Briefly decrsibe the process for setting up a spy You have to link it in the page with a require and showing that it is an extension of chai. Then in the tests, you must specify the module you are simulating and any methods you may want to run from it.

  3. How many mandatory arguments does spy take? 2, 3 optional

  4. What arguments does chai.spy.on() take? The module, and any methods, and something else I forget.

  • install shai-spies (dev-dep)
  • require chai spies
  • initialize chai to use spies ( chai.use(spies))
  • make object to spy on (prevent ref errors)
  • chai.spy.on(...)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment