Spies for the Chai assertion library.

(chai, chai-plugin, browser, mocks-and-spies, testing, spies, stubs, mocks)

Critical or Security advisories for 1.0.0

This version doesn't have any advisory. Want to report one?

Changelog for 1.0.0

This is a major release which introduces many new featues, such as sandboxes, as well as new convenience methods and assertions.

This major release also comes with some breaking changes:

Breaking Changes

  • removed reset method. #35 (@stalniy) You will need to update any callsites to spy.reset() to instead assign a new spy to that variable or property.

  • exposes original function body in spy.toString calls #93 (@stalniy) If you check the spy.toString() output in your tests, this will need to be changed to accomodate for this change.


  • added spy.returns #35 (@stalniy) Allows to create spies that return constant value

  • added spy.on #61 (#38, @stalniy) Allows to add spies on existing methods

  • added posibility to replace original method when spying with spy.on #68 (#62, @stalniy)

chai.spy.on(array, 'push', returns => 5)
chai.spy.on(array, 'push', function() {
  // custom implementation
  • added spy.restore #61 (#38, @stalniy) Allows to remove spies added by spy.on and restore original method implementation

  • added support for sanboxes #61 (#38, @stalniy) Now it's possible to create sanboxes and track/restore spies in sandbox

  • added support for nth call checks #75 (#59, @cnexans) Allows to check spy arguments on specified call

const spy = chai.spy()

spy(1); // first call
spy(2); // second call
spy(3); // third call
spy(i); // nth call


Bug Fixes

  • removed dead links in README #84, #80 (#79, @damianb)
  • fixed formatting in README #67 (@jethrolarson)
  • added MIT license to package.json #58 (@reiz)
  • fixed .with when multiple identical arguments are passed in spy call #56 (#45, @meeber)
  • updated reference to github repository #42 (@robcolburn)


  • better organization for spy methods #92 (@stalniy)
  • added documentation about sanboxes #92 (@stalniy)

Dependencies for 1.0.0

Name Requirement Status




My projects using chai-spies

Name Locked version Requirement
You should be logged in to see which of your projects use chai-spies.