使用Jest时无效的节点缓存 [英] Invalidate node cache when using Jest

查看:183
本文介绍了使用Jest时无效的节点缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有对象的文件,其中填充了 process.env 属性:

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

env .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;

现在我尝试用不同的 process.env 属性:

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

env.test.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"


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

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

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

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

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

I tried to invalidate Nodes cache like:

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

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

but require.cache is empty {} before each test so it seems that Jest is somehow responsible for importing the file.

我也尝试运行纱线jest --no-cache 但没有帮助。

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

那我是什么想要在每次测试之前加载 env.js ,这样我就可以测试它对不同节点环境变量的行为。

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

jest @ ^ 22.0.4

推荐答案

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

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

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

这篇关于使用Jest时无效的节点缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆