Skip to content

Commit

Permalink
feature: add stat enoent error test (#255)
Browse files Browse the repository at this point in the history
* feature: add stat enoent error test

* test
  • Loading branch information
levivilet authored Dec 27, 2024
1 parent 224daec commit 3045897
Showing 1 changed file with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { expect, test } from '@jest/globals'
import getPort from 'get-port'
import { fileURLToPath } from 'url'
import { connectToCdp } from '../src/parts/ConnectToCdp/ConnectToCdp.ts'
import { createPreviewProcess } from '../src/parts/CreatePreviewProcess/CreatePreviewProcess.js'
import { get } from '../src/parts/Get/Get.js'
import { getRoot } from '../src/parts/GetRoot/GetRoot.js'
import { mockModule } from '../src/parts/MockModule/MockModule.js'

test('preview process - handles range request with stat ENOENT error', async () => {
const debugPort = await getPort()
const mockCdpImport = new URL('../src/mockCdpImport.js', import.meta.url).toString()
const root = getRoot()
const rootPath = fileURLToPath(root)
const slash = process.platform === 'win32' ? '\\' : '/'
const filePath = `${rootPath}${slash}any-file.txt`
const previewProcess = createPreviewProcess({
execArgv: [`--inspect=localhost:${debugPort}`, '--experimental-strip-types', `--import=${mockCdpImport}`],
})

const client = await connectToCdp(debugPort)

await mockModule(client.Runtime, {
moduleName: 'node:fs/promises',
functionName: 'stat',
filePath,
errorMessage: 'File not found',
errorCode: 'ENOENT',
})

const id = 1
const port = await getPort()

await previewProcess.invoke('WebViewServer.create', id)
await previewProcess.invoke('WebViewServer.setHandler', id, '', root, '', '')
await previewProcess.invoke('WebViewServer.start', id, port)

const response = await get(`http://localhost:${port}/any-file.txt`, {
headers: {
Range: 'bytes=0-100',
},
})
expect(response.status).toBe(404)
expect(await response.text()).toBe('not found')

await client[Symbol.dispose]()
previewProcess[Symbol.dispose]()
})

0 comments on commit 3045897

Please sign in to comment.