We are using "Jest with React Testing Library" for unit testing in our project. In the same project we have some cases those needs "@jest-environment jsdom" and some cases needs @jest-environment node. How can we switch this in a Test Case file.
We have below jest.config.js file:
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node'
};
I have tried below approach, but it didn't work in my experience.
/**
* @jest-environment jsdom
*/
test('use jsdom in this test file', () => {
const element = document.createElement('div');
expect(element).not.toBeNull();
});
/**
* @jest-environment node
*/
test('do not use jsdom in this test file', () => {
console.log(window); // this will fail as node doesn't have a window object
});