jestjs - 一个函数使用了一个外部变量,如何用 Jest 测试它?
问题描述
在我们团队的项目中,遇到了一个还没有解决的问题。
这是关于单元测试的。我简化了如下代码。还要在下面添加一个在线演示
// this is temp.js
// when run temp.test.js, b is assigned undefined
// because process.env.TEMP is undefined at that time
const b = `${process.env.TEMP}`;
// console.log(process.env.TEMP); // is undefined
export function temp(a) {
// if b is defined in here, process.env.TEMP works
// const b = `${process.env.TEMP}`;
return b;
}
// export other function, also use b
// this is temp.test.js
import { temp } from "./temp";
process.env = {
TEMP: "temp"
};
describe("temp", () => {
test("test temp", () => {
// console.log("temp test.js: ", process.env.TEMP); // is temp
expect(temp()).toBe("undefined"); // pass
// expect(temp()).toBe("temp"); // not pass, hope it can works
});
});
我想知道如何测试temp.js
。感谢您的帮助!</p>
解决方案
推荐阅读
- html - 更改浏览器大小时网站混乱
- javascript - 使用 i18next 进行反应生产构建会导致翻译仅显示字符串
- reactjs - 将数据从 UseEffect 传递到 Chart.js 不起作用
- ios - 为什么我收到错误“类型‘PublicTableViewController’没有成员‘handleRefresh’’
- python - 计算与新列中平均值的差异
- sql-server - 从 SQL Server 2012 到 2008 R2 的 LoadData 问题将日期日期类型加载到日期时间数据类型列中
- java - Spring Boot 休息控制器错误消息始终为空
- git - 在不更改 HEAD 的情况下,在大型提交中为单个文件发生的命令行更改中显示 git
- python-3.x - SQLite:SQL 变量太多
- android - dex2oat 是否编译所有方法?