react-native/jest.config.js
Rob Hogan 86968c1104 Use modern timers in monorepo Jest tests (#38955)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/38955

Jest introduced "modern" timers based on `sinon/fake-timers` in Jest 26 ([release announcement](https://jestjs.io/blog/2020/05/05/jest-26#new-fake-timers)), and they became the default in Jest 27, in May 2021.

Modern timers have more capabilities - they were introduced with support for `queueMicrotask`, mocking `Date`, etc., and they've continued to receive more attention from the Jest team since - they're now much more comprehensive and more configurable than legacy timers.

Importantly, because they're not based on Jest mocks, they're not affected in surprising ways by eg `jest.resetAllMocks()` (a particularly confusing side-effect when fake timers are enabled globally, as in our setup).

This migrates RN's own tests and config to modern fake timers, or real timers where that's more appropriate.

NOTE: In cases where non-trivial changes to the tests are required, four test files are individually opted-in to `legacyFakeTimers` with a `TODO(legacy-fake-timers)`. I'll open these up for community contributions to fix.

Changelog: [Internal]

Reviewed By: motiz88

Differential Revision: D48189907

fbshipit-source-id: 2e7ce74cc60e80679d81d7c16d599ad1bbe2c921
2023-08-11 11:01:06 -07:00

65 lines
1.9 KiB
JavaScript

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
'use strict';
const {defaults} = require('jest-config');
module.exports = {
transform: {
'^.+\\.(bmp|gif|jpg|jpeg|mp4|png|psd|svg|webp)$':
'<rootDir>/packages/react-native/jest/assetFileTransformer.js',
'.*': './jest/preprocessor.js',
},
setupFiles: ['./packages/react-native/jest/local-setup.js'],
fakeTimers: {
enableGlobally: true,
legacyFakeTimers: false,
},
snapshotFormat: {
escapeString: true,
printBasicPrototype: true,
},
// This allows running Meta-internal tests with the `-test.fb.js` suffix.
testRegex: '/__tests__/.*-test(\\.fb)?\\.js$',
testPathIgnorePatterns: [
'/node_modules/',
'<rootDir>/packages/react-native/template',
'<rootDir>/packages/react-native/Libraries/Renderer',
'<rootDir>/packages/rn-tester/e2e',
],
transformIgnorePatterns: ['node_modules/(?!@react-native/)'],
haste: {
defaultPlatform: 'ios',
platforms: ['ios', 'android'],
},
moduleNameMapper: {
// This module is internal to Meta and used by their custom React renderer.
// In tests, we can just use a mock.
'^ReactNativeInternalFeatureFlags$':
'<rootDir>/packages/react-native/jest/ReactNativeInternalFeatureFlagsMock.js',
},
moduleFileExtensions: ['fb.js'].concat(defaults.moduleFileExtensions),
unmockedModulePathPatterns: [
'node_modules/react/',
'packages/react-native/Libraries/Renderer',
'promise',
'source-map',
'fastpath',
'denodeify',
],
testEnvironment: 'node',
collectCoverageFrom: ['packages/react-native/Libraries/**/*.js'],
coveragePathIgnorePatterns: [
'/__tests__/',
'/vendor/',
'<rootDir>/packages/react-native/Libraries/react-native/',
],
};