0

In my code, I am using the library jsonwebtoken. To expedite the testing process, I would like to replace the library with a fake dependency using testdouble.

To test the creation of my fake dependency, I replaced the functionality of the sign method to just return a basic string. However, I noticed it is not working and is still running the original jsonwebtoken method.

What am I doing wrong here?

main.js

const jwt = require('jsonwebtoken');

const signHere = (id) => {
    const token = jwt.sign({ id }, 'fakesignature');
    return token;
}

module.exports = { signHere } 

unit.test.js

const { expect } = require('chai');
const td = require('testdouble');

describe('Replace the jsonwebtoken library', function () { 
   let subject;
   before(function () {
       subject = require('./main.js');
       const fakeJWT = td.replace('jsonwebtoken');
       fakeJWT.sign = td.function();
       td.when(fakeJWT.sign(td.matchers.anything, td.matchers.anything)).thenReturn('test');
   })
   it('should replace the return value', function() {
     const value = subject.signHere(1);
     expect(value).to.equal('test');
   })
})
pythonNovice
  • 1,130
  • 1
  • 14
  • 36

0 Answers0