Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
169
rated 0 times [  172] [ 3]  / answers: 1 / hits: 35828  / 8 Years ago, mon, december 5, 2016, 12:00:00

I'm trying to migrate from AVA to Jest. In AVA you can set ava.setup, in which you set the jsdom environment. For example, creating the DOM structure and doing necessary polyfills (localStorage).



How do I accomplish that in Jest? Currently, I'm using beforeEach in each test suite, which doesn't feel like the best solution.



Thanks in advance!


More From » jestjs

 Answers
2

Great question.



Jest actually ships with jsdom and the environment already configured. You can override it with the testEnvironment setting.



If you need to set up more aspects of the environment though, you can use the setupTestFrameworkScriptFile setting to point to a file that executes before all of your tests run.



For example, if you need window.yourVar to be available on the window for all your tests, you would add this to your package.json:



jest: {
setupTestFrameworkScriptFile: tests/setup.js
}


And in tests/setup.js:



Object.defineProperty(window, 'yourVar', { value: 'yourValue' });

[#59813] Thursday, December 1, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
alfonsok

Total Points: 386
Total Questions: 101
Total Answers: 90

Location: Puerto Rico
Member since Sun, Jun 27, 2021
3 Years ago
;