如何让Dotenv和Nock协同工作?

How to make Dotenv and Nock work together?

目前在我的一个应用程序中,我正在使用 nock 在同一项目的另一个测试文件中模拟我的 api request.Unfortunately,我使用 dotenv.If 我使用 dotenv 我的 nock 不是模拟 url,它使用原始的 api 请求。 任何建议或帮助表示赞赏。 我的测试文件

'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'));
    });
  });
});

我在测试目录中的其他文件

require('dotenv').config();
.....some code

我的问题已解决。 解决方案:我必须从我的 script.Where 中删除 dotenv 包,因为我需要用

替换它

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