0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

Add test for astro:route:setup hook

This commit is contained in:
Luiz Ferraz 2024-08-31 00:13:38 -03:00
parent 11ebf3bd15
commit a6465c2a85
No known key found for this signature in database
GPG key ID: B20E8273A6810404
8 changed files with 94 additions and 0 deletions

View file

@ -0,0 +1,9 @@
import { defineConfig } from 'astro/config'
import test from './integration.js'
import node from '@astrojs/node';
export default defineConfig({
output: 'hybrid',
adapter: node({ mode: 'standalone' }),
integrations: [test()]
})

View file

@ -0,0 +1,12 @@
export default function () {
const routes = (globalThis.TEST_ROUTES_OBJECTS = []);
return {
name: '@astrojs/test-integration',
hooks: {
'astro:route:setup': ({ route }) => {
routes.push(route);
},
},
};
}

View file

@ -0,0 +1,10 @@
{
"name": "@test/integration-route-setup",
"type": "module",
"version": "0.0.0",
"private": true,
"dependencies": {
"@astrojs/node": "^8.3.3",
"astro": "workspace:*"
}
}

View file

@ -0,0 +1,5 @@
---
export const prerender = false;
---
<div></div>

View file

@ -0,0 +1,5 @@
---
export const prerender = true;
---
<div></div>

View file

@ -0,0 +1,13 @@
const routes = (globalThis.TEST_ROUTES_OBJECTS ?? []);
export const GET = () => {
// Routes are not garanteed to be loaded in any particular order
// sort for test stability.
routes.sort((a, b) => a.component.localeCompare(b.component));
return new Response(JSON.stringify(routes), {
headers: {
'content-type': 'application/json'
},
});
};

View file

@ -0,0 +1,31 @@
import assert from 'node:assert/strict';
import { before, describe, it } from 'node:test';
import { loadFixture } from './test-utils.js';
describe('Integration route setup', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
before(async () => {
fixture = await loadFixture({ root: './fixtures/integration-route-setup/' });
await fixture.build();
});
it('Adds middlewares in dev', async () => {
const routeInfo = JSON.parse(await fixture.readFile('client/report.json'));
assert.deepEqual(routeInfo, [
{
component: 'src/pages/no-prerender.astro',
prerender: false,
},
{
component: 'src/pages/prerender.astro',
prerender: true,
},
{
component: 'src/pages/report.json.js',
},
]);
});
});

View file

@ -3316,6 +3316,15 @@ importers:
specifier: workspace:*
version: link:../../..
packages/astro/test/fixtures/integration-route-setup:
dependencies:
'@astrojs/node':
specifier: ^8.3.3
version: 8.3.3(astro@packages+astro)
astro:
specifier: workspace:*
version: link:../../..
packages/astro/test/fixtures/integration-server-setup:
dependencies:
astro: