mardi 7 mars 2017

How to stub function in imported module while integration testing with nodejs and babel

What I am trying to do: run a test that hits the rest interface with supertest module and stub some functions that are imported there.

The setup is this:

node.js
babel-plugin-rewire module
sinonjs
mocha

Files:

app.js

import * as emailService from './services/emailService';

router.get('/sendEmail',function(req,res,next) {
  emailService.send('subject');
});

test.js

import supertest from 'supertest';
import app from '../js/app';
import * as emailService from '../js/services/emailService';
import { __RewireAPI__ as emailRewire } from '../js/services/emailService';

it.only('rewiring test', async function() {

 const sendStub = sinon.stub(emailService,'send',function() {
    console.log('stubbed');
  });

  emailRewire.__Rewire__('send',sendStub);

  await supertest(app)
    .get('/sendEmail')
    .expect(200);
});

The above does not print "stubbed" in the console as expected.

Aucun commentaire:

Enregistrer un commentaire