Enzyme expects an adapter to be configured

前端 未结 15 1555
太阳男子
太阳男子 2020-12-24 04:53

I created a new React application by create-react-app and I wanted to write a unit test to a component named \"MessageBox\" that I created in the application. This is the un

15条回答
  •  离开以前
    2020-12-24 05:14

    A lot of answers are saying import setupTests.js into your test file. Or configure enzyme adapter in each test file. Which does solve the immediate problem.

    But long term, if you add a jest.config.js file to the project root. You can configure it to run a setup file on launch.

    jest.config.js

    module.exports = {
      setupTestFrameworkScriptFile: "/src/setupTests.ts"
    }
    

    This tells Jest to run setupTest.ts every time it's launched.

    This way if you need to add polyfills or add global mock like localstorage, you can add it to your setupTests file and its configured everywhere.

    The Enzyme docs don't cover integration with Jest, so it is confusing to fuse these two together.

提交回复
热议问题