2023-07-12 02:06:08 -05:00
|
|
|
import { expect } from 'chai';
|
2023-07-05 10:45:58 -05:00
|
|
|
import chaiJestSnapshot from 'chai-jest-snapshot';
|
2023-08-16 03:21:05 -05:00
|
|
|
import { loadFixture } from './test-utils.js';
|
2023-07-05 10:45:58 -05:00
|
|
|
|
2023-07-12 02:06:08 -05:00
|
|
|
describe('Vercel edge middleware', () => {
|
2024-01-22 18:34:48 -05:00
|
|
|
/** @type {import('../../../astro/test/test-utils.js').Fixture} */
|
|
|
|
let build;
|
|
|
|
before(async () => {
|
|
|
|
build = await loadFixture({
|
|
|
|
root: './fixtures/middleware-with-edge-file/',
|
|
|
|
});
|
|
|
|
await build.build();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('an edge function is created', async () => {
|
|
|
|
const contents = await build.readFile(
|
|
|
|
'../.vercel/output/functions/_middleware.func/.vc-config.json'
|
|
|
|
);
|
|
|
|
expect(JSON.parse(contents)).to.deep.include({
|
|
|
|
"runtime": "edge",
|
|
|
|
"entrypoint": "middleware.mjs"
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('deployment config points to the middleware edge function', async () => {
|
|
|
|
const contents = await build.readFile(
|
|
|
|
'../.vercel/output/config.json'
|
|
|
|
);
|
|
|
|
const { routes } = JSON.parse(contents);
|
|
|
|
expect(routes.some(route => route.dest === '_middleware')).to.be.true;
|
|
|
|
});
|
|
|
|
|
2023-08-16 03:21:05 -05:00
|
|
|
// TODO: The path here seems to be inconsistent?
|
|
|
|
it.skip('with edge handle file, should successfully build the middleware', async () => {
|
2023-07-12 02:06:08 -05:00
|
|
|
const fixture = await loadFixture({
|
|
|
|
root: './fixtures/middleware-with-edge-file/',
|
2023-07-05 10:45:58 -05:00
|
|
|
});
|
2023-07-12 02:06:08 -05:00
|
|
|
await fixture.build();
|
|
|
|
const contents = await fixture.readFile(
|
|
|
|
// this is abysmal...
|
2023-08-16 03:21:05 -05:00
|
|
|
'../.vercel/output/functions/render.func/www/withastro/astro/packages/integrations/vercel/test/fixtures/middleware-with-edge-file/dist/middleware.mjs'
|
2023-07-12 02:06:08 -05:00
|
|
|
);
|
|
|
|
expect(contents.includes('title:')).to.be.true;
|
|
|
|
chaiJestSnapshot.setTestName('Middleware with handler file');
|
|
|
|
expect(contents).to.matchSnapshot(true);
|
2023-07-05 10:45:58 -05:00
|
|
|
});
|
|
|
|
|
2023-08-16 03:21:05 -05:00
|
|
|
// TODO: The path here seems to be inconsistent?
|
2023-08-30 12:56:10 -05:00
|
|
|
it.skip('without edge handle file, should successfully build the middleware', async () => {
|
2023-07-12 02:06:08 -05:00
|
|
|
const fixture = await loadFixture({
|
|
|
|
root: './fixtures/middleware-without-edge-file/',
|
|
|
|
});
|
2023-07-05 10:45:58 -05:00
|
|
|
await fixture.build();
|
|
|
|
const contents = await fixture.readFile(
|
|
|
|
// this is abysmal...
|
2023-08-16 03:21:05 -05:00
|
|
|
'../.vercel/output/functions/render.func/www/withastro/astro/packages/integrations/vercel/test/fixtures/middleware-without-edge-file/dist/middleware.mjs'
|
2023-07-05 10:45:58 -05:00
|
|
|
);
|
2023-07-12 02:06:08 -05:00
|
|
|
expect(contents.includes('title:')).to.be.false;
|
|
|
|
chaiJestSnapshot.setTestName('Middleware without handler file');
|
|
|
|
expect(contents).to.matchSnapshot(true);
|
2023-07-05 10:45:58 -05:00
|
|
|
});
|
|
|
|
});
|