sinon - GradedJestRisk/js-training GitHub Wiki

Table of Contents

General

Official

  • spies: function that records arguments for all its calls (also: return value, this and exception thrown)
    • can wrap methods that already exists
    • can also be anonymous
  • stubs: functions (spies) with pre-programmed behavior
  • mocks: functions (spies) with pre-programmed
    • behavior (like stubs)
    • expectations
Definition:
  • spies: offer information about function calls, without affecting their behavior
  • stubs: completely replace the function:
    • return a specific value
    • throw an exception
  • mocks: combining both spies and stubs
  • fakes: ?
API

Assertions

2 ways:

  • use native API
  • use sinon-chai plugin

Spies

Stubs

API: Test stubs are functions (spies) with pre-programmed behavior.

Mocks

API Mocks are fake methods (like spies) with pre-programmed behavior (like stubs) as well as pre-programmed expectations. A mock will fail your test if it is not used as expected.

The rule of thumb is: if you wouldn’t add an assertion for some specific call, don’t mock it. Use a stub instead.

Fakes

API

More

Handling promises

Sinon stub promise Sinon as promise

⚠️ **GitHub.com Fallback** ⚠️