Skip to content

Commit e65c670

Browse files
committed
fix: Run example tests from their own directories with local Jest configs
Example tests now execute from their respective example directories using their own jest.config.js files instead of the root configuration. This resolves path resolution issues and mock conflicts that occurred when running example tests from the repository root.
1 parent a41fe06 commit e65c670

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

run-tests.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const path = require('path')
44
const _glob = require('glob')
5+
const fs = require('fs')
56
const { existsSync } = require('fs')
67
const fsp = require('fs/promises')
78
const nodeFetch = require('node-fetch')
@@ -546,8 +547,37 @@ ${ENDGROUP}`)
546547
return resolve(new Date().getTime() - start)
547548
}
548549

549-
const child = spawn(jestPath, args, {
550+
// For example tests, run Jest from the example directory with its own config
551+
const isExampleTest = test.file.startsWith('examples/')
552+
let cwd = process.cwd()
553+
let actualJestPath = jestPath
554+
let actualArgs = args
555+
556+
if (isExampleTest) {
557+
// Extract example directory (e.g., 'examples/with-jest' from 'examples/with-jest/__tests__/index.test.tsx')
558+
const exampleDir = test.file.split('/').slice(0, 2).join('/')
559+
cwd = path.join(process.cwd(), exampleDir)
560+
561+
// Use relative path from example directory
562+
const relativePath = test.file.replace(exampleDir + '/', '')
563+
actualArgs = [...args]
564+
actualArgs[actualArgs.indexOf(test.file)] = relativePath
565+
566+
// Try to use the example's own Jest if it exists, otherwise use root Jest
567+
const localJestPath = path.join(
568+
cwd,
569+
'node_modules',
570+
'.bin',
571+
`jest${process.platform === 'win32' ? '.CMD' : ''}`
572+
)
573+
if (fs.existsSync(localJestPath)) {
574+
actualJestPath = localJestPath
575+
}
576+
}
577+
578+
const child = spawn(actualJestPath, actualArgs, {
550579
stdio: ['ignore', 'pipe', 'pipe'],
580+
cwd,
551581
env: {
552582
...process.env,
553583
...env,

0 commit comments

Comments
 (0)