56199

how to mock/stub a node.js module

Question:

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?

Answer1:

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?

Recommend

  • Unit testing Javascript anonymous functions
  • Testing an async function with Jasmine in Meteor
  • confuse about the execution order of nested testing suite and specs
  • AngularJS: inject causing error in tests
  • How to use HTTP Authentication with PHP and then run the entered data against a database?
  • Publishing HTML Field Control Converts Relative URL to Absolute URL
  • Number of nodes returned in MutationRecord.addedNodes nodelist (mutationObserver)
  • Recommended way to remove events on destroy with jQuery UI Widget Factory
  • Differences between drawing an Ellipse in Android and Java
  • Best way to produce 'colour correct' raster from a PDF or PS with ghostscript
  • In java, how do you retrieve images from a jar file?
  • Build Matrix of Comparisons in SQl Server
  • Mongodb update() vs. findAndModify() performace
  • GitHub default README markup
  • Vigenere cipher not working
  • What is this strange character in chrome's resource css viewer?
  • How to repeat sections of a SQL query across UNIONs? (DRY in SQL)
  • matlab crashes without dump file when using fopen for file
  • Alamofire and Reachability.swift not working on xCode8-beta5
  • iOS Cordova first plugin - plugin.xml to inject a feature
  • Simple linked list-C
  • Looking for good analogy/examples for monitor verses semaphore
  • CakePHP ACL tutorial initDB function warnings
  • What and where is mdimport
  • Does it make sense to call System.gc() and Thread.sleep() when working on Bitmaps?
  • How to match http request and response using Jersey ContainerRequestFilter and ContainerResponseFilt
  • Record samples being played with OpenAL
  • Asynchronous UI Testing in Xcode With Swift
  • How to make a tree having multiple type of nodes and each node can have multiple child nodes in java
  • Get object from AWS S3 as a stream
  • How would I use PHP exceptions to define a redirect?
  • angularjs unit test when to use $rootScope.$new()
  • ORA-29908: missing primary invocation for ancillary operator
  • How do I rollback to a specific git commit
  • retrieve vertices with no linked edge in arangodb
  • Python: how to group similar lists together in a list of lists?
  • Understanding cpu registers
  • How to get Windows thread pool to call class member function?
  • How can i traverse a binary tree from right to left in java?
  • Converting MP3 duration time