Skip to content

Commit

Permalink
test: Fix open handles to improve test lisibility
Browse files Browse the repository at this point in the history
We have a lot of open handles flooding the end of our test results. It seems that they are due to a React issue fixed in 18+.

Dan Abramov wrote a small package to fix the issue temporarly. I added it here manually because it has an error in the semver version check. This code should be removed when upgrading React to 18+.

facebook/react#20756 (comment)
  • Loading branch information
zatteo committed Dec 5, 2023
1 parent 156aecc commit 06265a1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
2 changes: 2 additions & 0 deletions __tests__/jestSetupFile.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import './openHandleFix'

import mockNotifee from '@notifee/react-native/jest-mock'
import mockAsyncStorage from '@react-native-async-storage/async-storage/jest/async-storage-mock'
import mockRNCameraRoll from '@react-native-camera-roll/camera-roll/src/__mocks__/nativeInterface'
Expand Down
34 changes: 34 additions & 0 deletions __tests__/openHandleFix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* eslint-disable no-console */
/* eslint-disable prettier/prettier */
'use strict';

// -- Added by Cozy --
// This code is taken from react-16-node-hanging-test-fix package
// which has an error in the semver version check
// -- Added by Cozy --

// See https://github.com/facebook/react/issues/20756
// This fix is only useful in a test environment with
// Node 15+, jsdom, and React < 17.1.0.

// It must be imported *before* any imports of 'react-dom'.

const version = require('react').version;
const semverGt = require('semver/functions/gt');

if (Object.prototype.toString.call(process) !== '[object process]') {
throw Error(
'The `react-16-node-hanging-test-fix` package must only be used in a Node environment. ' +
'Remove this import from your application code.'
);
}

if (semverGt(version, '18.0.0')) {
console.error(
'The `react-16-node-hanging-test-fix` package is no longer needed ' +
'with React ' + version + ' and may cause issues. Remove this import.'
)
}

// This is the "fix".
delete global.MessageChannel;

0 comments on commit 06265a1

Please sign in to comment.