Reputation: 2999
I have this variable defined on my main.ts
const mockMode = process.env.MOCK_MODE;
I just create a test and set this variable to true, but on the main does not get 'true'
, but 'false'
describe('onBook', () => {
// Arrange
const mockMode = "true";
...
Upvotes: 5
Views: 19857
Reputation: 102587
You can set the value of process.env.MOCK_MODE
directly within the unit test case and restore it to original value at the end.
E.g.
main.ts
:
export function main() {
const mockMode = process.env.MOCK_MODE;
return mockMode;
}
main.test.ts
:
import { main } from './main';
describe('main', () => {
it('should pass', () => {
const original = process.env.MOCK_MODE;
process.env.MOCK_MODE = 'true';
const actual = main();
expect(actual).toBe('true');
process.env.MOCK_MODE = original;
});
it('should restore MOCK_MODE', () => {
expect(process.env.MOCK_MODE).toBe('undefined');
});
});
Unit test result:
PASS src/stackoverflow/59319610/main.test.ts (14.207s)
main
✓ should pass (7ms)
✓ should restore MOCK_MODE (1ms)
Test Suites: 1 passed, 1 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 16.293s
Upvotes: 6