Skip to content

Commit

Permalink
fix concurrency issue and invalid snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
ijjk committed Apr 12, 2023
1 parent ea9f891 commit d84982d
Show file tree
Hide file tree
Showing 13 changed files with 62 additions and 31 deletions.
9 changes: 6 additions & 3 deletions .github/workflows/build_test_deploy_new.yml
Expand Up @@ -11,7 +11,9 @@ env:
PNPM_VERSION: 7.24.3
NODE_MAINTENANCE_VERSION: 16
NODE_LTS_VERSION: 18
TEST_CONCURRENCY: 10
TEST_CONCURRENCY: 6
# TODO: remove after testing
NEXT_TEST_CONTINUE_ON_ERROR: 'true'

jobs:
build:
Expand Down Expand Up @@ -61,10 +63,11 @@ jobs:

- run: rm -rf /tmp/next-repo-*; rm -rf /tmp/next-install-*

- run: node run-tests.js --type unit -c ${TEST_CONCURRENCY}
name: Run development/ tests

- run: node run-tests.js --type development -c ${TEST_CONCURRENCY}
name: Run development/ tests
env:
NEXT_TEST_CONTINUE_ON_ERROR: 'true'

- run: node run-tests.js --type production -c ${TEST_CONCURRENCY}
name: Run production/ tests
Expand Down
11 changes: 8 additions & 3 deletions run-tests.js
Expand Up @@ -375,9 +375,14 @@ async function main() {
testNames.map(async (test) => {
const dirName = path.dirname(test)
let dirSema = directorySemas.get(dirName)
if (dirSema === undefined)

// we only restrict 1 test per directory for
// legacy integration tests
if (!testType && dirSema === undefined) {
directorySemas.set(dirName, (dirSema = new Sema(1)))
await dirSema.acquire()
}
if (dirSema) await dirSema.acquire()

await sema.acquire()
let passed = false

Expand Down Expand Up @@ -454,7 +459,7 @@ async function main() {
}

sema.release()
dirSema.release()
if (dirSema) dirSema.release()
})
)

Expand Down
2 changes: 1 addition & 1 deletion test/development/acceptance-app/ReactRefreshLogBox.test.ts
Expand Up @@ -226,7 +226,7 @@ for (const variant of ['default', 'turbo']) {
expect(await session.hasRedbox(true)).toBe(true)

const source = await session.getRedboxSource()
expect(source).toMatchSnapshot()
expect(next.normalizeTestDirContent(source)).toMatchSnapshot()

await cleanup()
})
Expand Down
Expand Up @@ -104,7 +104,7 @@ exports[`ReactRefreshLogBox app default unterminated JSX 1`] = `
"./index.js
Error:
x Unexpected token. Did you mean \`{'}'}\` or \`}\`?
,-[5:1]
,-[TEST_DIR/index.js:5:1]
5 | <p>lol</p>
6 | div
7 | )
Expand All @@ -114,7 +114,7 @@ Error:
\`----
x Unexpected eof
,-[6:1]
,-[TEST_DIR/index.js:6:1]
6 | div
7 | )
8 | }
Expand Down
15 changes: 9 additions & 6 deletions test/development/acceptance-app/rsc-build-errors.test.ts
Expand Up @@ -272,13 +272,14 @@ createNextDescribe(

expect(await session.hasRedbox(true)).toBe(true)
await check(() => session.getRedboxSource(), /must be a Client Component/)
expect(await session.getRedboxSource()).toMatchInlineSnapshot(`
expect(next.normalizeTestDirContent(await session.getRedboxSource()))
.toMatchInlineSnapshot(`
"./app/server-with-errors/error-file/error.js
ReactServerComponentsError:
./app/server-with-errors/error-file/error.js must be a Client Component. Add the \\"use client\\" directive the top of the file to resolve this issue.
,----
,-[TEST_DIR/app/server-with-errors/error-file/error.js:1:1]
1 | export default function Error() {}
: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
\`----
Expand All @@ -302,13 +303,14 @@ createNextDescribe(

expect(await session.hasRedbox(true)).toBe(true)
await check(() => session.getRedboxSource(), /must be a Client Component/)
expect(await session.getRedboxSource()).toMatchInlineSnapshot(`
expect(next.normalizeTestDirContent(await session.getRedboxSource()))
.toMatchInlineSnapshot(`
"./app/server-with-errors/error-file/error.js
ReactServerComponentsError:
./app/server-with-errors/error-file/error.js must be a Client Component. Add the \\"use client\\" directive the top of the file to resolve this issue.
,----
,-[TEST_DIR/app/server-with-errors/error-file/error.js:1:1]
1 |
: ^
\`----
Expand Down Expand Up @@ -386,13 +388,14 @@ createNextDescribe(
)

expect(await session.hasRedbox(true)).toBe(true)
expect(await session.getRedboxSource()).toMatchInlineSnapshot(`
expect(next.normalizeTestDirContent(await session.getRedboxSource()))
.toMatchInlineSnapshot(`
"./app/Component.js
ReactServerComponentsError:
You're importing a component that needs useState. It only works in a Client Component but none of its parents are marked with \\"use client\\", so they're Server Components by default.
,----
,-[TEST_DIR/node_modules/client-package/module2.js:1:1]
1 | import { useState } from 'react'
: ^^^^^^^^
\`----
Expand Down
Expand Up @@ -109,7 +109,9 @@ for (const variant of ['default', 'turbo']) {
])
)
expect(await session.hasRedbox(true)).toBe(true)
expect(await session.getRedboxSource()).toMatchSnapshot()
expect(
next.normalizeTestDirContent(await session.getRedboxSource())
).toMatchSnapshot()

await session.patch(
'pages/_app.js',
Expand Down Expand Up @@ -158,7 +160,9 @@ for (const variant of ['default', 'turbo']) {
])
)
expect(await session.hasRedbox(true)).toBe(true)
expect(await session.getRedboxSource()).toMatchSnapshot()
expect(
next.normalizeTestDirContent(await session.getRedboxSource())
).toMatchSnapshot()

await session.patch(
'pages/_document.js',
Expand Down
2 changes: 1 addition & 1 deletion test/development/acceptance/ReactRefreshLogBox.test.ts
Expand Up @@ -215,7 +215,7 @@ for (const variant of ['default', 'turbo']) {
expect(await session.hasRedbox(true)).toBe(true)

const source = await session.getRedboxSource()
expect(source).toMatchSnapshot()
expect(next.normalizeTestDirContent(source)).toMatchSnapshot()

await cleanup()
})
Expand Down
Expand Up @@ -4,7 +4,7 @@ exports[`ReactRefreshLogBox default _app syntax error shows logbox 1`] = `
"./pages/_app.js
Error:
x Expression expected
,-[1:1]
,-[TEST_DIR/pages/_app.js:1:1]
1 |
2 | function MyApp({ Component, pageProps }) {
3 | return <<Component {...pageProps} />;
Expand All @@ -15,7 +15,7 @@ Error:
\`----
x Expression expected
,-[1:1]
,-[TEST_DIR/pages/_app.js:1:1]
1 |
2 | function MyApp({ Component, pageProps }) {
3 | return <<Component {...pageProps} />;
Expand All @@ -33,7 +33,7 @@ exports[`ReactRefreshLogBox default _document syntax error shows logbox 1`] = `
"./pages/_document.js
Error:
x Unexpected token \`{\`. Expected identifier, string literal, numeric literal or [ for the computed key
,-[1:1]
,-[TEST_DIR/pages/_document.js:1:1]
1 |
2 | import Document, { Html, Head, Main, NextScript } from 'next/document'
3 |
Expand Down
Expand Up @@ -75,7 +75,7 @@ exports[`ReactRefreshLogBox default unterminated JSX 1`] = `
"./index.js
Error:
x Unexpected token. Did you mean \`{'}'}\` or \`&rbrace;\`?
,-[5:1]
,-[TEST_DIR/index.js:5:1]
5 | <p>lol</p>
6 | div
7 | )
Expand All @@ -85,7 +85,7 @@ Error:
\`----
x Unexpected eof
,-[6:1]
,-[TEST_DIR/index.js:6:1]
6 | div
7 | )
8 | }
Expand Down
Expand Up @@ -16,7 +16,7 @@ exports[`ReactRefreshLogBox default syntax > runtime error 2`] = `
"./index.js
Error:
x Expected '}', got '<eof>'
,-[5:1]
,-[TEST_DIR/index.js:5:1]
5 | i++
6 | throw Error('no ' + i)
7 | }, 1000)
Expand All @@ -35,7 +35,7 @@ exports[`ReactRefreshLogBox default syntax > runtime error 3`] = `
"./index.js
Error:
x Expected '}', got '<eof>'
,-[5:1]
,-[TEST_DIR/index.js:5:1]
5 | i++
6 | throw Error('no ' + i)
7 | }, 1000)
Expand Down
8 changes: 6 additions & 2 deletions test/development/acceptance/error-recovery.test.ts
Expand Up @@ -426,12 +426,16 @@ for (const variant of ['default', 'turbo']) {

await new Promise((resolve) => setTimeout(resolve, 1000))
expect(await session.hasRedbox(true)).toBe(true)
expect(await session.getRedboxSource()).toMatchSnapshot()
expect(
next.normalizeTestDirContent(await session.getRedboxSource())
).toMatchSnapshot()

// Test that runtime error does not take over:
await new Promise((resolve) => setTimeout(resolve, 2000))
expect(await session.hasRedbox(true)).toBe(true)
expect(await session.getRedboxSource()).toMatchSnapshot()
expect(
next.normalizeTestDirContent(await session.getRedboxSource())
).toMatchSnapshot()

await cleanup()
})
Expand Down
Expand Up @@ -48,13 +48,14 @@ createNextDescribe(
() => session.getRedboxSource(),
/That only works in a Server Component/
)
expect(await session.getRedboxSource()).toMatchInlineSnapshot(`
expect(next.normalizeTestDirContent(await session.getRedboxSource()))
.toMatchInlineSnapshot(`
"./components/Comp.js
ReactServerComponentsError:
You're importing a component that needs next/headers. That only works in a Server Component which is not supported in the pages/ directory. Read more: https://beta.nextjs.org/docs/rendering/server-and-client-components
,-[1:1]
,-[TEST_DIR/components/Comp.js:1:1]
1 |
2 | import { cookies } from 'next/headers'
: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down Expand Up @@ -90,13 +91,14 @@ createNextDescribe(
() => session.getRedboxSource(),
/That only works in a Server Component/
)
expect(await session.getRedboxSource()).toMatchInlineSnapshot(`
expect(next.normalizeTestDirContent(await session.getRedboxSource()))
.toMatchInlineSnapshot(`
"./components/Comp.js
ReactServerComponentsError:
You're importing a component that needs server-only. That only works in a Server Component which is not supported in the pages/ directory. Read more: https://beta.nextjs.org/docs/rendering/server-and-client-components
,-[1:1]
,-[TEST_DIR/components/Comp.js:1:1]
1 |
2 | import 'server-only'
: ^^^^^^^^^^^^^^^^^^^^
Expand Down
10 changes: 10 additions & 0 deletions test/lib/next-modes/base.ts
Expand Up @@ -11,6 +11,7 @@ import webdriver from '../next-webdriver'
import { renderViaHTTP, fetchViaHTTP } from 'next-test-utils'
import cheerio from 'cheerio'
import { BrowserInterface } from '../browsers/base'
import escapeStringRegexp from 'escape-string-regexp'

type Event = 'stdout' | 'stderr' | 'error' | 'destroy'
export type InstallCommand =
Expand Down Expand Up @@ -280,6 +281,15 @@ export class NextInstance {
})
}

// normalize snapshots or stack traces being tested
// to a consistent test dir value since it's random
public normalizeTestDirContent(content) {
return content.replace(
new RegExp(escapeStringRegexp(this.testDir), 'g'),
'TEST_DIR'
)
}

public async clean() {
if (this.childProcess) {
throw new Error(`stop() must be called before cleaning`)
Expand Down

0 comments on commit d84982d

Please sign in to comment.