Does Jest reset a JSDOM document after each set or test?

I am testing a couple of components that go beyond their DOM structure when mounting and unmounting to provide certain interoperability that would otherwise be impossible.

I use Jest and default JSDOM initialization to create a browser-like environment in node. I could not find anything in the documentation to suggest that Jest reset the JSDOM after each test run, and there is no explicit documentation on how to do this manually if it is not.

My question is: is there a Jest reset JSDOM instance after each test, is the package or does it support one JSDOM instance in all test runs? If so, how can I control it?

+6
source share
3 answers

All jest test files run in their own thread, so nothing is used between them, so you will never need to reset mocks or spies after a trial run. On the Jest page:

Jest parallelizes test runs between workers to maximize performance. Console messages are buffered and printed along with test results. Sandboxes of test files and automatic global resetting of states for each test, therefore, no two tests conflict with each other.

+2
source

To correct (misleading) the accepted answer and explicitly emphasize this very important bit of information from one of the previous comments:

. Jest JSDOM ! DOM .

, , , . , , .

, JSDOM :

describe('my test suite', () => {
  afterEach(() => {
    document.getElementsByTagName('html')[0].innerHTML = ''; 
  });

  // your tests here ...
});
+10

Rico Pfaus , , innerHTML, , . , , .

describe('my test suite', () => {
    afterEach(() => {
        document.querySelector(SOME CLASS OR ID).innerHTML = ''
    })
})
0
source

Source: https://habr.com/ru/post/1672331/


All Articles