Skip to content

Commit 92a32ba

Browse files
committed
fix(tests): use mockResolvedValueOnce to prevent mock isolation issues
Fixed test failures in releases-github.test.mts caused by mock leakage between test suites when running with isolate: false in Vitest. Root cause: - Tests used mockResolvedValue() in beforeEach hooks - With isolate: false, mocks persisted across test suites in shared worker - Mock data from downloadReleaseAsset leaked into getReleaseAssetUrl tests - When mocks weren't set up, real httpRequest triggered retry logic (15s) exceeding the 10s test timeout Solution: - Replaced mockResolvedValue() with mockResolvedValueOnce() in each test - Each test now gets isolated mock implementation - Removed beforeEach mock setup, keeping only afterEach cleanup - Tests can now run concurrently without interference Tests fixed: - "should get asset URL with exact name" (was timing out after 10s) - "should get asset URL with wildcard pattern" (was getting wrong mock data) All 6,248 tests now pass including 37 in releases-github.test.mts.
1 parent 221e49c commit 92a32ba

File tree

1 file changed

+79
-17
lines changed

1 file changed

+79
-17
lines changed

test/unit/releases-github.test.mts

Lines changed: 79 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -564,21 +564,19 @@ describe('releases/github', () => {
564564
tag_name: 'v1.0.0',
565565
}
566566

567-
beforeEach(() => {
568-
vi.mocked(httpRequest).mockResolvedValue(
567+
afterEach(() => {
568+
vi.clearAllMocks()
569+
})
570+
571+
it('should get asset URL with exact name', async () => {
572+
vi.mocked(httpRequest).mockResolvedValueOnce(
569573
createMockHttpResponse(
570574
Buffer.from(JSON.stringify(mockRelease)),
571575
true,
572576
200,
573577
),
574578
)
575-
})
576579

577-
afterEach(() => {
578-
vi.clearAllMocks()
579-
})
580-
581-
it('should get asset URL with exact name', async () => {
582580
const url = await getReleaseAssetUrl(
583581
'v1.0.0',
584582
'yoga-sync-20260107-abc123.mjs',
@@ -591,6 +589,14 @@ describe('releases/github', () => {
591589
})
592590

593591
it('should get asset URL with wildcard pattern', async () => {
592+
vi.mocked(httpRequest).mockResolvedValueOnce(
593+
createMockHttpResponse(
594+
Buffer.from(JSON.stringify(mockRelease)),
595+
true,
596+
200,
597+
),
598+
)
599+
594600
const url = await getReleaseAssetUrl(
595601
'v1.0.0',
596602
'yoga-sync-*.mjs',
@@ -603,6 +609,14 @@ describe('releases/github', () => {
603609
})
604610

605611
it('should get asset URL with brace expansion', async () => {
612+
vi.mocked(httpRequest).mockResolvedValueOnce(
613+
createMockHttpResponse(
614+
Buffer.from(JSON.stringify(mockRelease)),
615+
true,
616+
200,
617+
),
618+
)
619+
606620
const url = await getReleaseAssetUrl(
607621
'v1.0.0',
608622
'yoga-{sync,layout}-*.mjs',
@@ -615,6 +629,14 @@ describe('releases/github', () => {
615629
})
616630

617631
it('should get asset URL with RegExp pattern', async () => {
632+
vi.mocked(httpRequest).mockResolvedValueOnce(
633+
createMockHttpResponse(
634+
Buffer.from(JSON.stringify(mockRelease)),
635+
true,
636+
200,
637+
),
638+
)
639+
618640
const url = await getReleaseAssetUrl(
619641
'v1.0.0',
620642
/^models-.+\.tar\.gz$/,
@@ -627,6 +649,14 @@ describe('releases/github', () => {
627649
})
628650

629651
it('should get asset URL with prefix/suffix object pattern', async () => {
652+
vi.mocked(httpRequest).mockResolvedValueOnce(
653+
createMockHttpResponse(
654+
Buffer.from(JSON.stringify(mockRelease)),
655+
true,
656+
200,
657+
),
658+
)
659+
630660
const url = await getReleaseAssetUrl(
631661
'v1.0.0',
632662
{ prefix: 'models-', suffix: '.tar.gz' },
@@ -639,6 +669,14 @@ describe('releases/github', () => {
639669
})
640670

641671
it('should throw error when pattern does not match any asset', async () => {
672+
vi.mocked(httpRequest).mockResolvedValueOnce(
673+
createMockHttpResponse(
674+
Buffer.from(JSON.stringify(mockRelease)),
675+
true,
676+
200,
677+
),
678+
)
679+
642680
await expect(
643681
getReleaseAssetUrl('v1.0.0', 'nonexistent-*.xyz', SOCKET_BTM_REPO, {
644682
quiet: true,
@@ -664,22 +702,20 @@ describe('releases/github', () => {
664702
tag_name: 'v1.0.0',
665703
}
666704

667-
beforeEach(() => {
668-
vi.mocked(httpRequest).mockResolvedValue(
705+
afterEach(() => {
706+
vi.clearAllMocks()
707+
})
708+
709+
it('should download asset with exact name', async () => {
710+
vi.mocked(httpRequest).mockResolvedValueOnce(
669711
createMockHttpResponse(
670712
Buffer.from(JSON.stringify(mockRelease)),
671713
true,
672714
200,
673715
),
674716
)
675-
vi.mocked(httpDownload).mockResolvedValue(undefined)
676-
})
677-
678-
afterEach(() => {
679-
vi.clearAllMocks()
680-
})
717+
vi.mocked(httpDownload).mockResolvedValueOnce(undefined)
681718

682-
it('should download asset with exact name', async () => {
683719
await downloadReleaseAsset(
684720
'v1.0.0',
685721
'yoga-sync-abc.mjs',
@@ -700,6 +736,15 @@ describe('releases/github', () => {
700736
})
701737

702738
it('should download asset with wildcard pattern', async () => {
739+
vi.mocked(httpRequest).mockResolvedValueOnce(
740+
createMockHttpResponse(
741+
Buffer.from(JSON.stringify(mockRelease)),
742+
true,
743+
200,
744+
),
745+
)
746+
vi.mocked(httpDownload).mockResolvedValueOnce(undefined)
747+
703748
await downloadReleaseAsset(
704749
'v1.0.0',
705750
'yoga-*.mjs',
@@ -716,6 +761,15 @@ describe('releases/github', () => {
716761
})
717762

718763
it('should download asset with brace expansion', async () => {
764+
vi.mocked(httpRequest).mockResolvedValueOnce(
765+
createMockHttpResponse(
766+
Buffer.from(JSON.stringify(mockRelease)),
767+
true,
768+
200,
769+
),
770+
)
771+
vi.mocked(httpDownload).mockResolvedValueOnce(undefined)
772+
719773
await downloadReleaseAsset(
720774
'v1.0.0',
721775
'{yoga,models}-*.{mjs,tar.gz}',
@@ -732,6 +786,14 @@ describe('releases/github', () => {
732786
})
733787

734788
it('should throw error when pattern does not match', async () => {
789+
vi.mocked(httpRequest).mockResolvedValueOnce(
790+
createMockHttpResponse(
791+
Buffer.from(JSON.stringify(mockRelease)),
792+
true,
793+
200,
794+
),
795+
)
796+
735797
await expect(
736798
downloadReleaseAsset(
737799
'v1.0.0',

0 commit comments

Comments
 (0)