Questions tagged [proxyquire]

Questions regarding proxyquire - Proxies nodejs's require to make overriding dependencies during testing

Documentation: https://github.com/thlorenz/proxyquire

116 questions
5
votes
2 answers

Mocking a nested module in Node.js?

I have these files: File1.js var mod1 = require('mod1'); mod1.someFunction() ... File2.js var File1 = require('./File1'); Now while writing unit tests for File2, is it possible to have mod1 mocked, so that I don't make calls to…
arjun
  • 103
  • 1
  • 10
4
votes
0 answers

Proxyquire is not working with TypeScript

I have the following test import proxyquire from "proxyquire"; const proxy = proxyquire.noCallThru(); const keytarStub: any = { setPassword: () => { console.log("MOCKED"); }, }; const foo = proxy("./keytar.service.ts", { keytar:…
Georgian Stan
  • 1,865
  • 3
  • 13
  • 27
4
votes
1 answer

Trying to mock response body from a fetch for a unit test

I'm pretty new to sinon and proxyquire and I think I've read all the answers here on SO but I'm still not finding out what I need. Anyway, here's a sanitized version of my code. const fetch = require('node-fetch'); async function deleteID(id,…
Paul Cezanne
  • 8,629
  • 7
  • 59
  • 90
4
votes
1 answer

Unit test of authenticateUser - aws-cognito-identity-js - sinon/proxyquire

I am new to node js and testing in general. I manage to use sinon to stub my functions etc but now I have to test a function that sends callback depending on event (onSuccess, onFailure). Here is the code I need to test. var AWSCognito =…
nguyeti
  • 41
  • 1
  • 5
4
votes
1 answer

testing node express endpoint and stub 3rd party api call

I have an express app like this: server.js const postsController = require('./controllers/posts_controller.js') module.exports = app = express() app.get('posts', postsController.index) posts_controller.js const post =…
dylanjha
  • 2,303
  • 4
  • 28
  • 47
4
votes
0 answers

Using proxyquire to stub require in 3rd party modules dependency

I am trying to use proxyquire to stub a require in a module that is required by a 3rd party module. Example: My module requires a 3rd mod called 'foo'. That module depends on another library called 'bar' and in bar there is a require I want to mock.…
lostintranslation
  • 23,756
  • 50
  • 159
  • 262
4
votes
1 answer

how to mock twilio in unit tests with either sinon/proxyquire or dependency inejction in node.js

Say I want to test a a user login controller that sends login codes via SMS with Twilio. How should I set up the test so that I can mock Twilio and see what codes it's sending back. My approach was to proxyquire the twilio client object and spy on…
MonkeyBonkey
  • 46,433
  • 78
  • 254
  • 460
3
votes
3 answers

Stub method of instance

I have an Express app that uses node-slack-sdk to make posts to Slack when certain endpoints are hit. I am trying to write integration tests for a route that, among many other things, calls a method from that library. I would like to prevent all…
bookcasey
  • 39,223
  • 13
  • 77
  • 94
3
votes
1 answer

How to simulate time delay (timeout) when mocking method calls in unit testing with proxyquire and mocha?

Using proxyquire I'm mocking a method of module B (injected with require() in module A), when testing the method in the module A. The mock (mocking get_campaigns method of admitad.model.js module): const admitadModelMock = { …
Anton Pegov
  • 1,413
  • 2
  • 20
  • 33
3
votes
1 answer

Stubbing Fetch Call - response.json will not invoke

I am trying to stub a fetch call with sinon and sinon-stub-promise. I'm pretty close...but I am not sure why it doesn't invoke the response.json() method I created. function toJSON(response) { console.log(response); return…
dman
  • 10,406
  • 18
  • 102
  • 201
3
votes
0 answers

How to stub out the controllers called by express routes when unit testing routes

I am trying to unit test express routes such as the following: var app = express(); app.get('/',indexController.Index); In my unit tests I am using code such as the following: const indexController = {}; indexController.Index = sinon.stub(); const…
user119020
  • 413
  • 1
  • 3
  • 13
3
votes
0 answers

proxyquire with require.exports = ClassConstructor

If I have a module that exports a single class, how can I mock/stub it with proxyquire? Currently I have the following, which seems to work, but is rather lengthy and doesn't make proper use of proxyquire: some-class.js var SomeClass = function(){ …
dan-man
  • 2,949
  • 2
  • 25
  • 44
3
votes
1 answer

Can I use a custom module resolution function (like "proxyquire") in place of require() with TypeScript?

I have a TypeScript file config.ts that will be run with node: import myDependency = require('my-dependency'); export = { doSomething = () => { ... } } In other TypeScript file, I can import this file with full type…
Nathan Friend
  • 12,155
  • 10
  • 75
  • 125
3
votes
1 answer

Stubbing with proxyquire

How would I stub the following module with proxyquire and sinon: var email = require("emailjs").server.connect.send(); I did the following, but its not working, because when I try to trigger an error within send() it still sends the…
hyprstack
  • 4,043
  • 6
  • 46
  • 89
3
votes
1 answer

nodeJS proxyquire overwrite specific function of a require

I am currently testing a module in isolation using proxquire to overwrite a require of this module. Overwriting a path of a require works fine with proxyquire. For example: var bar = require('./bar'); But can you use proxyquire also to overwrite…
Vegaaaa
  • 474
  • 4
  • 22