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

Add reproduction for HMR moving / renaming files. #57230

Merged
merged 4 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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: 3 additions & 0 deletions test/development/app-dir/hmr-move-file/app/button.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Button() {
return <button id="hello-world-button">hello world</button>
}
7 changes: 7 additions & 0 deletions test/development/app-dir/hmr-move-file/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function Root({ children }: { children: React.ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
4 changes: 4 additions & 0 deletions test/development/app-dir/hmr-move-file/app/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Button from './button'
export default function Page() {
return <Button />
}
28 changes: 28 additions & 0 deletions test/development/app-dir/hmr-move-file/hmr-move-file.test.ts
Original file line number Diff line number Diff line change
@@ -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'
)
})
}
)
6 changes: 6 additions & 0 deletions test/development/app-dir/hmr-move-file/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {}

module.exports = nextConfig
12 changes: 10 additions & 2 deletions test/lib/next-modes/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion test/lib/next-test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@ export async function hasRedbox(browser: BrowserInterface, expected = true) {
}
await waitFor(1000)
}
return false
return !expected
}

export async function getRedboxHeader(browser: BrowserInterface) {
Expand Down
Loading