[Unit Testing] Mock a Node module's dependencies using Proxyquire
Sometimes when writing a unit test, you know that the module you're testing imports a module that you would like to observe, or at the very least mock to prevent side effects like network activity or file system operations.
For JavaScript unit tests that run in Node, we can hijack the built-in require function by using the proxyquire module, which allows us to mock one or more modules that are second-class dependencies in our unit test, replacing them with whatever we want.
This means we could replace functions with no-ops to prevent side effects, or replace them with Sinon spies to observe the inner workings of the module you're testing.
In this video, we'll demonstrate the basics of using Proxyquire to prevent and observe a file system operation.
index.js:
const fs = require('fs');
exports.writeFile = (filename, contents) => {
fs.writeFileSync(filename, contents.trim(), 'utf8');
}
For unit testing, we don't want to call 'fs.writeFileSync', instead we want to using mock function.
const assert = require('assert');
const proxyquire = require('proxyquire');
const sinon = require('sinon');
const fsMock = {};
// In index.js file, we want to mock 'fs' module with our fsMock object
const main = proxyquire('./index.js', {'fs', fsMock});
describe('main.writeFile', () => {
it('should write a trimmed string to the specififed file', () => {
fsMock.writeFileSync = sinon.spy();
main.writeFile('file.txt', 'string');
assert.deepEqual(fsMock.writeFileSync.getCall(0).args, ['file.txt', 'string', 'utf8'])
})
})
[Unit Testing] Mock a Node module's dependencies using Proxyquire的更多相关文章
- [Angular + Unit Testing] Mock HTTP Requests made with Angular’s HttpClient in Unit Tests
In a proper unit test we want to isolate external dependencies as much as possible to guarantee a re ...
- [Unit Testing] Mock an HTTP request using Nock while unit testing
When testing functions that make HTTP requests, it's not preferable for those requests to actually r ...
- [Unit Testing] Using Mockito Annotations - @Mock, @InjectMocks, @RunWith
Previously we have seen how to do Unit testing with Mockito; import org.junit.Test; import static or ...
- [Java Basics3] XML, Unit testing
What's the difference between DOM and SAX? DOM creates tree-like representation of the XML document ...
- C/C++ unit testing tools (39 found)---reference
http://www.opensourcetesting.org/unit_c.php API Sanity AutoTest Description: An automatic generator ...
- Unit Testing a zend-mvc application
Unit Testing a zend-mvc application A solid unit test suite is essential for ongoing development in ...
- Unit Testing PowerShell Code with Pester
Summary: Guest blogger, Dave Wyatt, discusses using Pester to analyze small pieces of Windows PowerS ...
- Unit Testing of Spring MVC Controllers: “Normal” Controllers
Original link: http://www.petrikainulainen.net/programming/spring-framework/unit-testing-of-spring-m ...
- Unit Testing of Spring MVC Controllers: Configuration
Original Link: http://www.petrikainulainen.net/programming/spring-framework/unit-testing-of-spring-m ...
随机推荐
- (翻译)FakeKaKao木马分析
这是暑假时看到的一篇病毒分析文章,觉得里面有很多东西值得学习,刚好这几天有空就将它翻译了出来.有不对的地方请大家指正! FakeKaKao木马分析 Virus Bulletin是一个关于流氓软件与垃圾 ...
- Fix error of "you have been logged on with a temporary profile"
You have been logged on with a temporary profile on windows2008 R2 After looking into this issue, I ...
- 去除TB二合一页面弹窗
AdBlock插件 自定义AdBlock ###J_MMREDBOX_MASK 保存
- 解决jquery与zepto等其它库冲突兼容的问题
解决jquery与zepto等其它库冲突兼容的问题;(function ($) { }) (jQuery); ;(function ($) { }) (Zepto); 在Bootstrap ...
- ClientScript.RegisterClientScriptBlock 不执行
ClientScript.RegisterClientScriptBlock 不执行 页面中 form标签必须加入 runat=server
- 引进js文件运行在浏览器上调试报文件找不到
原因是jsp文件的路径和js文件不在同一目录下,用"../"将它们的路径配置在同一路径下或者在路径前面加上"<%= request.getContextPath() ...
- 关于java1.7集合源码阅读
工作中每天都会和java集合打交道,虽然以前也看过jdk源码的实现,但有些东西时间长了还是会遗忘,或者有些实现在新版本中有了新的变化,俗话说"温故而知新",所以打算再阅读一下相关源 ...
- Appium+python自动化14-查看webview上元素(DevTools)【转载】
前言 app上webview的页面实际上是启用的chrome浏览器的内核加载的,如何把手机的网页加载到电脑上,电脑的chrome浏览器上有个开发模式DevTools,是可以方便调试的. 一.环境准备 ...
- HDU 6312.Game-博弈-签到题 (2018 Multi-University Training Contest 2 1004)
2018 Multi-University Training Contest 2 6312.Game 博弈,直接官方题解,懒了. 考虑将游戏变成初始时只有2~n,如果先手必胜的话,那么先手第一步按这样 ...
- (12)C#枚举,结构
枚举 枚举类型是类似自定义的一个类,类里放着你自己定义的常量,关键字enum. enum Season{spring,summer,fall,winter} 想用这里的常量的话,首先把变量定义成 Se ...