Skip to content
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

fix: React next fails on build (#726) #732

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
"license": "MIT",
"dependencies": {
"@babel/runtime": "^7.10.3",
"@testing-library/dom": "^7.17.1"
"@testing-library/dom": "^7.17.1",
"semver": "^7.3.2"
},
"devDependencies": {
"@reach/router": "^1.3.3",
Expand Down
32 changes: 31 additions & 1 deletion src/flush-microtasks.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import React from 'react'
import satisfies from 'semver/functions/satisfies'

/* istanbul ignore file */
// the part of this file that we need tested is definitely being run
// and the part that is not cannot easily have useful tests written
Expand All @@ -15,8 +18,15 @@ function getIsUsingFakeTimers() {
)
}

const globalObj = typeof window === 'undefined' ? global : window
let Scheduler = globalObj.Scheduler
const isModernScheduleCallbackSupported = satisfies(React.version, '>16.8.6', {
includePrerelease: true,
})

let didWarnAboutMessageChannel = false
let enqueueTask

try {
// read require off the module object to get around the bundlers.
// we don't want them to detect a require and bundle a Node polyfill.
kentcdodds marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -25,6 +35,8 @@ try {
// assuming we're in node, let's try to get node's
// version of setImmediate, bypassing fake timers if any.
enqueueTask = nodeRequire.call(module, 'timers').setImmediate
// import React's scheduler so we'll be able to schedule our tasks later on.
Scheduler = nodeRequire.call(module, 'scheduler')
} catch (_err) {
// we're in a browser
// we can't use regular timers because they may still be faked
Expand All @@ -49,6 +61,20 @@ try {
}
}

function scheduleCallback(cb) {
const NormalPriority = Scheduler
? Scheduler.NormalPriority || Scheduler.unstable_NormalPriority
: null

const scheduleFn = Scheduler
? Scheduler.scheduleCallback || Scheduler.unstable_scheduleCallback
: callback => callback()

return isModernScheduleCallbackSupported
? scheduleFn(NormalPriority, cb)
: scheduleFn(cb)
}

export default function flushMicroTasks() {
return {
then(resolve) {
Expand All @@ -59,7 +85,11 @@ export default function flushMicroTasks() {
jest.advanceTimersByTime(0)
resolve()
} else {
enqueueTask(resolve)
scheduleCallback(() => {
enqueueTask(() => {
resolve()
})
})
}
},
}
Expand Down