-1

Currently in one of my apps i am using nock to mock my api request.Unfortunately in another test file of same project, i used dotenv.If i use dotenv my nock is not mocking the url, it is using the original api request. Any suggestions or help is appreciated. My test file

'use strict';

const assert = require('assert');
const nock = require('nock');

describe('example', () => {
  afterEach(async() => {
    nock.cleanAll();
  });

  describe("checktest", () => {

    it("checksomeupdate", async() => {
      nock('http://example.com')
        .get('/demend-point')
        .reply(200, {
          x: 1
        })

      const result = await demoCallToMainFileMetho();
      const [a, b] = result || []; // response array [1,2,3]
      assert.ok(a.includes('1'));
    });
  });
});

My other file in test dir

require('dotenv').config();
.....some code
OzBob
  • 4,227
  • 1
  • 39
  • 48
  • it's not really clear what you are asking, could you provide minimal code to reproduce the issue? – Krzysztof Krzeszewski Sep 16 '19 at 10:19
  • @KrzysztofKrzeszewski in my demo.test.js require('dotenv'); .....some code in my demo1.test.js if i nock('http://example.com')....some code it does not wrok. – Omar Faruque Sohag Sep 16 '19 at 10:23
  • i'm afraid i do not have access to your `demo.test.js` and i don't know what its contents are. On top of that i'm pretty sure this one file may not be enough to reproduce the issue – Krzysztof Krzeszewski Sep 16 '19 at 10:24
  • @OmarFaruqueSohag, I'm afraid we are still missing some relevant code. What are the configuration you are loading through the `require('dotenv').config()` statement? What is the call executed in `demoCallToMainFileMetho` method? – mgarcia Sep 17 '19 at 06:37
  • require('dotenv').config() is helping to appear process.env variables as those are being used in another files. @mgarcia – Omar Faruque Sohag Sep 17 '19 at 07:43
  • I understand that, but what variables are being defined? Are those variables being used in `demoCallToMainFileMetho` method? Please, provide more context/code so that we can help you better. – mgarcia Sep 17 '19 at 07:57
  • Yes. You are right.Say in my .env i have USER_NAME=,In my demoCallToMainFileMetho this variable is being used. – Omar Faruque Sohag Sep 17 '19 at 08:00

1 Answers1

0

My issue is fixed. Solution: I had to remove dotenv package from my script.Where ever i needed that i had to replace with

process.env = Object.assign(process.env, 
    { DEMO_VARIABLE_ONE: 'false' },
    { DEMO_VARIABLE_ONE_URL: 'value' },
    { DEMO_VARIABLE_TWO: 'value' }
);