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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [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 ...

  4. [Java Basics3] XML, Unit testing

    What's the difference between DOM and SAX? DOM creates tree-like representation of the XML document ...

  5. C/C++ unit testing tools (39 found)---reference

    http://www.opensourcetesting.org/unit_c.php API Sanity AutoTest Description: An automatic generator ...

  6. Unit Testing a zend-mvc application

    Unit Testing a zend-mvc application A solid unit test suite is essential for ongoing development in ...

  7. Unit Testing PowerShell Code with Pester

    Summary: Guest blogger, Dave Wyatt, discusses using Pester to analyze small pieces of Windows PowerS ...

  8. Unit Testing of Spring MVC Controllers: “Normal” Controllers

    Original link: http://www.petrikainulainen.net/programming/spring-framework/unit-testing-of-spring-m ...

  9. Unit Testing of Spring MVC Controllers: Configuration

    Original Link: http://www.petrikainulainen.net/programming/spring-framework/unit-testing-of-spring-m ...

随机推荐

  1. 以jhtml结尾的文件

    用一个实例来说明,直接上代码. LogonAction.java(一个servlet) package com.lz.web.action; import java.io.IOException; i ...

  2. Struts2.0中ActionInvocation使用

    Interceptor的接口定义没有什么特别的地方,除了init和destory方法以外,intercept方法是实现整个拦截器机制的核心方法.而它所依赖的参数ActionInvocation则是我们 ...

  3. codeforce 570 problem E&& 51Nod-1503-猪和回文

    1503 猪和回文 一只猪走进了一个森林.很凑巧的是,这个森林的形状是长方形的,有n行,m列组成.我们把这个长方形的行从上到下标记为1到n,列从左到右标记为1到m.处于第r行第c列的格子用(r,c)表 ...

  4. Autotools Mythbuster

    Preface Diego Elio "Flameeyes" Pettenò Author and Publisher <flameeyes@flameeyes.eu> ...

  5. UVA 10746 Crime Wave - The Sequel

    最小费用最大流 源点->警察->bank->汇点 剩下的模板就可以 #include <map> #include <set> #include <li ...

  6. java应用挂死故障排查

    现象: java开发的web应用无法访问 排查: 1.从resin/log/watchdog-manager.log的日志里可以看出来,jvm的内存满,无法创建新进程 java.lang.OutOfM ...

  7. java1.7集合源码阅读:ArrayBlockingQueue

    ArrayBlockingQueue是一个先进先出线程安全的队列,队列头部是进入队列时间最长的元素,队尾是进入队列时间最短的元素,同时队列的最大容量是固定的. 先看类定义: public class ...

  8. ZOJ 3820:Building Fire Stations(树的直径 Grade C)

    题意: n个点的树,边长全为1,求找出两个点,使得树上离这两个点距离最远的那个点,到这两个点(中某个点就行)的距离最小. 思路: 求树直径,找中点,删除中间那条边(如果直径上点数为奇数,则删任何一侧都 ...

  9. [BZOJ2038] [2009国家集训队]小Z的袜子(hose) 莫队算法练习

    2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 10299  Solved: 4685[Sub ...

  10. LINUX学习资源

    鸟哥:http://vbird.dic.ksu.edu.tw/linux_basic/0430cron.php#cron 五分钟教程:http://roclinux.cn/?page_id=3759