Examples of tests that mock Node system APIs: fs, http, child_process, timers
Practical working test examples for blog post Mock system APIs. Because mocking the stable, well documented Node system APIs is easier than stubbing your own internal code. And simpler to update in the future!
- spec individual synchronous
fs
methods with sinon - spec mocking a method for some arguments, but allowing the rest through to the original one
- spec restoring all mocked methods via sinon sandbox
- spec console mocking with mock-stdio and console-pop
- spec async
fs
methods - spec entire file system with mock-fs
- spec network request with nock
- spec mocks
child_process.exec
call using stub-spawn-once - spec mocks
child_process.spawn
call using stub-spawn-once - spec mocks
setInterval
(same forsetTimeout
) with fake timers using sinon - spec mocks Date using MockDate
- spec mocks specific Date prototype method using sinon
All examples use Mocha test framework, but do not rely on anything Mocha-specific.
File an issue!
Author: Gleb Bahmutov <gleb.bahmutov@gmail.com> © 2017
License: MIT - do anything with the code, but don't blame me if it does not work.