Create globalSetup
in config with following file:
// setup.js
module.exports = async () => {
process.env.TEST_VAR = "my-test-var";
};
You can use this variable in your tests:
// my-test.js
// ...
it("should read the test var from environment", async (done) => {
expect(process.env.TEST_VAR).toEqual("my-test-var");
process.env.TEST_VAR = "new-var";
expect(process.env.TEST_VAR).toEqual("new-var");
done();
});
// ...
You can unset this variable ones the test are complete with globalTeardown
config with the following file:
// teardown.js
module.exports = async () => {
delete process.env.TEST_VAR;
};
package.json
的jest
键中使用globals
或设置一个 ENV 变量,但是对这些变量的任何修改都不会在测试文件之间持久化。如果我错了,请纠正我。 - Joshua Pinter