edge-middleware.test.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import assert from 'node:assert/strict';
  2. import { before, describe, it } from 'node:test';
  3. import { loadFixture } from './test-utils.js';
  4. describe('Vercel edge middleware', () => {
  5. /** @type {import('../../../astro/test/test-utils.js').Fixture} */
  6. let build;
  7. before(async () => {
  8. build = await loadFixture({
  9. root: './fixtures/middleware-with-edge-file/',
  10. });
  11. await build.build();
  12. });
  13. it('an edge function is created', async () => {
  14. const contents = await build.readFile(
  15. '../.vercel/output/functions/_middleware.func/.vc-config.json'
  16. );
  17. const contentsJSON = JSON.parse(contents);
  18. assert.equal(contentsJSON.runtime, 'edge');
  19. assert.equal(contentsJSON.entrypoint, 'middleware.mjs');
  20. });
  21. it('deployment config points to the middleware edge function', async () => {
  22. const contents = await build.readFile('../.vercel/output/config.json');
  23. const { routes } = JSON.parse(contents);
  24. assert.equal(
  25. routes.some((route) => route.dest === '_middleware'),
  26. true
  27. );
  28. });
  29. // TODO: The path here seems to be inconsistent?
  30. it.skip('with edge handle file, should successfully build the middleware', async () => {
  31. const fixture = await loadFixture({
  32. root: './fixtures/middleware-with-edge-file/',
  33. });
  34. await fixture.build();
  35. const contents = await fixture.readFile(
  36. // this is abysmal...
  37. '../.vercel/output/functions/render.func/www/withastro/astro/packages/integrations/vercel/test/fixtures/middleware-with-edge-file/dist/middleware.mjs'
  38. );
  39. // assert.equal(contents.includes('title:')).to.be.true;
  40. // chaiJestSnapshot.setTestName('Middleware with handler file');
  41. // assert.equal(contents).to.matchSnapshot(true);
  42. });
  43. // TODO: The path here seems to be inconsistent?
  44. it.skip('without edge handle file, should successfully build the middleware', async () => {
  45. const fixture = await loadFixture({
  46. root: './fixtures/middleware-without-edge-file/',
  47. });
  48. await fixture.build();
  49. const contents = await fixture.readFile(
  50. // this is abysmal...
  51. '../.vercel/output/functions/render.func/www/withastro/astro/packages/integrations/vercel/test/fixtures/middleware-without-edge-file/dist/middleware.mjs'
  52. );
  53. // assert.equal(contents.includes('title:')).to.be.false;
  54. // chaiJestSnapshot.setTestName('Middleware without handler file');
  55. // assert.equal(contents).to.matchSnapshot(true);
  56. });
  57. });