简体   繁体   中英

how to mock/stub a node.js module

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?

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM