1

I am trying to create stubs using sinon to run the test but its throwing error. Its trying to find a module services and saying the module is not found but I have the services.js in the same folder as test. So I am not sure why its failing. Can someone please advise what is wrong in the code.

 1) the car-lookup controller "before each" hook for "should return filtered TAP response":
     Error: Cannot find module './services' from 'C:\nodejsworkspace\olive\server\api\car-lookup'
      at Function.module.exports [as sync] (node_modules\proxyquire\node_modules\resolve\lib\sync.js:40:15)
      at Proxyquire._resolveModule (node_modules\proxyquire\lib\proxyquire.js:137:20)
      at Proxyquire.<anonymous> (node_modules\proxyquire\lib\proxyquire.js:205:35)
      at Array.reduce (<anonymous>)
      at Proxyquire._withoutCache (node_modules\proxyquire\lib\proxyquire.js:204:6)
      at Proxyquire.load (node_modules\proxyquire\lib\proxyquire.js:129:15)
      at Context.<anonymous> (test\unit\server\api\car-lookup\car-lookup.controller.test.js:30:18)

    controller = proxyquire('../../../../../server/api/car-lookup/car-lookup.controller.js', {
      './services': { taClient: new MockTaClient() }
    });
  });


Below is how I think the services are being exported from the car-lookup.controller.js

Below is car lookup controller.js
If you see in the first line it is trying to import services and services is not a direct js file. I have an index.js file in the ../../directory which is what the first line is referring to. Directory structure is also below. Please advise.

>server
  > api
    > car-lookup
      >car-lookup.controller.js

>server 
 >services 
   >index.js
'use strict';
const services = require('../../services');
const { ApiError, ValidationError } = require('../../errors');
const AccountModel = require('../../models/account-model');

const lookupAccount = (req, res, next) => {

  const retrieveAccount = (oktaToken) => { 
    return services.tapClient.retrieveAccount(req.body);
  };



  const sendResponse = (account) => {
    res.send(new AccountModel(account));
  };

  const onError = (err) => {
    next(err instanceof ValidationError ?
      new ApiError(err, 400) :
      err);
  };

  retrive()
    .then(retrieveAccount)
    .then(sendResponse)
    .catch(onError);
};

module.exports = {
  lookupAccount
};
Jackson2489
  • 11
  • 3
  • 10
  • Your backslash directory navigation can be simplified using https://nodejs.org/docs/latest/api/modules.html#modules_dirname and NodeJS inbuilt Path functions https://nodejs.org/docs/latest/api/path.html – Zardoz Aug 19 '19 at 16:32
  • Can you provide how are you `exporting` and `importing` the services file. – ambianBeing Aug 19 '19 at 17:54
  • Hi , @ambianBeing I have updated my question on how I am importing the module. Please check it. – Jackson2489 Aug 21 '19 at 16:12
  • @Jackson2489 You've got the issue figured out in question itself. You can't require a directory, shouldn't it be `require('../../services/index.js');` – ambianBeing Aug 22 '19 at 04:53
  • @ambianBeing Even if i use that, I am getting the same error. ``` Cannot find module ../../services/index.js ``` – Jackson2489 Aug 22 '19 at 17:43

1 Answers1

1

Make sure you are exporting the file services properly

William
  • 1,107
  • 2
  • 11
  • 18