Skip to content

Instantly share code, notes, and snippets.

@phatnguyenuit
Created October 10, 2021 03:01
Show Gist options
  • Save phatnguyenuit/45ad0428d9abff08a53b0729e6886182 to your computer and use it in GitHub Desktop.
Save phatnguyenuit/45ad0428d9abff08a53b0729e6886182 to your computer and use it in GitHub Desktop.
Mock process.env in Jest - full example
import getAssetPath from './getAssetPath';
describe('getAssetPath', () => {
const originalEnv = process.env;
describe.each`
nodeEnv | expectedPath
${'production'} | ${'/production-path'}
${'development'} | ${'/development-path'}
${'test'} | ${'/development-path'}
`('when process.env.NODE_ENV="$nodeEnv"', ({ nodeEnv, expectedPath }) => {
beforeEach(() => {
jest.resetModules();
process.env = {
...originalEnv,
NODE_ENV: nodeEnv,
};
});
afterEach(() => {
process.env = originalEnv;
});
it(`should return "${expectedPath}"`, () => {
expect(getAssetPath()).toEqual(expectedPath);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment