-
richard.petersen authored
Root cause: Due to the timeout, every request that was launched while fetching the previous manifests could led to another fetch and another cache.warmup Solution: Be more synchronous
richard.petersen authoredRoot cause: Due to the timeout, every request that was launched while fetching the previous manifests could led to another fetch and another cache.warmup Solution: Be more synchronous
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
server_test.js 3.62 KiB
import { describe, it, expect, beforeAll, afterAll, beforeEach, afterEach } from '@jest/globals'
import mockfs from 'mock-fs'
import request from 'supertest'
import { createApp } from '../src/createApp'
import { createMockServer, generateSimpleViteManifest, getRandomPort } from './util.js'
describe('Manifest service', () => {
let app
let mockserver
const port = getRandomPort()
beforeAll(() => {
mockfs({
'./config/manifests': {
'urls.yaml': `manifests:
- http://localhost:${port}/api/manifest.json`
}
})
app = createApp()
})
afterAll(() => {
mockfs.restore()
})
beforeEach(async () => {
mockserver = await createMockServer({ port })
mockserver.respondWith({
'/api/manifest.json': generateSimpleViteManifest({ 'example.js': 'test' }),
'/example.js': ''
})
})
afterEach(() => {
mockserver.close()
process.env.CACHE_TTL = 30000
})
it('is healthy', async () => {
const response = await request(app).get('/healthy')
expect(response.statusCode).toBe(200)
})
it('fetches manifest data', async () => {
const response = await request(app).get('/manifests')
expect(response.statusCode).toBe(200)
expect(response.body).toEqual([{ namespace: 'test', path: 'example' }])
})
it('caches manifest data', async () => {
const response = await request(app).get('/manifests')
expect(response.statusCode).toBe(200)
expect(response.body).toEqual([{ namespace: 'test', path: 'example' }])
mockserver.close()
mockserver = await createMockServer({ port })
mockserver.respondWith({
'/api/manifest.json': generateSimpleViteManifest({ 'example.js': 'other' }),
'/example.js': ''
})
await new Promise(resolve => setTimeout(resolve, 150))
const response2 = await request(app).get('/manifests')
expect(response2.statusCode).toBe(200)
expect(response2.body).toEqual([{ namespace: 'test', path: 'example' }])
})
it('refreshes manifest data after caching timeout', async () => {
process.env.CACHE_TTL = 1
app = createApp()
const response = await request(app).get('/manifests')
expect(response.statusCode).toBe(200)
expect(response.body).toEqual([{ namespace: 'test', path: 'example' }])
mockserver.close()
mockserver = await createMockServer({ port })
mockserver.respondWith({
'/api/manifest.json': generateSimpleViteManifest({ 'example.js': 'other' }),
'/example.js': ''
})
// wait some time
await new Promise(resolve => setTimeout(resolve, 10))
const response2 = await request(app).get('/manifests')
expect(response2.statusCode).toBe(200)
expect(response2.body).toEqual([{ namespace: 'other', path: 'example' }])
})
it('can load multiple configurations', async () => {
mockfs({
'./config/manifests': {
'urls.yaml': `manifests:
- http://localhost:${port}/api/manifest.json
- http://localhost:${port}/api/no2/manifest.json`
}
})
mockserver.close()
mockserver = await createMockServer({ port })
mockserver.respondWith({
'/api/manifest.json': generateSimpleViteManifest({ 'example1.js': 'other' }),
'/api/no2/manifest.json': generateSimpleViteManifest({ 'example2.js': 'thing' }),
'/example1.js': '',
'/example2.js': ''
})
process.env.CACHE_TTL = 1
const app = createApp()
await request(app)
.get('/manifests')
.then(response => {
expect(response.statusCode).toBe(200)
expect(response.body).toEqual([
{ namespace: 'other', path: 'example1' },
{ namespace: 'thing', path: 'example2' }
])
})
})
})