Skip to content

Commit

Permalink
add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Dec 5, 2024
1 parent ddbe8bf commit 9d3367a
Show file tree
Hide file tree
Showing 15 changed files with 351 additions and 52 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { text } from 'my-package/typescript.ts'

export default function Page() {
return <p>{text}</p>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use client'

import { text } from 'my-package/typescript.ts'

export default function Page() {
return <p>{text}</p>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Page() {
return <p>hello world</p>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
throw new Error('Expected error')
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use client'

import { useEffect } from 'react'

export default function Page() {
useEffect(function effectCallback() {
innerFunction()
})
return <p>Hello Source Maps</p>
}

function innerFunction() {
innerArrowFunction()
}

const innerArrowFunction = () => {
require('../separate-file')
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export default async function Page({ searchParams }) {
innerFunction()
}

function innerFunction() {
innerArrowFunction()
}

const innerArrowFunction = () => {
require('../separate-file')
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use client'

export default function Page() {
innerFunction()
}

function innerFunction() {
innerArrowFunction()
}

const innerArrowFunction = () => {
require('../separate-file')
}
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: 12 additions & 0 deletions test/e2e/app-dir/non-root-project-monorepo/apps/web/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "web",
"version": "0.0.0",
"dependencies": {
"my-package": "workspace:*"
},
"scripts": {
"dev": "next dev",
"start": "next start",
"build": "next build"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
import { nextTestSetup, FileRef } from 'e2e-utils'
import {
assertHasRedbox,
assertNoRedbox,
getRedboxCallStack,
getRedboxSource,
} from 'next-test-utils'
import * as path from 'path'

describe('non-root-project-monorepo', () => {
const { next, skipped, isTurbopack, isNextDev } = nextTestSetup({
files: {
apps: new FileRef(path.resolve(__dirname, 'apps')),
packages: new FileRef(path.resolve(__dirname, 'packages')),
'pnpm-workspace.yaml': `packages:
- 'apps/*'
- 'packages/*'
`,
},
packageJson: require('./package.json'),
buildCommand: 'pnpm build',
startCommand: (global as any).isNextDev ? 'pnpm dev' : 'pnpm start',
installCommand: 'pnpm i',
skipDeployment: true,
})

if (skipped) {
return
}

describe('monorepo-package', () => {
it('should work during RSC', async () => {
const $ = await next.render$('/monorepo-package-rsc')
expect($('p').text()).toBe('Hello Typescript')
})

it('should work during SSR', async () => {
const $ = await next.render$('/monorepo-package-ssr')
expect($('p').text()).toBe('Hello Typescript')
})

it('should work on client-side', async () => {
const browser = await next.browser('/monorepo-package-ssr')
expect(await browser.elementByCss('p').text()).toBe('Hello Typescript')
await assertNoRedbox(browser)
expect(await browser.elementByCss('p').text()).toBe('Hello Typescript')
await browser.close()
})
})

if (isNextDev) {
describe('source-maps', () => {
function normalizeStackTrace(stack: string): string {
const isolatedPath = /file:\/\/.*\/next-install-[^/]+\//g
const nonIsolatedPath =
/file:\/\/.*\/test\/e2e\/app-dir\/non-root-project-monorepo\//g
return stack
.replaceAll(nonIsolatedPath, 'file://<full-path>/')
.replaceAll(isolatedPath, 'file://<full-path>/')
}

it('should work on RSC', async () => {
const browser = await next.browser('/source-maps-rsc')
await assertHasRedbox(browser)

if (isTurbopack) {
// TODO the function name should be hidden
expect(await getRedboxSource(browser)).toMatchInlineSnapshot(`
"./app/source-maps-rsc/page.tsx (9:28) @ innerArrowFunction
7 | }
8 |
> 9 | const innerArrowFunction = () => {
| ^
10 | require('../separate-file')
11 | }
12 |"
`)
// TODO stacktrace-parser breaks in some cases with the rsc:// protocol
expect(normalizeStackTrace(await getRedboxCallStack(browser)))
.toMatchInlineSnapshot(`
"<unknown>
[project]/apps/web/app/separate-file.ts [app-rsc] (ecmascript) (rsc://React/Server/file://<full-path>/apps/web/.next/server/chunks/ssr/apps_web_8d1c0a._.js (7:7)
innerFunction
./app/source-maps-rsc/page.tsx (6:3)
Page
./app/source-maps-rsc/page.tsx (2:3)"
`)
} else {
// TODO the function name is incorrect
expect(await getRedboxSource(browser)).toMatchInlineSnapshot(`
"app/separate-file.ts (1:11) @ Error
> 1 | throw new Error('Expected error')
| ^
2 |"
`)
// TODO webpack runtime code shouldn't be included in stack trace
expect(normalizeStackTrace(await getRedboxCallStack(browser)))
.toMatchInlineSnapshot(`
"<unknown>
rsc)/./app/separate-file.ts (rsc://React/Server/file://<full-path>/apps/web/.next/server/app/source-maps-rsc/page.js
__webpack_require__
file://<full-path>/apps/web/.next/server/webpack-runtime.js
require
app/source-maps-rsc/page.tsx (10:3)
innerArrowFunction
app/source-maps-rsc/page.tsx (6:3)
innerFunction
app/source-maps-rsc/page.tsx (2:3)"
`)
}
await browser.close()
})

it('should work on SSR', async () => {
const browser = await next.browser('/source-maps-ssr')
await assertHasRedbox(browser)

if (isTurbopack) {
// TODO the function name should be hidden
expect(await getRedboxSource(browser)).toMatchInlineSnapshot(`
"./app/separate-file.ts (1:7) @ [project]/apps/web/app/separate-file.ts [app-client] (ecmascript)
> 1 | throw new Error('Expected error')
| ^
2 |"
`)
expect(normalizeStackTrace(await getRedboxCallStack(browser)))
.toMatchInlineSnapshot(`
"innerArrowFunction
./app/source-maps-ssr/page.tsx (11:28)
innerFunction
./app/source-maps-ssr/page.tsx (8:3)
Page
./app/source-maps-ssr/page.tsx (4:3)"
`)
} else {
// TODO the function name should be hidden
expect(await getRedboxSource(browser)).toMatchInlineSnapshot(`
"app/separate-file.ts (1:7) @ eval
> 1 | throw new Error('Expected error')
| ^
2 |"
`)
// TODO webpack runtime code shouldn't be included in stack trace
expect(normalizeStackTrace(await getRedboxCallStack(browser)))
.toMatchInlineSnapshot(`
"./app/separate-file.ts
file://<full-path>/apps/web/.next/static/chunks/app/source-maps-ssr/page.js (27:1)
options.factory
file://<full-path>/apps/web/.next/static/chunks/webpack.js (700:31)
__webpack_require__
file://<full-path>/apps/web/.next/static/chunks/webpack.js (37:33)
fn
file://<full-path>/apps/web/.next/static/chunks/webpack.js (357:21)
require
app/source-maps-ssr/page.tsx (12:3)
innerArrowFunction
app/source-maps-ssr/page.tsx (8:3)
innerFunction
app/source-maps-ssr/page.tsx (4:3)"
`)
}
await browser.close()
})

it('should work on client-side', async () => {
const browser = await next.browser('/source-maps-client')
await assertHasRedbox(browser)

if (isTurbopack) {
// TODO the function name should be hidden
expect(await getRedboxSource(browser)).toMatchInlineSnapshot(`
"./app/separate-file.ts (1:7) @ [project]/apps/web/app/separate-file.ts [app-client] (ecmascript)
> 1 | throw new Error('Expected error')
| ^
2 |"
`)
expect(normalizeStackTrace(await getRedboxCallStack(browser)))
.toMatchInlineSnapshot(`
"innerArrowFunction
./app/source-maps-client/page.tsx (16:28)
innerFunction
./app/source-maps-client/page.tsx (13:3)
effectCallback
./app/source-maps-client/page.tsx (7:5)"
`)
} else {
// TODO the function name should be hidden
expect(await getRedboxSource(browser)).toMatchInlineSnapshot(`
"app/separate-file.ts (1:7) @ eval
> 1 | throw new Error('Expected error')
| ^
2 |"
`)
// TODO webpack runtime code shouldn't be included in stack trace
expect(normalizeStackTrace(await getRedboxCallStack(browser)))
.toMatchInlineSnapshot(`
"./app/separate-file.ts
file://<full-path>/apps/web/.next/static/chunks/app/source-maps-client/page.js (27:1)
options.factory
file://<full-path>/apps/web/.next/static/chunks/webpack.js (712:31)
__webpack_require__
file://<full-path>/apps/web/.next/static/chunks/webpack.js (37:33)
fn
file://<full-path>/apps/web/.next/static/chunks/webpack.js (369:21)
require
app/source-maps-client/page.tsx (17:3)
innerArrowFunction
app/source-maps-client/page.tsx (13:3)
innerFunction
app/source-maps-client/page.tsx (7:5)"
`)
}
await browser.close()
})
})
}
})
10 changes: 10 additions & 0 deletions test/e2e/app-dir/non-root-project-monorepo/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "monorepo-root",
"version": "0.0.0",
"private": true,
"scripts": {
"build": "pnpm i && pnpm run --dir apps/web build",
"start": "pnpm run --dir apps/web start",
"dev": "pnpm i && pnpm run --dir apps/web dev"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "my-package",
"version": "0.0.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const text: string = 'Hello Typescript'
Loading

0 comments on commit 9d3367a

Please sign in to comment.