Questions tagged [proxyquire]

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

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

116 questions
2
votes
1 answer

mocking MongoDB with proxyquire

I'd like to mock MongoDB dependency with proxyquire by doing this in my test: var proxyquire = require('proxyquire'); var controller = path.resolve('.path/to/controller/file.js'); in the before each statement: mocked_mongoose = { isMocked:…
pedrommuller
  • 15,741
  • 10
  • 76
  • 126
2
votes
1 answer

node.js unit testing mock dependency

I have a question on using proxyquire (or any other suggestions on how to test the following code) If i have the following file to test: var path = require('path'); module.exports = function (conf) { var exported = {}; exported.getIssue =…
Rookie
  • 5,179
  • 13
  • 41
  • 65
1
vote
1 answer

How to call or stub with same imported function with multiple ways

In controller I have imported config file for getting path for userId and secret path location and auth file for getting the userId name and secret value based on that I have redirected to some endpoint. but when I write the testcases how to pass…
1
vote
1 answer

How to call imported function using proxyquire and sinon

In my controller I have imported the secure util file when I call that util with path as parameter it returns unique Id. but how to call this function in test file using proxyquire and stub. controller.ts import { getSecret } from…
rrUI0026
  • 21
  • 4
1
vote
1 answer

Not able to properly stub with proxyquire

I can't wrap my head around proxyquire. I have this auditEvent method, part of auditEvent.js: const {verify} = require('@mycompany/verifylib'); const auditEvent = () => { blabla(); verify(); // I want to make this call do nothing …
Mike
  • 609
  • 12
  • 36
1
vote
1 answer

Stub adm-zip with proxyquire and sinon in TypeScript

I want to unit test the class zip.adapter.ts with jest. I tried a lot of different methods to mock/stub the adm-zip package but nothing works. I first tried ts-mock-imports but it always fails if I try to mock adm-zip. Then I tried sinon but it…
Shamshiel
  • 2,051
  • 3
  • 31
  • 50
1
vote
1 answer

How to mock a knex function used in a route

I have this function that configure knex by environment const knexConnection = () => { const config = require('./connection')[environment]; return knex(config) } I use this function in my route.js module.exports = (app) => { …
Ndx
  • 517
  • 2
  • 12
  • 29
1
vote
1 answer

Node JS Error: Cannot find module './services'

I am trying to create stubs using sinon to run the test but its throwing error. Its trying to find a module services and saying the module is not found but I have the services.js in the same folder as test. So I am not sure why its failing. Can…
Jackson2489
  • 11
  • 3
  • 10
1
vote
1 answer

Stubbing request across entire project

I'm writing tests for a small REST library that implements OAuth's refresh grant on top of the request library. As part of it's functionality, it's providing a retry function that has something like this in rest.js: const auth =…
mabi
  • 5,279
  • 2
  • 43
  • 78
1
vote
0 answers

mocking function with same name and different argument using proxyquire

I have a file with format below which uses certain module. let module = require('modulename') let r = module() r.post(variable, callbackFunction) r.post(variable, variable, variable, callbackFunction) I am trying to mock this two function using…
1
vote
0 answers

Stub nodejs proxyquire return error :TypeError: Cannot read property 'url' of undefined

My unit test case is failing, I am getting an error message 'TypeError: Cannot read property 'url' of undefined'. It seems issue with config file which have url property. Please someone help me on same. // Employee.js const config =…
Dharam
  • 469
  • 2
  • 9
  • 23
1
vote
0 answers

Unit testing the instance of Sequelize Model

I have the following code: async save(id: string) { const person = await PersonModel.findOne({ where: { id: id }, }); if (!person) { await PersonModel.create({ id: '2345', name: 'John Doe', age: 25 }); return; } await…
1
vote
1 answer

proxyquire TypeError: Datastore is not a constructor

I am trying to test the following piece of code. const Datastore = require('@google-cloud/datastore'); // Creates a client const datastore = new Datastore({ projectId: serviceConfig.projectId }); My test file contains function MockDatastore…
Neil
  • 5,919
  • 15
  • 58
  • 85
1
vote
1 answer

How do I stub a module with Jest and Proxyquire?

I want to force node-fetch to be a stub and I'm having a bit of trouble getting it to work. Code: const fetchMock = jest.fn(); const { getArticle } = require('../../sfpublish/api'); console.log('loaded api') const A =…
jcollum
  • 43,623
  • 55
  • 191
  • 321
1
vote
1 answer

Stubbing AWS SES with Sinon and Proxyquire

I'm trying to unit test some code that involves a call to AWS's SES service. Here's the code in question: const AWS = require('aws-sdk'); const send = function(options) { const SES = new AWS.SES(); return new Promise((resolve, reject) => { …
user1381745
  • 3,850
  • 2
  • 21
  • 35