Lazy-load export converters from jsDelivr#3389
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
| it("uses pinned jsDelivr ESM URLs", () => { | ||
| expect(getConverterCdnUrl("circuit-json-to-gerber")).toBe( | ||
| "https://cdn.jsdelivr.net/npm/circuit-json-to-gerber@0.0.52/+esm", | ||
| ) | ||
| expect(getConverterCdnUrl("circuit-json-to-bom-csv")).toBe( | ||
| "https://cdn.jsdelivr.net/npm/circuit-json-to-bom-csv@0.0.8/+esm", | ||
| ) | ||
| expect(getConverterCdnUrl("circuit-json-to-pnp-csv")).toBe( | ||
| "https://cdn.jsdelivr.net/npm/circuit-json-to-pnp-csv@0.0.7/+esm", | ||
| ) | ||
| expect(getConverterCdnUrl("circuit-json-to-kicad")).toBe( | ||
| "https://cdn.jsdelivr.net/npm/circuit-json-to-kicad@0.0.135/+esm", | ||
| ) | ||
| expect(getConverterCdnUrl("circuit-json-to-gltf")).toBe( | ||
| "https://cdn.jsdelivr.net/npm/circuit-json-to-gltf@0.0.62/+esm", | ||
| ) | ||
| expect(getConverterCdnUrl("circuit-json-to-step")).toBe( | ||
| "https://cdn.jsdelivr.net/npm/circuit-json-to-step@0.0.18/+esm", | ||
| ) | ||
| expect(getConverterCdnUrl("circuit-json-to-lbrn")).toBe( | ||
| "https://cdn.jsdelivr.net/npm/circuit-json-to-lbrn@0.0.74/+esm", | ||
| ) | ||
| }) | ||
|
|
||
| it("caches successful loads and clears failed loads for retry", async () => { | ||
| clearConverterModuleCache() | ||
|
|
||
| const importedSpecifiers: string[] = [] | ||
| const successfulImporter = async <TModule>(specifier: string) => { | ||
| importedSpecifiers.push(specifier) | ||
| return { | ||
| convertCircuitJsonToBomRows: () => [], | ||
| convertBomRowsToCsv: () => "", | ||
| } as TModule | ||
| } | ||
|
|
||
| const [firstLoad, secondLoad] = await Promise.all([ | ||
| loadConverterModule("circuit-json-to-bom-csv", successfulImporter), | ||
| loadConverterModule("circuit-json-to-bom-csv", successfulImporter), | ||
| ]) | ||
|
|
||
| expect(firstLoad).toBe(secondLoad) | ||
| expect(importedSpecifiers).toEqual([ | ||
| "https://cdn.jsdelivr.net/npm/circuit-json-to-bom-csv@0.0.8/+esm", | ||
| ]) | ||
|
|
||
| clearConverterModuleCache() | ||
|
|
||
| let attempts = 0 | ||
| const flakyImporter = async <TModule>() => { | ||
| attempts += 1 | ||
| if (attempts === 1) throw new Error("temporary CDN failure") | ||
| return { | ||
| convertCircuitJsonToBomRows: () => [], | ||
| convertBomRowsToCsv: () => "", | ||
| } as TModule | ||
| } | ||
|
|
||
| await expect( | ||
| loadConverterModule("circuit-json-to-bom-csv", flakyImporter), | ||
| ).rejects.toThrow("temporary CDN failure") | ||
|
|
||
| await expect( | ||
| loadConverterModule("circuit-json-to-bom-csv", flakyImporter), | ||
| ).resolves.toEqual({ | ||
| convertCircuitJsonToBomRows: expect.any(Function), | ||
| convertBomRowsToCsv: expect.any(Function), | ||
| }) | ||
| expect(attempts).toBe(2) | ||
|
|
||
| clearConverterModuleCache() | ||
| }) |
There was a problem hiding this comment.
This file contains two it(...) blocks (lines 9–31 and lines 33–80), which violates the rule that a *.test.ts file may have AT MOST one test(...) / it(...). The second test ('caches successful loads and clears failed loads for retry') must be moved to a separate, numbered file. For example, rename the current file to dynamic-converters1.test.ts (keeping the first it) and create dynamic-converters2.test.ts for the second it.
| it("uses pinned jsDelivr ESM URLs", () => { | |
| expect(getConverterCdnUrl("circuit-json-to-gerber")).toBe( | |
| "https://cdn.jsdelivr.net/npm/circuit-json-to-gerber@0.0.52/+esm", | |
| ) | |
| expect(getConverterCdnUrl("circuit-json-to-bom-csv")).toBe( | |
| "https://cdn.jsdelivr.net/npm/circuit-json-to-bom-csv@0.0.8/+esm", | |
| ) | |
| expect(getConverterCdnUrl("circuit-json-to-pnp-csv")).toBe( | |
| "https://cdn.jsdelivr.net/npm/circuit-json-to-pnp-csv@0.0.7/+esm", | |
| ) | |
| expect(getConverterCdnUrl("circuit-json-to-kicad")).toBe( | |
| "https://cdn.jsdelivr.net/npm/circuit-json-to-kicad@0.0.135/+esm", | |
| ) | |
| expect(getConverterCdnUrl("circuit-json-to-gltf")).toBe( | |
| "https://cdn.jsdelivr.net/npm/circuit-json-to-gltf@0.0.62/+esm", | |
| ) | |
| expect(getConverterCdnUrl("circuit-json-to-step")).toBe( | |
| "https://cdn.jsdelivr.net/npm/circuit-json-to-step@0.0.18/+esm", | |
| ) | |
| expect(getConverterCdnUrl("circuit-json-to-lbrn")).toBe( | |
| "https://cdn.jsdelivr.net/npm/circuit-json-to-lbrn@0.0.74/+esm", | |
| ) | |
| }) | |
| it("caches successful loads and clears failed loads for retry", async () => { | |
| clearConverterModuleCache() | |
| const importedSpecifiers: string[] = [] | |
| const successfulImporter = async <TModule>(specifier: string) => { | |
| importedSpecifiers.push(specifier) | |
| return { | |
| convertCircuitJsonToBomRows: () => [], | |
| convertBomRowsToCsv: () => "", | |
| } as TModule | |
| } | |
| const [firstLoad, secondLoad] = await Promise.all([ | |
| loadConverterModule("circuit-json-to-bom-csv", successfulImporter), | |
| loadConverterModule("circuit-json-to-bom-csv", successfulImporter), | |
| ]) | |
| expect(firstLoad).toBe(secondLoad) | |
| expect(importedSpecifiers).toEqual([ | |
| "https://cdn.jsdelivr.net/npm/circuit-json-to-bom-csv@0.0.8/+esm", | |
| ]) | |
| clearConverterModuleCache() | |
| let attempts = 0 | |
| const flakyImporter = async <TModule>() => { | |
| attempts += 1 | |
| if (attempts === 1) throw new Error("temporary CDN failure") | |
| return { | |
| convertCircuitJsonToBomRows: () => [], | |
| convertBomRowsToCsv: () => "", | |
| } as TModule | |
| } | |
| await expect( | |
| loadConverterModule("circuit-json-to-bom-csv", flakyImporter), | |
| ).rejects.toThrow("temporary CDN failure") | |
| await expect( | |
| loadConverterModule("circuit-json-to-bom-csv", flakyImporter), | |
| ).resolves.toEqual({ | |
| convertCircuitJsonToBomRows: expect.any(Function), | |
| convertBomRowsToCsv: expect.any(Function), | |
| }) | |
| expect(attempts).toBe(2) | |
| clearConverterModuleCache() | |
| }) | |
| it("uses pinned jsDelivr ESM URLs", () => { | |
| expect(getConverterCdnUrl("circuit-json-to-gerber")).toBe( | |
| "https://cdn.jsdelivr.net/npm/circuit-json-to-gerber@0.0.52/+esm", | |
| ) | |
| expect(getConverterCdnUrl("circuit-json-to-bom-csv")).toBe( | |
| "https://cdn.jsdelivr.net/npm/circuit-json-to-bom-csv@0.0.8/+esm", | |
| ) | |
| expect(getConverterCdnUrl("circuit-json-to-pnp-csv")).toBe( | |
| "https://cdn.jsdelivr.net/npm/circuit-json-to-pnp-csv@0.0.7/+esm", | |
| ) | |
| expect(getConverterCdnUrl("circuit-json-to-kicad")).toBe( | |
| "https://cdn.jsdelivr.net/npm/circuit-json-to-kicad@0.0.135/+esm", | |
| ) | |
| expect(getConverterCdnUrl("circuit-json-to-gltf")).toBe( | |
| "https://cdn.jsdelivr.net/npm/circuit-json-to-gltf@0.0.62/+esm", | |
| ) | |
| expect(getConverterCdnUrl("circuit-json-to-step")).toBe( | |
| "https://cdn.jsdelivr.net/npm/circuit-json-to-step@0.0.18/+esm", | |
| ) | |
| expect(getConverterCdnUrl("circuit-json-to-lbrn")).toBe( | |
| "https://cdn.jsdelivr.net/npm/circuit-json-to-lbrn@0.0.74/+esm", | |
| ) | |
| }) | |
Spotted by Graphite (based on custom rule: Custom rule)
Is this helpful? React 👍 or 👎 to let us know.
…nverters-1539 # Conflicts: # bun.lock # package.json
# Conflicts: # bun.lock # package.json
…260524-0627 # Conflicts: # bun.lock
|
This PR has been automatically marked as stale because it has had no recent activity. It will be closed if no further activity occurs. |
|
This PR was closed because it has been inactive for 1 day since being marked as stale. |
Summary
Fixes tscircuit/tscircuit#1539
/claim tscircuit/tscircuit#1539
This revives the same issue direction as the stale, closed runframe#2983 attempt, rebased onto current main with focused loader coverage.
Validation