JavaScript Configuring Enzyme 3.x Adapter configure enzyme adapter,configure enzyme adapter jest,enzyme.configure( adapter

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()});
Answer:1

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:

Configure with Jest

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"
  }
}

Jest version 15 and up

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';
Answer:2

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',
}
Answer:3

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.

Answer:4

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 ...

  1. javascript compare input value
  2. javascript compare input date
  3. javascript compare input string
  4. javascript compare input text
  5. javascript compare two input values
  6. javascript compare two input fields
  7. javascript compare user input
  8. javascript compare 2 input fields
  9. javascript compare user input string
  10. javascript compare two dates input
  11. compare input javascript
  12. javascript compare 2 input
  13. javascript compare two input

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 ...