Questions tagged [proxyquire]

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

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

116 questions
73
votes
1 answer

Proxyquire, rewire, SandboxedModule, and Sinon: pros & cons

When mocking Node dependencies, I've happened upon the following libraries: Proxyquire Rewire SandboxedModule Sinon They all seem to do more-or-less the same thing: allow you to mock require() calls (with the exception of Sinon which mocks pretty…
robrich
  • 13,017
  • 7
  • 36
  • 63
45
votes
1 answer

How to mock middleware in Express to skip authentication for unit test?

I have the following in Express //index.js var service = require('./subscription.service'); var auth = require('../auth/auth.service'); var router = express.Router(); router.post('/sync', auth.isAuthenticated, service.synchronise); …
jeh
  • 2,373
  • 4
  • 23
  • 38
15
votes
6 answers

How can I mock a fake database for when unit testing against Knex?

I've been using Knex successfully to connect to a backend database. But I want to be able to unit test my code. Is there a way to mock the database connection? I've tried using proxyquire but I can't seem to get it to work. The problem seems to be…
Trevor Allred
  • 888
  • 2
  • 13
  • 22
14
votes
3 answers

Proxyquire with webpack don't compile

I have installed proxyquire and my ajax.test.tsx file contains the following code, just 2 lines import * as proxyquire from 'proxyquire'; proxyquire.noCallThru(); My webpack code is the following module.exports = { entry: { …
hendrixchord
  • 4,662
  • 4
  • 25
  • 39
12
votes
1 answer

How does proxyquire handle second level (indirect) requires of proxies modules?

If we have three modules names A, B and C so module A requires B and B requires C: what would be the effect of this call? var A = proxyquire('A', {'C': mockedModule}) Would module B get the mock or the real C module?
fortran
  • 74,053
  • 25
  • 135
  • 175
10
votes
1 answer

Proxyquire not stubbing my required class

I have a class AProvider that requires './b.provider'. const BProvider = require('./b.provider'); class AProvider { static get defaultPath() { return `defaults/a/${BProvider.getThing()}`; } } module.exports = AProvider; b.provider.js is…
Dave Sag
  • 13,266
  • 14
  • 86
  • 134
10
votes
1 answer

stubbing a function in a proxyquired object

I want to unit-test the following simplified module: const Logger = require('logplease'); const logger = Logger.create('utils'); const tester = { one: () => { logger.log('called real one()'); tester.two(); }, two: () =>…
montrealist
  • 5,593
  • 12
  • 46
  • 68
8
votes
1 answer

Unit test a private method that uses request, pipe and stream using mocks

I want to unit test the exported method in the code below. I want to mock the values in the private method to control the reject/resolves of the returned Promise. client is node-postgres object that is already connected to the database. I know I…
Brian
  • 4,931
  • 3
  • 32
  • 55
7
votes
3 answers

How to assert stubbed fetch more than once

Using proxyquire, sinon, and mocha. I am able to stub fetch on the first call of fetch. But on the second fetch call, which is recursive, I am not able to assert it. From the output, it looks like the assertion may run before the test finishes. You…
dman
  • 10,406
  • 18
  • 102
  • 201
7
votes
1 answer

Unit test: mock document used by an imported TypeScript dependency?

I need to somehow mock the document object to be able to unit-test a legacy TypeScript class. The class imports another class (View.ts), which has an import of a 3-rd party module, and that, in turn, imports something else which assumes document…
montrealist
  • 5,593
  • 12
  • 46
  • 68
7
votes
3 answers

Map paths for Node modules, for unit testing

Client side I stub out paths to modules with SystemJS, like this var systemJsConfig = { baseURL: "./", defaultJSExtensions: true, map: { 'root-components': 'applicationRoot/rootComponents' } }; and so…
Adam Rackis
  • 82,527
  • 56
  • 270
  • 393
6
votes
0 answers

Proxyquire shows error "Cannot find module"

I'm trying to use proxyquire to replace a private function for testing in my Meteor app. Meteor 1.6.1 meteortesting:mocha@1.1.2 In my parentFunction.js: import { some function } from 'anotherFile'; function childFunction() { ... return…
Little Brain
  • 2,647
  • 1
  • 30
  • 54
6
votes
0 answers

`proxyquire` - error: cannot find module

For some reason proxquire cannot find my module index.js. I must be doing something really stupid or obvious and can't see it. Error: invoicer good request 1) "before all" hook 0 passing (9ms) 1 failing 1) invoicer good request…
hyprstack
  • 4,043
  • 6
  • 46
  • 89
5
votes
1 answer

Typescript / Node.js - How to mock transitive dependencies for integration testing?

Let say I have an Express route which is handled by a controller. The controller uses a service and the service uses a repository to talk to a data source. I want to create an integration test using Supertest to test that route : test -> my…
electrotype
  • 8,342
  • 11
  • 59
  • 96
5
votes
1 answer

Proxyquire calling original file instead of stub

Hi I'm trying to mock a Model inside my controller using proxyquire. But for some reason when a try to inject a mock, the original file is being called. This is how my model is being required inside the controller: var Product =…
1
2 3 4 5 6 7 8