From e248a51092503cc4a715c06ee53a113f470ebd34 Mon Sep 17 00:00:00 2001 From: 0age <37939117+0age@users.noreply.github.com> Date: Wed, 4 Dec 2024 17:59:02 -0800 Subject: [PATCH] smoke test port --- jest.config.js | 12 ++++++++++++ package.json | 3 ++- scripts/run-sharded-tests.js | 30 ++++++++++++++++++++++++++++++ scripts/smoke-test.js | 17 ++++++++++------- 4 files changed, 54 insertions(+), 8 deletions(-) create mode 100644 jest.config.js create mode 100644 scripts/run-sharded-tests.js diff --git a/jest.config.js b/jest.config.js new file mode 100644 index 0000000..254e845 --- /dev/null +++ b/jest.config.js @@ -0,0 +1,12 @@ +/** @type {import('jest').Config} */ +export default { + transform: {}, + extensionsToTreatAsEsm: ['.ts'], + moduleNameMapper: { + '^(\\.{1,2}/.*)\\.js$': '$1', + }, + testMatch: ['**/src/__tests__/**/*.test.ts'], + testEnvironment: 'node', + // Support for sharding tests across multiple CI jobs + shard: process.env.JEST_SHARD, +}; diff --git a/package.json b/package.json index 3d70088..a883b42 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "test": "NODE_OPTIONS='--experimental-vm-modules --no-warnings' jest --detectOpenHandles", "test:related": "NODE_OPTIONS='--experimental-vm-modules --no-warnings' jest --bail --findRelatedTests src/__tests__/*.test.ts", "smoke-test": "node scripts/smoke-test.js", + "smoke-test:precommit": "SMOKE_TEST_PORT=4000 node scripts/smoke-test.js", "lint": "eslint .", "type-check": "tsc --noEmit -p tsconfig.lint.json", "type-check:staged": "tsc --noEmit --skipLibCheck --skipDefaultLibCheck --target ES2022 --module ESNext --moduleResolution bundler --strict --esModuleInterop --forceConsistentCasingInFileNames --baseUrl . --paths viem:['./src/types/viem.d.ts'] --paths webauthn-p256:['./src/types/webauthn-p256.d.ts'] src/types/fastify.d.ts", @@ -34,7 +35,7 @@ "eslint --max-warnings 0", "tsc --noEmit --skipLibCheck", "pnpm test:related", - "pnpm smoke-test" + "pnpm smoke-test:precommit" ], "*.{json,md}": [ "prettier --write", diff --git a/scripts/run-sharded-tests.js b/scripts/run-sharded-tests.js new file mode 100644 index 0000000..40130ad --- /dev/null +++ b/scripts/run-sharded-tests.js @@ -0,0 +1,30 @@ +import { spawnSync } from 'child_process'; + +// Get shard info from command line args +const shardArg = process.argv.find(arg => arg.startsWith('--shard=')); +if (!shardArg) { + console.error('Missing --shard argument'); + process.exit(1); +} + +const [currentShard, totalShards] = shardArg.split('=')[1].split('/').map(Number); +if (!currentShard || !totalShards || currentShard > totalShards) { + console.error('Invalid shard format. Use --shard=X/Y where X <= Y'); + process.exit(1); +} + +// Run Jest with sharding +const result = spawnSync('jest', [ + '--detectOpenHandles', + `--shard=${currentShard}/${totalShards}` +], { + stdio: 'inherit', + shell: true, + env: { + ...process.env, + NODE_OPTIONS: '--experimental-vm-modules --no-warnings', + JEST_SHARD: `${currentShard}/${totalShards}` + } +}); + +process.exit(result.status); diff --git a/scripts/smoke-test.js b/scripts/smoke-test.js index 4567a7b..bd7537c 100644 --- a/scripts/smoke-test.js +++ b/scripts/smoke-test.js @@ -75,18 +75,21 @@ async function killProcessOnPort(port) { } } +// Get port from environment variable or use default +const PORT = process.env.SMOKE_TEST_PORT || 3000; + async function main() { try { // Ensure port is free before starting - await killProcessOnPort(3000); + await killProcessOnPort(PORT); // Test development mode - console.log('Testing development mode (pnpm dev)...'); - await waitForServer('pnpm', ['dev'], 10000); + console.log(`Testing development mode (pnpm dev) on port ${PORT}...`); + await waitForServer('PORT=' + PORT + ' pnpm', ['dev'], 10000); console.log('āœ“ Development server started successfully'); // Kill the development server - await killProcessOnPort(3000); + await killProcessOnPort(PORT); // Test production mode console.log('\nTesting production mode (pnpm start)...'); @@ -100,7 +103,7 @@ async function main() { }); }); - await waitForServer('pnpm', ['start'], 10000); + await waitForServer('PORT=' + PORT + ' pnpm', ['start'], 10000); console.log('āœ“ Production server started successfully'); console.log('\nāœ… All smoke tests passed!'); @@ -109,8 +112,8 @@ async function main() { console.error('\nāŒ Smoke tests failed:', error.message); process.exit(1); } finally { - // Cleanup: ensure port 3000 is free - await killProcessOnPort(3000); + // Cleanup: ensure port is free + await killProcessOnPort(PORT); } }