Spies are used to: 1) Track function calls, 2) Record arguments, 3) Check call count, 4) Verify call order, 5) Monitor return values. Example: const spy = sinon.spy(object, 'method'); Test wraps existing functions without changing behavior.
WebSocket mocking: 1) Mock socket events, 2) Simulate messages, 3) Test connection states, 4) Handle disconnects, 5) Mock real-time data. Important for real-time applications.
Auth flow mocking: 1) Mock auth providers, 2) Simulate tokens, 3) Test permissions, 4) Mock sessions, 5) Handle auth errors. Important for security testing.
Key differences include: 1) Stubs provide canned answers to calls, 2) Mocks verify behavior and interactions, 3) Stubs don't typically fail tests, 4) Mocks can fail tests if expected behavior doesn't occur, 5) Stubs are simpler and used for state testing while mocks are used for behavior testing.
Common mocking libraries: 1) Sinon.js for comprehensive mocking, 2) Jest mocks when using Jest, 3) testdouble.js for test doubles, 4) Proxyquire for module mocking, 5) Nock for HTTP mocking. Each has specific use cases and features.
Module mocking involves: 1) Using Proxyquire or similar tools, 2) Replacing module dependencies, 3) Mocking specific exports, 4) Maintaining module interface, 5) Handling module side effects. Helps isolate code under test.
Promise mocking: 1) Use stub.resolves() for success, 2) Use stub.rejects() for failure, 3) Chain promise behavior, 4) Mock async operations, 5) Test error handling. Example: stub.resolves('value');
Native module mocking: 1) Mock binary modules, 2) Handle platform specifics, 3) Mock system calls, 4) Test native interfaces, 5) Handle compilation. Important for low-level testing.
Fake timers: 1) Mock Date/setTimeout/setInterval, 2) Control time progression, 3) Test time-dependent code, 4) Simulate delays without waiting, 5) Handle timer cleanup. Example: sinon.useFakeTimers();
API mocking approaches: 1) Use HTTP mocking libraries, 2) Mock API clients, 3) Simulate API responses, 4) Handle API errors, 5) Mock authentication. Isolates from external dependencies.
Partial mocking: 1) Mock specific methods, 2) Keep original behavior, 3) Combine real/mock functionality, 4) Control mock scope, 5) Handle method dependencies. Useful for complex objects.
Stream mocking: 1) Mock stream events, 2) Simulate data flow, 3) Test backpressure, 4) Handle stream errors, 5) Mock transformations. Important for stream processing.
Mock monitoring: 1) Track mock usage, 2) Monitor interactions, 3) Collect metrics, 4) Analyze patterns, 5) Generate reports. Important for test analysis.
Call verification includes: 1) Check call count with calledOnce/Twice, 2) Verify arguments with calledWith, 3) Check call order with calledBefore/After, 4) Verify call context with calledOn, 5) Assert on return values.
Sinon sandboxes: 1) Group mocks/stubs together, 2) Provide automatic cleanup, 3) Isolate test setup, 4) Prevent mock leakage, 5) Simplify test maintenance. Example: const sandbox = sinon.createSandbox(); sandbox.restore();
Mock cleanup approaches: 1) Use afterEach hooks, 2) Implement sandbox restoration, 3) Reset individual mocks, 4) Clean up module mocks, 5) Restore original implementations. Prevents test interference.
Environment mocking: 1) Mock process.env, 2) Stub configuration, 3) Handle different environments, 4) Restore original values, 5) Mock system info. Important for configuration testing.
Microservice mocking: 1) Mock service communication, 2) Simulate service failures, 3) Test service discovery, 4) Mock service registry, 5) Handle distributed state. Important for distributed systems.