[Jest] Set up Testing Globals in an Application with Jest

For some React component testing, we have common setup in each test file:

import { render } from 'react-testing-library'
import 'jest-dom/extend-expect'
import 'react-testing-library/cleanup-after-each'

We want to setup a common place for JEST to load those scripts:

// jest.config.js

module.exports = {
  setupTestFrameworkScriptFile: '<rootDir>/testSetup.js'
}
// testSetup.js

import 'jest-dom/extend-expect'
import 'react-testing-library/cleanup-after-each'

Then for component test file, we can remove those two lines.

原文地址:https://www.cnblogs.com/Answer1215/p/10710633.html