javascript 使用 Jest 时使节点缓存无效

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48042200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 07:45:49  来源:igfitidea点击:

Invalidate node cache when using Jest

javascriptnode.jstestingcachingjest

提问by Tomasz Mularczyk

I have a file with object that gets populated with process.envproperties:

我有一个包含对象的文件,该文件填充了process.env属性:

env.js

环境.js

console.log('LOADING env.js');

const {
  PROXY_PREFIX = '/api/',
  USE_PROXY = 'true',
  APP_PORT = '8080',
  API_URL = 'https://api.address.com/',
  NODE_ENV = 'production',
} = process.env;

const ENV = {
  PROXY_PREFIX,
  USE_PROXY,
  APP_PORT,
  API_URL,
  NODE_ENV,
};

module.exports.ENV = ENV;

Now I try to test this file with different process.envproperties:

现在我尝试用不同的process.env属性测试这个文件:

env.test.js

环境测试.js

const envFilePath = '../../config/env';

describe('environmental variables', () => {
  const OLD_ENV = process.env;

  beforeEach(() => {
    process.env = { ...OLD_ENV };
    delete process.env.NODE_ENV;
  });

  afterEach(() => {
    process.env = OLD_ENV;
  });

  test('have default values', () => {
    const { ENV } = require(envFilePath);
    expect(ENV).toMatchSnapshot();
  });

  test('are string values (to avoid casting errors)', () => {
    const { ENV } = require(envFilePath);
    Object.values(ENV).forEach(val => expect(typeof val).toEqual('string'));
  });

  test('will receive process.env variables', () => {
    process.env.NODE_ENV = 'dev';
    process.env.PROXY_PREFIX = '/new-prefix/';
    process.env.API_URL = 'https://new-api.com/';
    process.env.APP_PORT = '7080';
    process.env.USE_PROXY = 'false';

    const { ENV } = require(envFilePath);

    expect(ENV.NODE_ENV).toEqual('dev');
    expect(ENV.PROXY_PREFIX).toEqual('/new-prefix/');
    expect(ENV.API_URL).toEqual('https://new-api.com/');
    expect(ENV.APP_PORT).toEqual('7080');
    expect(ENV.USE_PROXY).toEqual('false');
  });
});

Unfortunately, even though I try to load the file in every test separately the file gets loaded only once, making the third test fail with:

不幸的是,即使我尝试在每个测试中单独加载文件,文件也只加载一次,使第三个测试失败:

Expected value to equal:
  "dev"
Received:
  "production"
Expected value to equal:
  "dev"
Received:
  "production"

P.S. It doesn't fail when I run the test alone.

PS 当我单独运行测试时它不会失败。

I also know that env.jsloads only once because console.log('LOADING env.js');gets fired only once.

我也知道env.js只加载一次,因为console.log('LOADING env.js');只被触发一次。

I tried to invalidate Nodes cache like:

我试图使节点缓存无效,例如:

  beforeEach(() => {
    delete require.cache[require.resolve(envFilePath)];
    process.env = { ...OLD_ENV };
    delete process.env.NODE_ENV;
  });

but require.cacheis empty {}before each test so it seems that Jestis somehow responsible for importing the file.

但在每次测试之前require.cache都是空的{},所以似乎Jest以某种方式负责导入文件。

I also tried to run yarn jest --no-cachebut didn't help.

我也试图逃跑,yarn jest --no-cache但没有帮助。

So what I want is to load env.jsbefore each test so I can test how it behaves with different node environmental variables.

所以我想要的是在每次测试之前加载env.js以便我可以测试它在不同节点环境变量下的行为。

jest@^22.0.4

开玩笑@^22.0.4

回答by Prakash Sharma

You can use jest.resetModules()in beforeEachmethod to reset the already required modules

您可以使用jest.resetModules()inbeforeEach方法重置已经需要的模块

beforeEach(() => {
  jest.resetModules()
  process.env = { ...OLD_ENV };
  delete process.env.NODE_ENV;
});