[READ-ONLY] Mirror of https://github.com/danielroe/cross-origin-storage. Load shared dependencies from Cross-Origin Storage (COS).
cross-origin-storage
experimental
nuxt
vite
vite-plugin
1import { fileURLToPath } from 'node:url'
2import { describe, expect, it } from 'vitest'
3import { $fetch, setup } from '@nuxt/test-utils/e2e'
4import type { CosManifest } from '../src/runtime/loader'
5
6function parseManifest(html: string): CosManifest {
7 const start = html.indexOf('{"base":')
8 expect(start, 'cos manifest not found in loader script').toBeGreaterThan(-1)
9
10 let depth = 0
11 for (let i = start; i < html.length; i++) {
12 if (html[i] === '{') depth++
13 else if (html[i] === '}' && --depth === 0) {
14 return JSON.parse(html.slice(start, i + 1)) as CosManifest
15 }
16 }
17 throw new Error('unterminated cos manifest')
18}
19
20describe('ssr', async () => {
21 await setup({
22 rootDir: fileURLToPath(new URL('./fixtures/basic', import.meta.url)),
23 build: true,
24 })
25
26 it('renders the server-rendered markup', async () => {
27 const html = await $fetch('/')
28 expect(html).toContain('count: 0')
29 })
30
31 it('injects the cos loader and removes the default entry script', async () => {
32 const html = await $fetch('/')
33 expect(html).toContain('<script id="cos-loader">')
34 expect(html).toMatch(/cos1:[a-f0-9]{64}/)
35 expect(html).not.toMatch(/<script type="module"[^>]*src="\/_nuxt\/[^"]*"/)
36 })
37
38 it('keys every managed chunk by the content hash it declares', async () => {
39 const { chunks } = parseManifest(await $fetch('/'))
40 expect(Object.keys(chunks).length).toBe(5)
41 for (const [specifier, { hash }] of Object.entries(chunks)) {
42 expect(specifier).toBe(`cos1:${hash}`)
43 }
44 })
45
46 it('declares an entry that is loaded outside cos', async () => {
47 const { entry, chunks } = parseManifest(await $fetch('/'))
48 expect(entry.specifier).toBe('cos1:entry')
49 expect(entry.file).toBeTruthy()
50 // The entry is app-specific and must not be a COS-managed chunk.
51 expect(chunks[entry.specifier]).toBeUndefined()
52 })
53})