How to test function which returns function with parameters?

Catfish

I want to test the code below, but i'm unsure how to test this function since it returns a function with parameters. You can see in the image that i'm trying to achieve 100% test coverage and to do that I need a test that gets into the returned function.

const jwt = require('express-jwt')

function validateJwt (tokenConfig) {
  if (!tokenConfig || !tokenConfig.secret) {
    throw new TypeError('tokenConfig param must be defined and have attribute "secret"')
  }

  return (req, res, next) => {
    jwt(_.extend({}, tokenConfig, {
      requestProperty: 'tkn',
      getToken: ReqHelpers.getEitherTkn
    }))
  }
}

Test method which obviously fails with the error AssertionError: expected [Function] to be true

it('should succeed', () => {
  let result = middleware.validateJwt({secret: 'foo'})
  expect(result).to.be.true
})

enter image description here

deerawan

For this kind of test, what we can do is to spy on jwt function call and checking its arguments.

Updated:

since express-jwt return function, we need to involve proxyquire to spy the function. Ref: https://github.com/thlorenz/proxyquire

You can do something like this:

const proxyquire = require('proxyquire');
const sinon = require('sinon');

const jwtSpy = sinon.spy();
const middleware = proxyquire('./middleware', { 'express-jwt': jwtSpy }); // 'express-jwt' comes from your require statement for this package

it('should call jwt', () => {
  const req = sinon.spy();
  const res = sinon.spy();
  const next = sinon.spy();  

  middleware.validateJwt({secret: 'foo'})(req, res, next);

  expect(jwtSpy.called).to.be.ok;
  expect(jwtSpy.calledWithArg({ secret: 'foo', requestProperty: 'tkn'}).to.be.ok; // for checking the arguments
})

Hope it helps

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to test a function which returns a promise $q?

How to test a function which returns anonymous function in jest?

How to test a function which returns void in google test?

How to test a function which returns something and has side effect?

How do I test a function which returns a complex object?

How to unit test a function which calls another that returns a promise?

How to test node.js module which returns anonymous function?

How to write Unit test for a method which returns anonymous function

Junit test function which returns a string

How to test a function which include an async function?

Jasmine How to test a function that returns an anonymous function

How to unit test (using Jasmine) a function in a controller which calls a factory service which returns a promise

Trying to test a function in Jasmine which returns `expected a spy but got a function`

How can I create a function prototype in C++ which accepts two integers as parameters and returns their difference?

How to define a function which returns a map

How to simplify function which returns an object?

How to make a function which returns Type <T>

How to fix async function which returns `undefined`?

How to change a function which returns local variable

How to define a function which returns sum of functions?

How to use xpcall with a function which has parameters?

Function which returns function scheme

JavaScript function that returns function with parameters

Test a function in Python which merges files and returns nothing

Jasmine, AngularJS: Unit test a function which returns a value after timeout

How to test a Go function which runs a command?

How to unit test a function which as viewchild

How to test a function which has a setTimeout with jasmine?

How to test utility function which creates a file

TOP Ranking

HotTag

Archive