使用.env文件通过Jest进行单元测试 [英] Using .env files for unit testing with jest

查看:50
本文介绍了使用.env文件通过Jest进行单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在Jest中从环境文件中加载环境变量以进行单元测试?我希望像这样对它进行一系列测试:

Is it possible to load environment variables from an env file for unit testing purposes in Jest? I'm looking to run a series of tests on it like so:

// unit tests for env file
describe('env', () => {
    it('should have a client id', () => {
        expect(process.env.CLIENT_ID).toBeDefined();
    });
    it('should have a client secret', () => {
        expect(process.env.CLIENT_SECRET).toBeDefined();
    });
    it('should have a host', () => {
        expect(process.env.HOST).toBeDefined();
    });
    it('should have a scope', () => {
        expect(process.env.SCOPE).toBeDefined();
    });
    it('should have a response type', () => {
        expect(process.env.RESPONSE_TYPE).toBeDefined();
    });
    it('should have a redirect uri', () => {
        expect(process.env.REDIRECT_URI).toBeDefined();
    });
});

当前,以上所有测试均将失败,说明变量未定义.最初,我使用的是mocha/chai设置,该设置允许我通过使用dotenv来加载我所有的env变量.这涉及通过webpack运行所有单元测试,并且运行良好.

Currently, all the above tests will fail, stating that the variables are undefined. Initially I was using a mocha/chai setup, which allowed me to just load all of my env variables via the use of dotenv. This involved running all unit tests through webpack and worked fine.

但是,通过阅读文档 Jest不会运行测试通过webpack;相反,模块是通过moduleNameMapper模拟出来的.这对于其他所有功能都可以正常工作,但是我无法加载env文件变量.到目前为止,我已经尝试将setupFiles选项用于js文件,该文件调用dotenv.config,并为其指定env文件的路径,如下所示:

However, from reading the documentation Jest doesn't run tests through webpack; instead modules are mocked out via moduleNameMapper. This works fine for everything else, but I can't get the env file variables to load. So far I've tried using the setupFiles option to a js file that calls dotenv.config with the path of the env file given to it like so:

// setup file for jest
const dotenv = require('dotenv');
dotenv.config({ path: './env' });

这没有用,因此我现在仅使用.env.js文件进行单元测试,并将该文件传递到setupFiles选项中.但是,为了保持可维护性,并使其与webpack一起用于生产,我只想将它们全部保存在一个文件中.这是.env.js文件如何查找参考的摘录

This didn't work, so I've now resorted to using just a .env.js file for unit tests and passing this file into the setupFiles option instead. However, for maintainability, and to keep it working with webpack for production, I'd like to just keep it all in one file. Here's an extract of how the .env.js file looks for reference

// .env.js extract example
process.env.PORT = 3000;
process.env.HOST = 'localhost';
process.env.CLIENT_ID = 'your client id';
process.env.REDIRECT_URI = 'your callback endpoint';

推荐答案

使用 ./的顶级配置路径从注入点开始是相对的,您的测试套件可能位于名为的文件夹中 test ,导致在运行单元测试时找不到该代码. dotenv.config(); 使用类似于绝对路径的全局注入策略.

Your top config path using ./ is relative from the point of injection, it's likely your test suite might be inside a folder named test which causes it to not be found when running your unit tests. dotenv.config(); Uses global injection strategy which is similar to absolute pathing.

这篇关于使用.env文件通过Jest进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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