Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
"mongo-logs:find": "bun script/deploy/query-deployment-logs.ts find",
"mongo-logs:exists": "bun script/deploy/query-deployment-logs.ts exists",
"mongo-logs:get": "bun script/deploy/query-deployment-logs.ts get",
"okr:contract-coverage-above-90": "bun run script/utils/analyzeCoverage.ts",
"okr:contract-coverage-above-90": "bunx tsx script/utils/analyzeCoverage.ts",
"preinstall": "bash preinstall.sh",
"prepare": "bun prepare:husky",
"prepare:husky": "husky install",
Expand Down
76 changes: 76 additions & 0 deletions script/runTypescriptTests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/usr/bin/env bun
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Execute this script with bunx tsx per repo convention.

Replace the Bun shebang and usage notes to match our standard.

-#!/usr/bin/env bun
+#!/usr/bin/env -S bunx tsx
@@
- * - npm run test:ts (runs all TypeScript tests)
- * - bun script/runTypescriptTests.ts (direct execution)
+ * - npm run test:ts (runs all TypeScript tests)
+ * - bunx tsx script/runTypescriptTests.ts (direct execution)

Note: If env -S is unavailable in some environments, drop the shebang and rely on package.json’s script.

Also applies to: 16-18

🤖 Prompt for AI Agents
In script/runTypescriptTests.ts around lines 1 and 16-18, replace the current
Bun shebang and accompanying usage notes with the repo-standard invocation using
bunx tsx: either change the shebang/launcher to use the env wrapper that runs
"bunx tsx" per convention or, if env -S is not supported in target environments,
remove the shebang entirely and update the repository scripts (package.json) and
the file's usage notes to instruct running via "bunx tsx
script/runTypescriptTests.ts"; ensure the usage text clearly reflects the chosen
approach.


/**
* TypeScript Test Runner Script
*
* This script finds and runs all TypeScript test files in the script/ directory
* while excluding external library tests from lib/ and node_modules/ directories.
*
* Features:
* - Automatically discovers all .test.ts files in script/ directory
* - Excludes lib/ and node_modules/ directories to avoid running external tests
* - Uses Bun's built-in test runner with Jest-like syntax
* - Provides clear output showing which test files were found and executed
*
* Usage:
* - npm run test:ts (runs all TypeScript tests)
* - bun script/runTypescriptTests.ts (direct execution)
*
* Output:
* - Lists found test files
* - Runs each test file with Bun's test runner
* - Shows test results and summary
*/

import { execSync } from 'child_process'
import { readdirSync, statSync } from 'fs'
import { join } from 'path'
Comment on lines +25 to +27
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Avoid shell quoting pitfalls; use spawnSync with args instead of execSync with a string.

Prevents issues if paths contain spaces/quotes and removes reliance on a shell.

-import { execSync } from 'child_process'
+import { spawnSync } from 'child_process'
@@
-// Run all test files together with a specific pattern
-const testFilePatterns = testFiles.map((file) => `"${file}"`).join(' ')
-console.log(`Running all tests with: bun test ${testFilePatterns}`)
-
-try {
-  execSync(`bun test ${testFilePatterns}`, { stdio: 'inherit' })
-} catch (error) {
-  console.error('Some tests failed')
-  process.exit(1)
-}
+console.log('Running all tests with: bun test', testFiles.join(' '))
+const { status } = spawnSync('bun', ['test', ...testFiles], { stdio: 'inherit' })
+if (status !== 0) {
+  console.error('Some tests failed')
+  process.exit(status ?? 1)
+}

Also applies to: 65-74


function findTestFiles(dir: string): string[] {
const testFiles: string[] = []

try {
const items = readdirSync(dir)

for (const item of items) {
const fullPath = join(dir, item)
const stat = statSync(fullPath)

if (stat.isDirectory()) {
// Skip lib/ and node_modules/ directories
if (item === 'lib' || item === 'node_modules') continue

testFiles.push(...findTestFiles(fullPath))
} else if (item.endsWith('.test.ts')) testFiles.push(fullPath)
}
} catch (error) {
// Ignore errors for directories we can't access
}

return testFiles
}

const scriptDir = join(process.cwd(), 'script')
const testFiles = findTestFiles(scriptDir)

if (testFiles.length === 0) {
console.log('No TypeScript test files found in script/ directory')
process.exit(0)
}

console.log(`Found ${testFiles.length} test file(s):`)
testFiles.forEach((file) => console.log(` - ${file}`))
console.log()

// Run all test files together with a specific pattern
const testFilePatterns = testFiles.map((file) => `"${file}"`).join(' ')
console.log(`Running all tests with: bun test ${testFilePatterns}`)

try {
execSync(`bun test ${testFilePatterns}`, { stdio: 'inherit' })
} catch (error) {
console.error('Some tests failed')
process.exit(1)
}

console.log('\n✅ All TypeScript tests completed successfully!')
Loading