mirror of
https://github.com/supabase/supabase.git
synced 2026-07-03 04:24:20 +08:00
* Add more unit tests to lib * Add tests for eszip-parser * Add tests for integration-utils * Update role-impersonation test * lint
120 lines
3.2 KiB
TypeScript
120 lines
3.2 KiB
TypeScript
import { Parser } from '@deno/eszip'
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import { parseEszip } from './eszip-parser'
|
|
|
|
vi.mock('@deno/eszip', () => ({
|
|
Parser: {
|
|
createInstance: vi.fn(),
|
|
},
|
|
}))
|
|
|
|
vi.stubGlobal(
|
|
'File',
|
|
class MockFile {
|
|
name: string
|
|
content: string
|
|
|
|
constructor(content: string[], name: string) {
|
|
this.name = name
|
|
this.content = content[0]
|
|
}
|
|
|
|
async text() {
|
|
return this.content
|
|
}
|
|
}
|
|
)
|
|
|
|
vi.stubGlobal(
|
|
'URL',
|
|
class MockURL {
|
|
pathname: string
|
|
|
|
constructor(url: string) {
|
|
this.pathname = url
|
|
}
|
|
}
|
|
)
|
|
|
|
describe('eszip-parser', () => {
|
|
const mockParser = {
|
|
parseBytes: vi.fn(),
|
|
load: vi.fn(),
|
|
getModuleSource: vi.fn(),
|
|
}
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
;(Parser.createInstance as any).mockResolvedValue(mockParser)
|
|
})
|
|
|
|
afterEach(() => {
|
|
vi.resetAllMocks()
|
|
})
|
|
|
|
describe('parseEszip', () => {
|
|
it('should successfully parse and extract files from eszip', async () => {
|
|
const mockBytes = new Uint8Array([1, 2, 3])
|
|
const mockSpecifiers = ['file1.ts', 'file2.ts']
|
|
const mockModuleSource1 = 'export const hello = "world"'
|
|
const mockModuleSource2 = 'export const foo = "bar"'
|
|
|
|
mockParser.parseBytes.mockResolvedValue(mockSpecifiers)
|
|
mockParser.load.mockResolvedValue(undefined)
|
|
mockParser.getModuleSource
|
|
.mockResolvedValueOnce(mockModuleSource1)
|
|
.mockResolvedValueOnce(mockModuleSource2)
|
|
|
|
const result = await parseEszip(mockBytes)
|
|
|
|
expect(Parser.createInstance).toHaveBeenCalledTimes(1)
|
|
expect(mockParser.parseBytes).toHaveBeenCalledWith(mockBytes)
|
|
expect(mockParser.load).toHaveBeenCalled()
|
|
expect(result).toHaveLength(2)
|
|
expect(result[0]).toEqual({
|
|
name: 'file1.ts',
|
|
content: mockModuleSource1,
|
|
})
|
|
expect(result[1]).toEqual({
|
|
name: 'file2.ts',
|
|
content: mockModuleSource2,
|
|
})
|
|
})
|
|
|
|
it('should handle parseBytes failure', async () => {
|
|
mockParser.parseBytes.mockRejectedValue(new Error('Parse error'))
|
|
await expect(parseEszip(new Uint8Array())).rejects.toThrow('Parse error')
|
|
})
|
|
|
|
it('should handle load failure', async () => {
|
|
mockParser.parseBytes.mockResolvedValue(['file1.ts'])
|
|
mockParser.load.mockRejectedValue(new Error('Load error'))
|
|
|
|
await expect(parseEszip(new Uint8Array())).rejects.toThrow('Load error')
|
|
})
|
|
|
|
it('should filter out unwanted specifiers', async () => {
|
|
const mockBytes = new Uint8Array([1, 2, 3])
|
|
const mockSpecifiers = [
|
|
'file1.ts',
|
|
'npm:package',
|
|
'https://example.com/file.ts',
|
|
'file2.ts',
|
|
'---internal',
|
|
'jsr:package',
|
|
]
|
|
const mockModuleSource = 'export const test = "test"'
|
|
|
|
mockParser.parseBytes.mockResolvedValue(mockSpecifiers)
|
|
mockParser.load.mockResolvedValue(undefined)
|
|
mockParser.getModuleSource.mockResolvedValue(mockModuleSource)
|
|
|
|
const result = await parseEszip(mockBytes)
|
|
// Only file1.ts and file2.ts should be included
|
|
expect(result).toHaveLength(2)
|
|
expect(result[0].name).toBe('file1.ts')
|
|
expect(result[1].name).toBe('file2.ts')
|
|
})
|
|
})
|
|
})
|