Add reproduction for HMR moving / renaming files. (#57230)

Pairing with @timneutkens. Passes with webpack, fails with Turbopack.

```
TURBOPACK=1 pn testonly-dev test/development/app-dir/hmr-move-file
```

---------

Co-authored-by: Leah <[email protected]>
Co-authored-by: Tobias Koppers <[email protected]>
This commit is contained in:
Lee Robinson
2024-01-09 19:02:00 +01:00
committed by GitHub
co-authored by Leah Tobias Koppers
parent 967784b203
commit fb73c3a9d2
6 changed files with 58 additions and 2 deletions
@@ -0,0 +1,3 @@
export default function Button() {
return <button id="hello-world-button">hello world</button>
}
@@ -0,0 +1,7 @@
export default function Root({ children }: { children: React.ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
@@ -0,0 +1,4 @@
import Button from './button'
export default function Page() {
return <Button />
}
@@ -0,0 +1,28 @@
import { createNextDescribe } from 'e2e-utils'
import { hasRedbox } from 'next-test-utils'
createNextDescribe(
'HMR Move File',
{
files: __dirname,
},
({ next }) => {
it('should work when moving a component to another directory', async () => {
const browser = await next.browser('/')
expect(await browser.elementByCss('#hello-world-button').text()).toBe(
'hello world'
)
await next.renameFile('app/button.tsx', 'app/button2.tsx')
await next.patchFile('app/page.tsx', (content) =>
content.replace('./button', './button2')
)
expect(await hasRedbox(browser, false)).toBe(false)
expect(await browser.elementByCss('#hello-world-button').text()).toBe(
'hello world'
)
})
}
)
@@ -0,0 +1,6 @@
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {}
module.exports = nextConfig
+10 -2
View File
@@ -455,13 +455,21 @@ export class NextInstance {
await new Promise((resolve) => setTimeout(resolve, 500))
}
}
public async patchFile(filename: string, content: string) {
public async patchFile(
filename: string,
content: string | ((contents: string) => string)
) {
await this.handleDevWatchDelayBeforeChange(filename)
const outputPath = path.join(this.testDir, filename)
const newFile = !(await fs.pathExists(outputPath))
await fs.ensureDir(path.dirname(outputPath))
await fs.writeFile(outputPath, content)
await fs.writeFile(
outputPath,
typeof content === 'function'
? content(await this.readFile(filename))
: content
)
if (newFile) {
await this.handleDevWatchDelayAfterChange(filename)