Unverified Commit 01e7c373 authored by Sebastian Silbermann's avatar Sebastian Silbermann Committed by GitHub

Ensure "[Fast Refresh] rebuilding" logs have a matching "[Fast Refresh] done"…

Ensure "[Fast Refresh] rebuilding" logs have a matching "[Fast Refresh] done" log in Webpack (#67968)
parent f65a064f
...@@ -59,7 +59,6 @@ function onFastRefresh( ...@@ -59,7 +59,6 @@ function onFastRefresh(
updatedModules: ReadonlyArray<string> updatedModules: ReadonlyArray<string>
) { ) {
dispatcher.onBuildOk() dispatcher.onBuildOk()
reportHmrLatency(sendMessage, updatedModules) reportHmrLatency(sendMessage, updatedModules)
dispatcher.onRefresh() dispatcher.onRefresh()
...@@ -161,6 +160,7 @@ function tryApplyUpdates( ...@@ -161,6 +160,7 @@ function tryApplyUpdates(
) { ) {
if (!isUpdateAvailable() || !canApplyUpdates()) { if (!isUpdateAvailable() || !canApplyUpdates()) {
dispatcher.onBuildOk() dispatcher.onBuildOk()
reportHmrLatency(sendMessage, [])
return return
} }
...@@ -430,7 +430,6 @@ function processMessage( ...@@ -430,7 +430,6 @@ function processMessage(
router.hmrRefresh() router.hmrRefresh()
dispatcher.onRefresh() dispatcher.onRefresh()
}) })
reportHmrLatency(sendMessage, [])
if (process.env.__NEXT_TEST_MODE) { if (process.env.__NEXT_TEST_MODE) {
if (self.__NEXT_HMR_CB) { if (self.__NEXT_HMR_CB) {
......
...@@ -3,8 +3,6 @@ import { retry, waitFor } from 'next-test-utils' ...@@ -3,8 +3,6 @@ import { retry, waitFor } from 'next-test-utils'
const envFile = '.env.development.local' const envFile = '.env.development.local'
const isPPREnabledByDefault = process.env.__NEXT_EXPERIMENTAL_PPR === 'true'
describe(`app-dir-hmr`, () => { describe(`app-dir-hmr`, () => {
const { next } = nextTestSetup({ const { next } = nextTestSetup({
files: __dirname, files: __dirname,
...@@ -57,12 +55,11 @@ describe(`app-dir-hmr`, () => { ...@@ -57,12 +55,11 @@ describe(`app-dir-hmr`, () => {
}) })
it('should update server components after navigating to a page with a different runtime', async () => { it('should update server components after navigating to a page with a different runtime', async () => {
const envContent = await next.readFile(envFile)
const browser = await next.browser('/env/node') const browser = await next.browser('/env/node')
await browser.loadPage(`${next.url}/env/edge`) await browser.loadPage(`${next.url}/env/edge`)
await browser.eval('window.__TEST_NO_RELOAD = true') await browser.eval('window.__TEST_NO_RELOAD = true')
expect(await browser.elementByCss('p').text()).toBe('mac')
await next.patchFile(envFile, 'MY_DEVICE="ipad"') await next.patchFile(envFile, 'MY_DEVICE="ipad"')
try { try {
...@@ -73,7 +70,7 @@ describe(`app-dir-hmr`, () => { ...@@ -73,7 +70,7 @@ describe(`app-dir-hmr`, () => {
const fastRefreshLogs = logs.filter((log) => { const fastRefreshLogs = logs.filter((log) => {
return log.message.startsWith('[Fast Refresh]') return log.message.startsWith('[Fast Refresh]')
}) })
// FIXME: 3+ "rebuilding" but single "done" is confusing. // FIXME: 3+ "rebuilding" but no "done" is confusing.
// There may actually be more "rebuilding" but not reliably. // There may actually be more "rebuilding" but not reliably.
// To ignore this flakiness, we just assert on subset matches. // To ignore this flakiness, we just assert on subset matches.
// Once the bug is fixed, each "rebuilding" should be paired with a "done in" exactly. // Once the bug is fixed, each "rebuilding" should be paired with a "done in" exactly.
...@@ -81,62 +78,69 @@ describe(`app-dir-hmr`, () => { ...@@ -81,62 +78,69 @@ describe(`app-dir-hmr`, () => {
expect.arrayContaining([ expect.arrayContaining([
{ source: 'log', message: '[Fast Refresh] rebuilding' }, { source: 'log', message: '[Fast Refresh] rebuilding' },
{ source: 'log', message: '[Fast Refresh] rebuilding' }, { source: 'log', message: '[Fast Refresh] rebuilding' },
{
source: 'log',
message: expect.stringContaining('[Fast Refresh] done in '),
},
{ source: 'log', message: '[Fast Refresh] rebuilding' }, { source: 'log', message: '[Fast Refresh] rebuilding' },
]) ])
) )
// FIXME: Turbopack should have matching "done in" for each "rebuilding"
expect(logs).not.toEqual(
expect.arrayContaining([
expect.objectContaining({
message: expect.stringContaining('[Fast Refresh] done in'),
source: 'log',
}),
])
)
}) })
} else { } else {
await retry( await retry(
async () => { async () => {
const fastRefreshLogs = logs.filter((log) => { const envValue = await browser.elementByCss('p').text()
return log.message.startsWith('[Fast Refresh]') const mpa = await browser.eval(
'window.__TEST_NO_RELOAD === undefined'
)
// Used to be flaky but presumably no longer is.
// If this flakes again, please add the received value as a commnet.
expect({ envValue, mpa }).toEqual({
envValue: 'ipad',
mpa: false,
}) })
// FIXME: Should be either a single "rebuilding"+"done" or the last "rebuilding" should be followed by "done"
expect(fastRefreshLogs).toEqual([
{ source: 'log', message: '[Fast Refresh] rebuilding' },
{ source: 'log', message: '[Fast Refresh] rebuilding' },
{
source: 'log',
message: expect.stringContaining('[Fast Refresh] done in '),
},
{ source: 'log', message: '[Fast Refresh] rebuilding' },
])
}, },
// Very slow Hot Update for some reason. // Very slow Hot Update for some reason.
// May be related to receiving 3 rebuild events but only one finish event // May be related to receiving 3 rebuild events but only one finish event
5000 5000
) )
const fastRefreshLogs = logs.filter((log) => {
return log.message.startsWith('[Fast Refresh]')
})
expect(fastRefreshLogs).toEqual([
{ source: 'log', message: '[Fast Refresh] rebuilding' },
{
source: 'log',
message: expect.stringContaining('[Fast Refresh] done in '),
},
{ source: 'log', message: '[Fast Refresh] rebuilding' },
{ source: 'log', message: '[Fast Refresh] rebuilding' },
{
source: 'log',
message: expect.stringContaining('[Fast Refresh] done in '),
},
{
source: 'log',
message: expect.stringContaining('[Fast Refresh] done in '),
},
])
} }
const envValue = await browser.elementByCss('p').text()
const mpa = await browser.eval('window.__TEST_NO_RELOAD === undefined')
// Flaky sometimes in Webpack:
// A. misses update and just receives `{ envValue: 'mac', mpa: false }`
// B. triggers error on server resulting in MPA: `{ envValue: 'ipad', mpa: true }` and server logs: ⨯ [TypeError: Cannot read properties of undefined (reading 'polyfillFiles')] ⨯ [TypeError: Cannot read properties of null (reading 'default')]
// A is more common than B.
expect({ envValue, mpa }).toEqual({
envValue:
isPPREnabledByDefault && !process.env.TURBOPACK
? // FIXME: Should be 'ipad' but PPR+Webpack swallows the update reliably
'mac'
: 'ipad',
mpa: false,
})
} finally { } finally {
await next.patchFile(envFile, envContent) // TOOD: use sandbox instead
await next.patchFile(envFile, 'MY_DEVICE="mac"')
await retry(async () => {
expect(await browser.elementByCss('p').text()).toBe('mac')
})
} }
}) })
it('should update server components pages when env files is changed (nodejs)', async () => { it('should update server components pages when env files is changed (nodejs)', async () => {
// If "should update server components after navigating to a page with a different runtime" failed, the dev server is in a corrupted state.
// Restart fixes this.
await next.stop()
await next.start()
const envContent = await next.readFile(envFile)
const browser = await next.browser('/env/node') const browser = await next.browser('/env/node')
expect(await browser.elementByCss('p').text()).toBe('mac') expect(await browser.elementByCss('p').text()).toBe('mac')
await next.patchFile(envFile, 'MY_DEVICE="ipad"') await next.patchFile(envFile, 'MY_DEVICE="ipad"')
...@@ -158,25 +162,36 @@ describe(`app-dir-hmr`, () => { ...@@ -158,25 +162,36 @@ describe(`app-dir-hmr`, () => {
expect(await browser.elementByCss('p').text()).toBe('ipad') expect(await browser.elementByCss('p').text()).toBe('ipad')
}) })
expect(logs).toEqual( if (process.env.TURBOPACK) {
expect.arrayContaining([ // FIXME: Turbopack should have matching "done in" for each "rebuilding"
expect.objectContaining({ expect(logs).not.toEqual(
message: expect.stringContaining('[Fast Refresh] done in'), expect.arrayContaining([
source: 'log', expect.objectContaining({
}), message: expect.stringContaining('[Fast Refresh] done in'),
]) source: 'log',
) }),
])
)
} else {
expect(logs).toEqual(
expect.arrayContaining([
expect.objectContaining({
message: expect.stringContaining('[Fast Refresh] done in'),
source: 'log',
}),
])
)
}
} finally { } finally {
await next.patchFile(envFile, envContent) // TOOD: use sandbox instead
await next.patchFile(envFile, 'MY_DEVICE="mac"')
await retry(async () => {
expect(await browser.elementByCss('p').text()).toBe('mac')
})
} }
}) })
it('should update server components pages when env files is changed (edge)', async () => { it('should update server components pages when env files is changed (edge)', async () => {
// Restart to work around a bug highlighted in the flakiness of "should update server components after navigating to a page with a different runtime"
await next.stop()
await next.start()
const envContent = await next.readFile(envFile)
const browser = await next.browser('/env/edge') const browser = await next.browser('/env/edge')
expect(await browser.elementByCss('p').text()).toBe('mac') expect(await browser.elementByCss('p').text()).toBe('mac')
await next.patchFile(envFile, 'MY_DEVICE="ipad"') await next.patchFile(envFile, 'MY_DEVICE="ipad"')
...@@ -198,16 +213,32 @@ describe(`app-dir-hmr`, () => { ...@@ -198,16 +213,32 @@ describe(`app-dir-hmr`, () => {
expect(await browser.elementByCss('p').text()).toBe('ipad') expect(await browser.elementByCss('p').text()).toBe('ipad')
}) })
expect(logs).toEqual( if (process.env.TURBOPACK) {
expect.arrayContaining([ // FIXME: Turbopack should have matching "done in" for each "rebuilding"
expect.objectContaining({ expect(logs).not.toEqual(
message: expect.stringContaining('[Fast Refresh] done in'), expect.arrayContaining([
source: 'log', expect.objectContaining({
}), message: expect.stringContaining('[Fast Refresh] done in'),
]) source: 'log',
) }),
])
)
} else {
expect(logs).toEqual(
expect.arrayContaining([
expect.objectContaining({
message: expect.stringContaining('[Fast Refresh] done in'),
source: 'log',
}),
])
)
}
} finally { } finally {
await next.patchFile(envFile, envContent) // TOOD: use sandbox instead
await next.patchFile(envFile, 'MY_DEVICE="mac"')
await retry(async () => {
expect(await browser.elementByCss('p').text()).toBe('mac')
})
} }
}) })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment