how to mock/stub a node.js module

自作多情 提交于 2019-12-24 23:43:53

问题


I'm working on some code to run on RaspberryPi, and I'm using the Wiring-Pi node module. I have two problems

1) Wiring-Pi won't build on x86 platforms

2) Node-jasmine won't build on RaspberryPi

So, after playing around with a bunch of different ideas, I'm wondering if I'm best off to mock or stub the Wiring-Pi module when on x86 platforms, so I can run the tests.

Of course, my problem is that the file I'm testing includes the require statment

    // getters.js
    var wpi = require('wiring-pi');

    // getters.spec.js
   var get = require('../lib/getters.js');

Is there a way I can set-up the spec file so that the getters.js file doesn't load the wiring-pi module?


回答1:


You can use this to control loading of the wiring-pi module:

var wpi = require('os').arch() === 'arm' ? require('wiring-pi') : {};

Jasmine compiles, installs and runs on the Pi. What error do you get?



来源:https://stackoverflow.com/questions/23355051/how-to-mock-stub-a-node-js-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!