0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-23 21:53:55 -05:00
astro/packages/integrations/node/test/prerender.test.js
Arsh 4467441896
fix(NodeApp): fix responses with null bodies never completing (#9931)
* fix(NodeApp): fix responses with null bodies never completing

* add changeset

* add test

* chore(tests): restore correct assertions

* adjust incorrect test

* added Astro.redirect and Response.redirect test cases

* updated incorrect HTTP status

* adjust api-routes.test.js after cherry-pick

* bup markdoc test timeout

---------

Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>
Co-authored-by: Friedemann Sommer <friedemannsommer@users.noreply.github.com>
2024-02-01 16:24:21 -05:00

349 lines
9.6 KiB
JavaScript

import * as assert from 'node:assert/strict';
import { describe, it, before, after } from 'node:test';
import nodejs from '../dist/index.js';
import { loadFixture, waitServerListen } from './test-utils.js';
import * as cheerio from 'cheerio';
/**
* @typedef {import('../../../astro/test/test-utils').Fixture} Fixture
*/
async function load() {
const mod = await import(`./fixtures/prerender/dist/server/entry.mjs?dropcache=${Date.now()}`);
return mod;
}
describe('Prerendering', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
let server;
describe('With base', async () => {
before(async () => {
process.env.PRERENDER = true;
fixture = await loadFixture({
base: '/some-base',
root: './fixtures/prerender/',
output: 'server',
adapter: nodejs({ mode: 'standalone' }),
});
await fixture.build();
const { startServer } = await load();
let res = startServer();
server = res.server;
await waitServerListen(server.server);
});
after(async () => {
await server.stop();
await fixture.clean();
delete process.env.PRERENDER;
});
it('Can render SSR route', async () => {
const res = await fetch(`http://${server.host}:${server.port}/some-base/one`);
const html = await res.text();
const $ = cheerio.load(html);
assert.equal(res.status, 200);
assert.equal($('h1').text(), 'One');
});
it('Can render prerendered route', async () => {
const res = await fetch(`http://${server.host}:${server.port}/some-base/two`);
const html = await res.text();
const $ = cheerio.load(html);
assert.equal(res.status, 200);
assert.equal($('h1').text(), 'Two');
});
it('Can render prerendered route with redirect and query params', async () => {
const res = await fetch(`http://${server.host}:${server.port}/some-base/two?foo=bar`);
const html = await res.text();
const $ = cheerio.load(html);
assert.equal(res.status, 200);
assert.equal($('h1').text(), 'Two');
});
it('Can render prerendered route with query params', async () => {
const res = await fetch(`http://${server.host}:${server.port}/some-base/two/?foo=bar`);
const html = await res.text();
const $ = cheerio.load(html);
assert.equal(res.status, 200);
assert.equal($('h1').text(), 'Two');
});
it('Can render prerendered route without trailing slash', async () => {
const res = await fetch(`http://${server.host}:${server.port}/some-base/two`, {
redirect: 'manual',
});
const html = await res.text();
const $ = cheerio.load(html);
assert.equal(res.status, 200);
assert.equal($('h1').text(), 'Two');
});
});
describe('Without base', async () => {
before(async () => {
process.env.PRERENDER = true;
fixture = await loadFixture({
root: './fixtures/prerender/',
output: 'server',
adapter: nodejs({ mode: 'standalone' }),
});
await fixture.build();
const { startServer } = await await load();
let res = startServer();
server = res.server;
await waitServerListen(server.server);
});
after(async () => {
await server.stop();
await fixture.clean();
delete process.env.PRERENDER;
});
it('Can render SSR route', async () => {
const res = await fetch(`http://${server.host}:${server.port}/one`);
const html = await res.text();
const $ = cheerio.load(html);
assert.equal(res.status, 200);
assert.equal($('h1').text(), 'One');
});
it('Can render prerendered route', async () => {
const res = await fetch(`http://${server.host}:${server.port}/two`);
const html = await res.text();
const $ = cheerio.load(html);
assert.equal(res.status, 200);
assert.equal($('h1').text(), 'Two');
});
it('Can render prerendered route with redirect and query params', async () => {
const res = await fetch(`http://${server.host}:${server.port}/two?foo=bar`);
const html = await res.text();
const $ = cheerio.load(html);
assert.equal(res.status, 200);
assert.equal($('h1').text(), 'Two');
});
it('Can render prerendered route with query params', async () => {
const res = await fetch(`http://${server.host}:${server.port}/two/?foo=bar`);
const html = await res.text();
const $ = cheerio.load(html);
assert.equal(res.status, 200);
assert.equal($('h1').text(), 'Two');
});
});
describe('Dev', () => {
let devServer;
before(async () => {
process.env.PRERENDER = true;
fixture = await loadFixture({
root: './fixtures/prerender/',
output: 'server',
adapter: nodejs({ mode: 'standalone' }),
});
devServer = await fixture.startDevServer();
});
after(async () => {
await devServer.stop();
delete process.env.PRERENDER;
});
it('Can render SSR route', async () => {
const res = await fixture.fetch(`/one`);
const html = await res.text();
const $ = cheerio.load(html);
assert.equal(res.status, 200);
assert.equal($('h1').text(), 'One');
});
it('Can render prerendered route', async () => {
const res = await fixture.fetch(`/two`);
const html = await res.text();
const $ = cheerio.load(html);
assert.equal(res.status, 200);
assert.equal($('h1').text(), 'Two');
});
});
});
describe('Hybrid rendering', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
let server;
describe('With base', async () => {
before(async () => {
process.env.PRERENDER = false;
fixture = await loadFixture({
base: '/some-base',
root: './fixtures/prerender/',
output: 'hybrid',
adapter: nodejs({ mode: 'standalone' }),
});
await fixture.build();
const { startServer } = await load();
let res = startServer();
server = res.server;
await waitServerListen(server.server);
});
after(async () => {
await server.stop();
await fixture.clean();
delete process.env.PRERENDER;
});
it('Can render SSR route', async () => {
const res = await fetch(`http://${server.host}:${server.port}/some-base/two`);
const html = await res.text();
const $ = cheerio.load(html);
assert.equal(res.status, 200);
assert.equal($('h1').text(), 'Two');
});
it('Can render prerendered route', async () => {
const res = await fetch(`http://${server.host}:${server.port}/some-base/one`);
const html = await res.text();
const $ = cheerio.load(html);
assert.equal(res.status, 200);
assert.equal($('h1').text(), 'One');
});
it('Can render prerendered route with redirect and query params', async () => {
const res = await fetch(`http://${server.host}:${server.port}/some-base/one?foo=bar`);
const html = await res.text();
const $ = cheerio.load(html);
assert.equal(res.status, 200);
assert.equal($('h1').text(), 'One');
});
it('Can render prerendered route with query params', async () => {
const res = await fetch(`http://${server.host}:${server.port}/some-base/one/?foo=bar`);
const html = await res.text();
const $ = cheerio.load(html);
assert.equal(res.status, 200);
assert.equal($('h1').text(), 'One');
});
it('Can render prerendered route without trailing slash', async () => {
const res = await fetch(`http://${server.host}:${server.port}/some-base/one`, {
redirect: 'manual',
});
const html = await res.text();
const $ = cheerio.load(html);
assert.equal(res.status, 200);
assert.equal($('h1').text(), 'One');
});
});
describe('Without base', async () => {
before(async () => {
process.env.PRERENDER = false;
fixture = await loadFixture({
root: './fixtures/prerender/',
output: 'hybrid',
adapter: nodejs({ mode: 'standalone' }),
});
await fixture.build();
const { startServer } = await load();
let res = startServer();
server = res.server;
await waitServerListen(server.server);
});
after(async () => {
await server.stop();
await fixture.clean();
delete process.env.PRERENDER;
});
it('Can render SSR route', async () => {
const res = await fetch(`http://${server.host}:${server.port}/two`);
const html = await res.text();
const $ = cheerio.load(html);
assert.equal(res.status, 200);
assert.equal($('h1').text(), 'Two');
});
it('Can render prerendered route', async () => {
const res = await fetch(`http://${server.host}:${server.port}/one`);
const html = await res.text();
const $ = cheerio.load(html);
assert.equal(res.status, 200);
assert.equal($('h1').text(), 'One');
});
it('Can render prerendered route with redirect and query params', async () => {
const res = await fetch(`http://${server.host}:${server.port}/one?foo=bar`);
const html = await res.text();
const $ = cheerio.load(html);
assert.equal(res.status, 200);
assert.equal($('h1').text(), 'One');
});
it('Can render prerendered route with query params', async () => {
const res = await fetch(`http://${server.host}:${server.port}/one/?foo=bar`);
const html = await res.text();
const $ = cheerio.load(html);
assert.equal(res.status, 200);
assert.equal($('h1').text(), 'One');
});
});
describe('Shared modules', async () => {
before(async () => {
process.env.PRERENDER = false;
fixture = await loadFixture({
root: './fixtures/prerender/',
output: 'hybrid',
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('Can render SSR route', async () => {
const res = await fetch(`http://${server.host}:${server.port}/third`);
const html = await res.text();
const $ = cheerio.load(html);
assert.equal(res.status, 200);
assert.equal($('h1').text(), 'shared');
});
});
});