Skip to content

Commit fa80b35

Browse files
committed
fix(tests): resolve Vitest worker timeout and improve import patterns
Fixed multiple issues in coverage test file that caused CI failures: 1. Worker timeout: HTTP server wasn't being properly closed in afterAll hook, causing Vitest workers to hang. Changed to async/await pattern to ensure complete server shutdown before test exit. 2. Cross-platform compatibility: Replaced hardcoded '/tmp' paths with os.tmpdir() to work on Windows, macOS, and Linux. 3. Import optimization: Replaced all dynamic await import() statements with static imports at top of file for better performance and clarity. Used default imports for os and path modules following project standards.
1 parent e459caa commit fa80b35

File tree

1 file changed

+33
-31
lines changed

1 file changed

+33
-31
lines changed

test/unit/socket-sdk-api-methods.coverage.test.mts

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,17 @@
66
* using actual HTTP, not module patching.
77
*/
88

9+
import {
10+
existsSync,
11+
mkdtempSync,
12+
readFileSync,
13+
rmSync,
14+
unlinkSync,
15+
writeFileSync,
16+
} from 'node:fs'
917
import { createServer } from 'node:http'
18+
import os from 'node:os'
19+
import path from 'node:path'
1020

1121
import { afterAll, beforeAll, describe, expect, it } from 'vitest'
1222

@@ -415,8 +425,16 @@ describe('SocketSdk - API Methods Coverage', () => {
415425
})
416426
})
417427

418-
afterAll(() => {
419-
server.close()
428+
afterAll(async () => {
429+
await new Promise<void>((resolve, reject) => {
430+
server.close(err => {
431+
if (err) {
432+
reject(err)
433+
} else {
434+
resolve()
435+
}
436+
})
437+
})
420438
})
421439

422440
describe('Package Analysis Methods', () => {
@@ -507,12 +525,8 @@ describe('SocketSdk - API Methods Coverage', () => {
507525

508526
it('covers createFullScan', async () => {
509527
// Create a temporary test file
510-
const { mkdtempSync, rmSync, writeFileSync } = await import('node:fs')
511-
const { tmpdir } = await import('node:os')
512-
const { join } = await import('node:path')
513-
514-
const tempDir = mkdtempSync(join(tmpdir(), 'socket-test-'))
515-
const testFile = join(tempDir, 'package.json')
528+
const tempDir = mkdtempSync(path.join(os.tmpdir(), 'socket-test-'))
529+
const testFile = path.join(tempDir, 'package.json')
516530
writeFileSync(
517531
testFile,
518532
JSON.stringify({ name: 'test-pkg', version: '1.0.0' }),
@@ -613,12 +627,8 @@ describe('SocketSdk - API Methods Coverage', () => {
613627

614628
it('covers createDependenciesSnapshot', async () => {
615629
// Create a temporary test file
616-
const { mkdtempSync, rmSync, writeFileSync } = await import('node:fs')
617-
const { tmpdir } = await import('node:os')
618-
const { join } = await import('node:path')
619-
620-
const tempDir = mkdtempSync(join(tmpdir(), 'socket-test-'))
621-
const testFile = join(tempDir, 'package.json')
630+
const tempDir = mkdtempSync(path.join(os.tmpdir(), 'socket-test-'))
631+
const testFile = path.join(tempDir, 'package.json')
622632
writeFileSync(
623633
testFile,
624634
JSON.stringify({ name: 'test-pkg', version: '1.0.0' }),
@@ -808,10 +818,8 @@ describe('SocketSdk - API Methods Coverage', () => {
808818
describe('Full Scan Archive Methods', () => {
809819
it('covers createOrgFullScanFromArchive', async () => {
810820
// Create a temporary test file to upload
811-
const tmpDir = '/tmp'
812-
const testFilePath = `${tmpDir}/test-archive-${Date.now()}.tar.gz`
813-
const fs = await import('node:fs')
814-
fs.writeFileSync(testFilePath, 'test archive content')
821+
const testFilePath = `${os.tmpdir()}/test-archive-${Date.now()}.tar.gz`
822+
writeFileSync(testFilePath, 'test archive content')
815823

816824
try {
817825
const result = await client.createOrgFullScanFromArchive(
@@ -830,7 +838,7 @@ describe('SocketSdk - API Methods Coverage', () => {
830838
} finally {
831839
// Clean up test file
832840
try {
833-
fs.unlinkSync(testFilePath)
841+
unlinkSync(testFilePath)
834842
} catch {
835843
// Ignore cleanup errors
836844
}
@@ -839,9 +847,7 @@ describe('SocketSdk - API Methods Coverage', () => {
839847

840848
it('covers downloadOrgFullScanFilesAsTar', async () => {
841849
// Create a temporary output file path
842-
const tmpDir = '/tmp'
843-
const outputPath = `${tmpDir}/test-download-${Date.now()}.tar`
844-
const fs = await import('node:fs')
850+
const outputPath = `${os.tmpdir()}/test-download-${Date.now()}.tar`
845851

846852
try {
847853
const result = await client.downloadOrgFullScanFilesAsTar(
@@ -852,14 +858,14 @@ describe('SocketSdk - API Methods Coverage', () => {
852858
// The method executes successfully
853859
expect(result.success).toBe(true)
854860
// Verify the file was written
855-
expect(fs.existsSync(outputPath)).toBe(true)
861+
expect(existsSync(outputPath)).toBe(true)
856862
// Verify file has content
857-
const content = fs.readFileSync(outputPath)
863+
const content = readFileSync(outputPath)
858864
expect(content.length).toBeGreaterThan(0)
859865
} finally {
860866
// Clean up output file
861867
try {
862-
fs.unlinkSync(outputPath)
868+
unlinkSync(outputPath)
863869
} catch {
864870
// Ignore cleanup errors
865871
}
@@ -1129,12 +1135,8 @@ describe('SocketSdk - API Methods Coverage', () => {
11291135
describe('Upload Methods', () => {
11301136
it('covers uploadManifestFiles', async () => {
11311137
// Create a temporary test file
1132-
const { mkdtempSync, rmSync, writeFileSync } = await import('node:fs')
1133-
const { tmpdir } = await import('node:os')
1134-
const { join } = await import('node:path')
1135-
1136-
const tempDir = mkdtempSync(join(tmpdir(), 'socket-test-'))
1137-
const testFile = join(tempDir, 'package.json')
1138+
const tempDir = mkdtempSync(path.join(os.tmpdir(), 'socket-test-'))
1139+
const testFile = path.join(tempDir, 'package.json')
11381140
writeFileSync(
11391141
testFile,
11401142
JSON.stringify({ name: 'test-pkg', version: '1.0.0' }),

0 commit comments

Comments
 (0)