I've found react tests recipes quite verbose, because they need to setup a container and cleanup that after each test.
I would like to share that setup-teardown code between test files that require that but could not find a way to do it and I would prefer not to repeat beforeEach and afterEach on every component test.
I've tried with something like:
# sample.test.js
#...
import container from './react_helpers.js'
#...
# react_helpers.js
import { unmountComponentAtNode } from "react-dom"
let container = null;
beforeEach(() => {
// setup a DOM element as a render target
container = document.createElement("div");
document.body.appendChild(container);
});
afterEach(() => {
// cleanup on exiting
unmountComponentAtNode(container);
container.remove();
container = null;
});
export default container
But it seems that beforeEach is not being run.