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

ci(workflow): enable test trace upload #51107

Merged
merged 3 commits into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ env:
# canary next-swc binaries in the monorepo
NEXT_SKIP_NATIVE_POSTINSTALL: 1
DATADOG_API_KEY: ${{ secrets.DATA_DOG_API_KEY }}
DATADOG_TRACE_NEXTJS_TEST: 'true'
DD_ENV: 'ci'
TEST_TIMINGS_TOKEN: ${{ secrets.TEST_TIMINGS_TOKEN }}
NEXT_TEST_JOB: 1
Expand Down Expand Up @@ -181,6 +182,27 @@ jobs:
afterBuild: pnpm playwright install && BROWSER_NAME=firefox node run-tests.js test/integration/production/test/index.test.js && BROWSER_NAME=safari NEXT_TEST_MODE=start node run-tests.js -c 1 test/integration/production/test/index.test.js test/e2e/basepath.test.ts && BROWSER_NAME=safari DEVICE_NAME='iPhone XR' node run-tests.js -c 1 test/production/prerender-prefetch/index.test.ts
secrets: inherit

report-test-results:
needs: ['test-dev', 'test-prod', 'test-integration', 'test-turbopack-dev']
runs-on: [self-hosted, linux, x64]
name: upload test trace to datadog
# We'll not block the CI if the upload fails
continue-on-error: true
steps:
- uses: actions/download-artifact@v3
with:
name: Test trace reports
path: test
- name: Upload traces
run: |
ls -al ./test
npm install -g junit-report-merger@6.0.2 @datadog/datadog-ci@2.14.0
jrm ./nextjs-test-result-junit.xml "test/test-junit-report/**/*.xml"
jrm ./turbopack-test-result-junit.xml "test/turbopack-test-junit-report/**/*.xml"
# Put a separate tag for the tests with turbopack to distinguish between same test names
DD_ENV=ci datadog-ci junit upload --tags test.type:nextjs --service nextjs ./nextjs-test-result-junit.xml
DD_ENV=ci datadog-ci junit upload --tags test.type:turbopack --service nextjs ./turbopack-test-result-junit.xml

tests-pass:
needs:
[
Expand Down
11 changes: 11 additions & 0 deletions .github/workflows/build_reusable.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ env:
# canary next-swc binaries in the monorepo
NEXT_SKIP_NATIVE_POSTINSTALL: 1
DATADOG_API_KEY: ${{ secrets.DATA_DOG_API_KEY }}
DATADOG_TRACE_NEXTJS_TEST: 'true'
DD_ENV: 'ci'
TEST_TIMINGS_TOKEN: ${{ secrets.TEST_TIMINGS_TOKEN }}
NEXT_TEST_JOB: 1
Expand Down Expand Up @@ -140,3 +141,13 @@ jobs:
with:
name: turbo run summary
path: .turbo/runs

- name: Upload test reports artifact
uses: actions/upload-artifact@v3
if: ${{ inputs.afterBuild }}
with:
name: Test trace reports
path: |
test/test-junit-report
test/turbopack-test-junit-report
if-no-files-found: ignore
28 changes: 28 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,33 @@ const customJestConfig = {
},
}

// Check if the environment variable is set to enable test trace,
// Insert a reporter to generate a junit report to upload.
// This won't count for the retry to avoid duplicated test being reported twice
// - which means our test trace will report test results for the flaky test as failed without retry.
const shouldEnableTestTrace =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to track these on PRs or should these only be recorded on the main branch?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turbopack currently does collect on all branch, and it's easy to filter out in datadog. If we feel it's too much we can reduce it later but it was helpful to track PR's pipeline time to time for some cases.

process.env.DATADOG_API_KEY &&
process.env.DATADOG_TRACE_NEXTJS_TEST &&
!process.env.IS_RETRY

if (shouldEnableTestTrace) {
if (!customJestConfig.reporters) {
customJestConfig.reporters = ['default']
}

const outputDirectory = process.env.__INTERNAL_NEXT_DEV_TEST_TURBO_DEV
? '<rootDir>/turbopack-test-junit-report'
: '<rootDir>/test-junit-report'
customJestConfig.reporters.push([
'jest-junit',
{
outputDirectory,
reportTestSuiteErrors: 'true',
uniqueOutputName: 'true',
outputName: 'nextjs-test-junit',
},
])
}

// createJestConfig is exported in this way to ensure that next/jest can load the Next.js config which is async
module.exports = createJestConfig(customJestConfig)
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@
"isomorphic-unfetch": "3.0.0",
"jest": "27.0.6",
"jest-extended": "1.2.1",
"jest-junit": "16.0.0",
"json5": "2.2.3",
"ky": "0.19.1",
"ky-universal": "0.6.0",
Expand Down
16 changes: 16 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 1 addition & 28 deletions run-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,33 +322,6 @@ async function main() {

const shouldRecordTestWithReplay = process.env.RECORD_REPLAY && isRetry

let shouldEnableTestTrace = false
let traceInitPath = null

try {
traceInitPath = require.resolve('dd-trace/ci/init')
// Enable trace only if tracer (dd-trace) can be resolved
shouldEnableTestTrace =
process.env.DATADOG_API_KEY && process.env.DATADOG_TRACE_NEXTJS_TEST
} catch (e) {
shouldEnableTestTrace = false
}

const traceEnv =
shouldEnableTestTrace && traceInitPath
? {
DD_API_KEY: process.env.DATADOG_API_KEY,
DD_CIVISIBILITY_AGENTLESS_ENABLED: 1,
DD_ENV: 'ci',
DD_SERVICE: 'nextjs',
NODE_OPTIONS: `-r ${traceInitPath}`,
}
: {}

if (shouldEnableTestTrace) {
console.log(`Running test with Datadog tracing enabled`)
}

const child = spawn(
jestPath,
[
Expand All @@ -368,7 +341,7 @@ async function main() {
stdio: ['ignore', 'pipe', 'pipe'],
env: {
...process.env,
...traceEnv,
IS_RETRY: isRetry ? 'true' : undefined,
RECORD_REPLAY: shouldRecordTestWithReplay,
// run tests in headless mode by default
HEADLESS: 'true',
Expand Down
2 changes: 2 additions & 0 deletions test/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
.vscode

e2e/**/tsconfig.json
test-junit-report/
turbopack-test-junit-report/