It doesn't need a particular name, and yes, it's run before any tests are.
You hook it up in your package.json
's jest
stanza.
This is an example from a project I'm working on.
"jest": {
// other stuff...
"setupFiles": [
"./js/jest-setup.js"
],
// ....
}
The actual js/jest-setup.js
file looks like this (i.e. like your example).
import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
Enzyme.configure({adapter: new Adapter()});
Enzyme has a nifty guide on setup here: https://github.com/airbnb/enzyme/blob/master/docs/guides/jest.md
Though the initial step doesn't say it is the package.json
.
Posting the relevant sections (with some minor tweaks) here so we don't lose it:
To run the setup file to configure Enzyme and the Adapter with Jest direct setupTestFrameworkScriptFile
to literally the string <rootDir>
and the path to your setup file.
package.json:
{
"jest": {
"setupTestFrameworkScriptFile": "<rootDir>src/setupTests.js"
}
}
Starting with version 15, Jest no longer mocks modules by default. Because of this, you no longer have to add any special configuration for Jest to use it with enzyme.
Install Jest, and its Babel integrations, as recommended in the Jest docs. Install enzyme. Then, simply require/import React, enzyme functions, and your module at the top of a test file.
setupTests.js:
import React from 'react';
import { shallow, mount, render } from 'enzyme';
import Foo from '../Foo';
Jest & enzyme configuration : Add following code in package.json
"jest": {
"testEnvironment": "jsdom",
"moduleDirectories": [
"src",
"node_modules"
],
"moduleNameMapper": {
"\\.(css|scss)$": "<rootDir>/__mocks__/styleMock.js",
"\\.(jpg|gif|ttf|eot|svg)$": "<rootDir>/__mocks__/fileMock.js"
},
"transform": {
"^.+\\.(js|jsx)$": "babel-jest",
".+\\.(css|styl|less|sass|scss)$": "<rootDir>/node_modules/jest-css-modules-transform"
},
"setupTestFrameworkScriptFile": "<rootDir>/setupTests.js",
"setupFiles": [
"<rootDir>setup.js"
],
"moduleFileExtensions": [
"css",
"scss",
"js",
"json",
"jsx"
],
"testRegex": "\/test\/spec\/.*\\.js$",
"transformIgnorePatterns": [
"/node_modules/(?!test-component).+\\.js$"
]
}
For setup of Enzyme => setup.js
import { configure } from 'enzyme'
import Adapter from 'enzyme-adapter-react-16'
configure({ adapter: new Adapter() })
For setupTestFrameworkScriptFile : setupTests.js global.fetch = require('jest-fetch-mock')
const { JSDOM } = require('jsdom')
const jsdom = new JSDOM('<!doctype html><html><body></body></html>')
const { window } = jsdom
const exposedProperties = ['window', 'navigator', 'document']
const { document } = new JSDOM('').window
global.document = document
global.window = document.defaultView
global.HTMLElement = window.HTMLElement
global.HTMLAnchorElement = window.HTMLAnchorElement
Object.keys(document.defaultView).forEach(property => {
if (typeof global[property] === 'undefined') {
exposedProperties.push(property)
global[property] = document.defaultView[property]
}
})
global.navigator = {
userAgent: 'node.js',
}
I was just having this same issue and the easiest solution was to simply create a file named exactly setupTests.js
, in the src/
directory with the contents:
import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
configure({ adapter: new Adapter() });
Jest automatically detects it and the error goes away.
In PHP , I've used traits before which is a nice way of separating out reusable code & generally making things more readable. Here is a specific Example: ( trait and class can be in separate ...
In PHP , I've used traits before which is a nice way of separating out reusable code & generally making things more readable. Here is a specific Example: ( trait and class can be in separate ...
I'm stuck with this problem for 3 days now... Someone please help me. Challenge 5 Construct a function intersection that compares input arrays and returns a new array with elements found in all ...
I'm stuck with this problem for 3 days now... Someone please help me. Challenge 5 Construct a function intersection that compares input arrays and returns a new array with elements found in all ...
I want to develop a browser extension for google chrome and for firefox, which will have shared code. Is there a way, that both extensions (the firefox version and the chrome version) will use the ...
I want to develop a browser extension for google chrome and for firefox, which will have shared code. Is there a way, that both extensions (the firefox version and the chrome version) will use the ...
I have multiple arrays of promises Each array is put inside a Promise.all() The then() of each Promise.all() adds data to a tempObject I need to set the tempObject to state after then() of all ...
I have multiple arrays of promises Each array is put inside a Promise.all() The then() of each Promise.all() adds data to a tempObject I need to set the tempObject to state after then() of all ...