Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to stub indirect improts #255

Closed
sujeet-agrahari opened this issue Oct 9, 2020 · 3 comments
Closed

Unable to stub indirect improts #255

sujeet-agrahari opened this issue Oct 9, 2020 · 3 comments

Comments

@sujeet-agrahari
Copy link

I have an app.js which imports routes and routes is exported from particular modules.

Below is the who picture.
app.js imports route load and route loader imports routes from a module, and that module imports the services and controller.

ap.js
- loaders
   - routes.js 
- components
   - auth
      auth.module.js
      auth.service.js
      auth.routes.js
      otp.service.js 

Below is the code that I tried.

/* eslint-disable no-unused-expressions */
const test = require('ava');
const request = require('supertest');
const sinon = require('sinon');
const { expect } = require('chai');
const proxyquire = require('proxyquire');
const app = require('../src/app');

test.before(async (t) => {
  const sendOtpStub = sinon.stub().resolves({});
  proxyquire('../src/components/auth/auth.module', {
    './otp.service': {
      doSendOtp: sendOtpStub,
    },
  });
  t.context.stubs = {
    sendOtpStub,
  };
  t.context.baseUrl = '/api/v1/auth';
  t.context.server = request(app);
});

test.after.always((t) => {
  delete require.cache[require.resolve('../src/app')]; // kills
});

test.serial('Send OTP', async (t) => {
  const { server, baseUrl, stubs } = t.context;
  const response = await server.get(`${baseUrl}/send-otp?phone=8576863491`)
    .expect(200);
  expect(stubs.sendOtpStub.calledOnce).to.be.true;
  expect(response).to.be.a('Object');
});

// test.serial('Login', async (t) => {

// })

Below error I am getting.

Send OTP

  tests\auth.test.js:25

   24:     .expect(200);
   25:   expect(sendOtpStub.calledOnce).to.be.true;
   26:   expect(response).to.be.a('Object');

  Rejected promise returned by test. Reason:

  AssertionError {
    actual: false,
    expected: true,
    message: 'expected false to be true',
    showDiff: true,
  }

  » tests/auth.test.js:25:39

  ─

  1 test failed
npm ERR! Test failed.  See above for more details.

Can someone explain if this is possible what I have trying to do with proxyquire?

What's the issue?

@bendrucker
Copy link
Collaborator

Hi, in order to further investigate your issue I'll need the complete code for all the modules in question. A simpler reproduction would be better.

It sounds like you're asking about replacing transitive dependencies, that's explained here:

https://github.com/thlorenz/proxyquire#globally-override-require

@sujeet-agrahari
Copy link
Author

@bendrucker Please see this https://github.com/sujeet-agrahari/node-express-clean-architecture.
In this case it's same as to stub doLogin or doRegister imported in auth module.

@bendrucker
Copy link
Collaborator

bendrucker commented Oct 9, 2020

I don't see any otp.service require in here, but it sounds like you might be asking about global overrides, in which case my advice above would apply.

@bendrucker bendrucker reopened this Oct 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants