Test process.env with Jest

I have an application that depends on environmental variables like: const APP_PORT = process.env.APP_PORT || 8080; And I would like to test that for example: APP_PORT can be set by a Node.js environment variable. or that an Express.js application is running on the port set with process.env.APP_PORT How can I achieve this with Jest? Can … Read more

How do you test for the non-existence of an element using jest and react-testing-library?

I have a component library that I’m writing unit tests for using Jest and react-testing-library. Based on certain props or events I want to verify that certain elements aren’t being rendered. getByText, getByTestId, etc throw and error in react-testing-library if the element isn’t found causing the test to fail before the expect function fires. How … Read more

Can you write async tests that expect toThrow?

I’m writing an async test that expects the async function to throw like this: it(“expects to have failed”, async () => { let getBadResults = async () => { await failingAsyncTest() } expect(await getBadResults()).toThrow() }) But jest is just failing instead of passing the test: FAIL src/failing-test.spec.js ● expects to have failed Failed: I should … Read more