Skip to content

Commit 9769ece

Browse files
committed
Shenanigans to get the RN test passing again
1 parent ed2b23f commit 9769ece

File tree

5 files changed

+22
-21
lines changed

5 files changed

+22
-21
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ jobs:
5151
fail-fast: false
5252
matrix:
5353
node: ['16.x']
54-
ts: ['4.7', '4.8', '4.9', '5.0']
54+
ts: ['4.7', '4.8', '4.9', '5.0', '5.1']
5555
steps:
5656
- name: Checkout repo
5757
uses: actions/checkout@v3

.babelrc.js renamed to babel.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module.exports = {
1616
},
1717
],
1818
'@babel/preset-typescript',
19+
'module:metro-react-native-babel-preset',
1920
],
2021
plugins: [
2122
['@babel/proposal-decorators', { legacy: true }],

jest.config.js

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
const { defaults: tsjPreset } = require('ts-jest/presets')
2-
3-
const defaults = {
4-
coverageDirectory: './coverage/',
5-
collectCoverage: true,
6-
testURL: 'http://localhost',
7-
}
8-
91
process.env.TS_JEST_DISABLE_VER_CHECKER = true
102

113
const NORMAL_TEST_FOLDERS = ['components', 'hooks', 'integration', 'utils']
@@ -14,20 +6,22 @@ const tsTestFolderPath = (folderName) =>
146
`<rootDir>/test/${folderName}/**/*.{ts,tsx}`
157

168
const tsStandardConfig = {
17-
...defaults,
189
displayName: 'ReactDOM 18',
1910
preset: 'ts-jest',
2011
testMatch: NORMAL_TEST_FOLDERS.map(tsTestFolderPath),
12+
testEnvironment: 'jsdom',
13+
setupFilesAfterEnv: ['<rootDir>/jest.setupAfter.js'],
2114
}
2215

2316
const rnConfig = {
24-
...defaults,
2517
displayName: 'React Native',
2618
testMatch: [tsTestFolderPath('react-native')],
2719
preset: 'react-native',
2820
transform: {
29-
'^.+\\.js$': '<rootDir>/node_modules/react-native/jest/preprocessor.js',
30-
...tsjPreset.transform,
21+
'^.+\\.(js|jsx|ts|tsx)$': [
22+
'babel-jest',
23+
{ configFile: './babel.config.js' }, // <- cannot use rootDir here
24+
],
3125
},
3226
}
3327

jest.setupAfter.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const { TextEncoder, TextDecoder } = require('util')
2+
global.TextEncoder = TextEncoder
3+
global.TextDecoder = TextDecoder

test/react-native/batch-integration.tsx

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -465,10 +465,12 @@ describe('React Native', () => {
465465
})
466466

467467
describe('useSelector', () => {
468-
it('should stay in sync with the store', () => {
468+
it('should stay in sync with the store', async () => {
469469
// https://github.com/reduxjs/react-redux/issues/1437
470470

471-
jest.useFakeTimers()
471+
function delay(ms: number) {
472+
return new Promise((resolve) => setTimeout(resolve, ms))
473+
}
472474

473475
// Explicitly silence "not wrapped in act()" messages for this test
474476
const spy = jest.spyOn(console, 'error')
@@ -587,21 +589,22 @@ describe('React Native', () => {
587589
rtl.fireEvent.press(button)
588590
}
589591

590-
const clickAndRender = (rendered: RenderedType, testId: string) => {
592+
const clickAndRender = async (rendered: RenderedType, testId: string) => {
591593
// Note: Normally we'd wrap this all in act(), but that automatically
592594
// wraps your code in batchedUpdates(). The point of this bug is that it
593595
// specifically occurs when you are _not_ batching updates!
594596
clickButton(rendered, testId)
595-
jest.advanceTimersByTime(100)
597+
// jest.advanceTimersByTime(100)
598+
await delay(100)
596599
assertValuesMatch(rendered)
597600
}
598601

599602
assertValuesMatch(rendered)
600603

601-
clickAndRender(rendered, 'setTimeout')
602-
clickAndRender(rendered, 'standardBatching')
603-
clickAndRender(rendered, 'unstableBatched')
604-
clickAndRender(rendered, 'reactReduxBatch')
604+
await clickAndRender(rendered, 'setTimeout')
605+
await clickAndRender(rendered, 'standardBatching')
606+
await clickAndRender(rendered, 'unstableBatched')
607+
await clickAndRender(rendered, 'reactReduxBatch')
605608

606609
spy.mockRestore()
607610
})

0 commit comments

Comments
 (0)