astro/packages/integrations/node/test/trailing-slash.test.js
Rishi Raj Jain 6499c9b3da add test
2023-09-24 10:38:49 +05:30

82 lines
2.2 KiB
JavaScript

import nodejs from '../dist/index.js';
import { loadFixture } from './test-utils.js';
import { expect } from 'chai';
/**
* @typedef {import('astro/test/test-utils.js').Fixture} Fixture
*/
async function load() {
const mod = await import(
`./fixtures/trailing-slash/dist/server/entry.mjs?dropcache=${Date.now()}`
);
return mod;
}
describe('Trailing Slash In Production', () => {
/** @type {import('./test-utils.js').Fixture} */
let fixture;
let server;
describe('By default does add the trailing slash', async () => {
before(async () => {
process.env.ASTRO_NODE_AUTOSTART = 'disabled';
process.env.PRERENDER = true;
fixture = await loadFixture({
// inconsequential config that differs between tests
// to bust cache and prevent modules and their state
// from being reused
output: 'hybrid',
root: './fixtures/trailing-slash/',
adapter: nodejs({ mode: 'standalone' }),
});
await fixture.build();
const { startServer } = await load();
let res = startServer();
server = res.server;
});
after(async () => {
await server.stop();
await fixture.clean();
delete process.env.PRERENDER;
});
it('redirects each route to have trailing slash', async () => {
const res = await fetch(`http://${server.host}:${server.port}/one`);
expect(res.url).to.contain('one/');
});
});
describe('Does not add trailing slash when set to never', async () => {
before(async () => {
process.env.ASTRO_NODE_AUTOSTART = 'disabled';
process.env.PRERENDER = true;
fixture = await loadFixture({
// inconsequential config that differs between tests
// to bust cache and prevent modules and their state
// from being reused
output: 'hybrid',
trailingSlash: 'never',
root: './fixtures/trailing-slash/',
adapter: nodejs({ mode: 'standalone' }),
});
await fixture.build();
const { startServer } = await load();
let res = startServer();
server = res.server;
});
after(async () => {
await server.stop();
await fixture.clean();
delete process.env.PRERENDER;
});
it('redirects each route to have trailing slash', async () => {
const res = await fetch(`http://${server.host}:${server.port}/one`);
expect(res.url).to.not.contain('one/');
});
});
});