New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Upgrade to react-scripts v4 #919
Comments
@raineorshine I finally figured out why Line 120 in 5baf45d
It still does not pass However tests are failing for |
@raineorshine |
@raineorshine I found the reason for dexie not working with modern implementation of fake timer. With new jest , get: function (_a) {
var trans = _a.trans, key = _a.key;
return new Promise(function (resolve, reject) {
resolve = wrap(resolve);
var store = trans.objectStore(tableName);
var req = store.get(key);
req.onsuccess = function (event) { return resolve(event.target.result); };
req.onerror = eventRejectHandler(reject);
});
}, This is a code snippet from If we use I was tempted to use different timer implementation when needed throughout the test but it seems impossible to do so. However I was was able to fix current |
Nice work! Let's make sure to document relevant links so we have them for our records. jestjs/jest#10221 seems to be the most relevant issue. If faking Is it only I also saw a few solutions that were suggested. Did you try these?
Let me know what you've tried and what you haven't, or if you get stuck and would like me to jump in. Thanks! |
@raineorshine I tried import FakeTimers from '@sinonjs/fake-timers'
it('test', async () => {
const clock = FakeTimers.install()
initialize()
await clock.runAllAsync()
expect(1).toBe(1)
}) Everything get's initialized properly. All the timers gets flushed and promise callbacks are executed too. Including
|
Problems with jest fake timer
Why use Sinon Fake TimersSinon js fake timer mocks import FakeTimers from '@sinonjs/fake-timers'
it('With sinon js fake timer', async () => {
const clock = FakeTimers.install()
new Promise(resolve => setImmediate(() => {
console.log('Inside setImmediate')
resolve(true)
})).then(() => {
console.log('Flush Promise')
})
await clock.runAllAsync()
expect(1).toBe(1)
console.log('Test complete')
})
// 'Inside setImmediate'
// 'Flush Promise'
// 'Test complete'
https://jestjs.io/blog/2020/05/05/jest-26#new-fake-timers Jest 26 timers are based on sinon fake timers but it doesn't seem to flush promises. Usage in testSample of a it('persist newThought', async () => {
const clock = FakeTimers.install()
store.dispatch({ type: 'newThought', value: 'a' })
// wait till all the pending timers are run and promise callbacks are executed. https://github.com/sinonjs/fake-timers#clockrunall--await-clockrunallasync
await clock.runAllAsync()
// Note: make sure to use real timer before awaiting db calls. Else it will time out. More on that in the caveat section below
clock.uninstall()
const parentEntryRoot = await getContext(db, [ROOT_TOKEN])
expect(parentEntryRoot).toMatchObject({
children: [{ value: 'a', rank: 0 }]
})
}) CaveatDo not use fake timer before awaited get: function (_a) {
var trans = _a.trans, key = _a.key;
return new Promise(function (resolve, reject) {
resolve = wrap(resolve);
var store = trans.objectStore(tableName);
var req = store.get(key);
req.onsuccess = function (event) { return resolve(event.target.result); };
req.onerror = eventRejectHandler(reject);
});
}, This is a code snippet from With both // jest_legacy.test.js
it('with jest legacy timer', async () => {
// this test passes
jest.useFakeTimers('legacy')
await new Promise(setImmediate)
expect(1).toBe(1)
})
// jest_modern.test.js
it('with jest modern timer', async () => {
jest.useFakeTimers('modern')
// this times out and fails
await new Promise(setImmediate)
expect(1).toBe(1)
})
// sinon.test.js
it('with sinon js', async () => {
const clock = FakeTimers.install()
// this times out and fails
await new Promise(setImmediate)
// similarly
expect(1).toBe(1)
}) Similarly, // This test times out and fails
it('persist newThought', async () => {
const clock = FakeTimers.install()
store.dispatch({ type: 'newThought', value: 'a' })
// wait till all the pending timers are run and promise callbacks are executed. https://github.com/sinonjs/fake-timers#clockrunall--await-clockrunallasync
await clock.runAllAsync()
// clock.uninstall()
// this will timeout just like the above examples
const parentEntryRoot = await getContext(db, [ROOT_TOKEN])
expect(parentEntryRoot).toMatchObject({
children: [{ value: 'a', rank: 0 }]
})
}) |
The pullQueue tests fail on
react-scripts
v4. It is related to the fake timer and mock debounce logic, but I can't figure out how to fix it. We should be able to useuseFakeTimers('modern')
now and not have to mock lodash's debounce, but that causes an error.Here are the CHANGELOGS although I didn't find anything relevant:
The text was updated successfully, but these errors were encountered: