Skip to content

Commit

Permalink
extend react-native preset
Browse files Browse the repository at this point in the history
  • Loading branch information
sbalay committed Oct 22, 2020
1 parent fbb96bb commit 64a7648
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 2 deletions.
7 changes: 7 additions & 0 deletions jest-preset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const reactNativePreset = require('react-native/jest-preset');

module.exports = Object.assign({}, reactNativePreset, {
setupFiles: [require.resolve('./save-promise.js')]
.concat(reactNativePreset.setupFiles)
.concat([require.resolve('./restore-promise.js')]),
});
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
"react-test-renderer": "16.13.1"
},
"jest": {
"preset": "react-native",
"setupFilesAfterEnv": ["@testing-library/jest-native/extend-expect"],
"preset": "./jest-preset.js",
"setupFilesAfterEnv": [
"@testing-library/jest-native/extend-expect"
],
"testMatch": [
"**/__tests__/**/?(*.)+(spec).js?(x)"
]
Expand Down
1 change: 1 addition & 0 deletions restore-promise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global.Promise = global.originalPromise;
1 change: 1 addition & 0 deletions save-promise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global.originalPromise = Promise;

4 comments on commit 64a7648

@pke
Copy link

@pke pke commented on 64a7648 Nov 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much! This was a time saver. Spent whole day figuring out what did not work on my test code because it was running into timeouts when using fakeTimers("modern") but without me doing any async tests:

it("should report time updates every 30 minutes", () => {
  jest.setSystemTime(new Date("2020-11-19T10:00:00.000Z"))
  const { result } = renderHook(() => useDateTime("minute"))
  expect(result.current.toISOString()).toEqual("2020-11-19T10:00:00.000Z")
  act(() => {
    console.log("Advancing timer")
    jest.advanceTimersByTime(1000 * 1 * 60)
  })
  expect(result.current.toISOString()).toEqual("2020-11-19T10:01:00.000Z")
})

Your custom preset helped this test to pass! Thank you very much. I think its time to addres this over at the jest react native package?

@JanithaR
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I first came across the dreaded,

Warning: You called act(async () => ...) without await. This could lead to unexpected testing behaviour, interleaving multiple act calls and mixing their scopes. You should - await act(async () => ...);

message in the console many months ago and never understood what caused it. I just accepted it is what it is since all my test suites passed just fine. But always at the back of my mind, it bugged me that the console output wasn't just all green. Now it is all neat and green. 😍

Thank you.

PS: Is this something that we can expect for the react-native team to take care of or the react-native-testing-library team to take care of in a future update?

@gabrielsideprojects
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much! This saved my day! There are a lot of days that my terminal was red because of this warning! rsrs. Now it is all green βœ….
But, I'd like to know how you discover this solution? I have now idea what you thought to resolve this problem rsrs.

@andreialecu
Copy link

@andreialecu andreialecu commented on 64a7648 Apr 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also tried this in order to get rid of the act related warnings, as per: callstack/react-native-testing-library#379 (comment)

However, it seems to have broken all our tests. The first failure was related to the useAppTheme hook from react-native-paper returning undefined. Without the patch it works, but the warning is being logged.

So, be careful with this approach.

Please sign in to comment.