static-assets.test.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import assert from 'node:assert/strict';
  2. import { describe, it } from 'node:test';
  3. import { loadFixture } from './test-utils.js';
  4. describe('Static Assets', () => {
  5. /** @type {import('../../../astro/test/test-utils.js').Fixture} */
  6. let fixture;
  7. const VALID_CACHE_CONTROL = 'public, max-age=31536000, immutable';
  8. async function build({ adapter, assets, output }) {
  9. fixture = await loadFixture({
  10. root: './fixtures/static-assets/',
  11. output,
  12. adapter,
  13. build: {
  14. assets,
  15. },
  16. });
  17. await fixture.build();
  18. }
  19. async function getConfig() {
  20. const json = await fixture.readFile('../.vercel/output/config.json');
  21. const config = JSON.parse(json);
  22. return config;
  23. }
  24. async function getAssets() {
  25. return fixture.config.build.assets;
  26. }
  27. async function checkValidCacheControl(assets) {
  28. const config = await getConfig();
  29. const theAssets = assets ?? (await getAssets());
  30. const route = config.routes.find((r) => r.src === `^/${theAssets}/(.*)$`);
  31. assert.equal(route.headers['cache-control'], VALID_CACHE_CONTROL);
  32. assert.equal(route.continue, true);
  33. }
  34. describe('static adapter', () => {
  35. it('has cache control', async () => {
  36. const { default: vercel } = await import('@astrojs/vercel/static');
  37. await build({ adapter: vercel() });
  38. await checkValidCacheControl();
  39. });
  40. it('has cache control other assets', async () => {
  41. const { default: vercel } = await import('@astrojs/vercel/static');
  42. const assets = '_foo';
  43. await build({ adapter: vercel(), assets });
  44. await checkValidCacheControl(assets);
  45. });
  46. });
  47. describe('serverless adapter', () => {
  48. it('has cache control', async () => {
  49. const { default: vercel } = await import('@astrojs/vercel/serverless');
  50. await build({ output: 'server', adapter: vercel() });
  51. await checkValidCacheControl();
  52. });
  53. it('has cache control other assets', async () => {
  54. const { default: vercel } = await import('@astrojs/vercel/serverless');
  55. const assets = '_foo';
  56. await build({ output: 'server', adapter: vercel(), assets });
  57. await checkValidCacheControl(assets);
  58. });
  59. });
  60. });