2024-02-01 02:02:40 -05:00
|
|
|
import assert from 'node:assert/strict';
|
2024-01-25 11:17:31 -05:00
|
|
|
import { after, before, describe, it } from 'node:test';
|
2024-03-18 09:15:46 -05:00
|
|
|
import { Worker } from 'node:worker_threads';
|
2023-05-01 09:08:18 -05:00
|
|
|
import * as cheerio from 'cheerio';
|
|
|
|
import nodejs from '../dist/index.js';
|
|
|
|
import { loadFixture } from './test-utils.js';
|
|
|
|
|
|
|
|
describe('Errors', () => {
|
2024-03-18 09:14:46 -05:00
|
|
|
/** @type {import('./test-utils.js').Fixture} */
|
2023-05-01 09:08:18 -05:00
|
|
|
let fixture;
|
2024-03-18 09:14:46 -05:00
|
|
|
|
2023-05-01 09:08:18 -05:00
|
|
|
before(async () => {
|
|
|
|
fixture = await loadFixture({
|
|
|
|
root: './fixtures/errors/',
|
|
|
|
output: 'server',
|
|
|
|
adapter: nodejs({ mode: 'standalone' }),
|
|
|
|
});
|
|
|
|
await fixture.build();
|
|
|
|
});
|
2024-01-25 11:17:31 -05:00
|
|
|
let devPreview;
|
2023-05-01 09:08:18 -05:00
|
|
|
|
2024-01-25 11:17:31 -05:00
|
|
|
before(async () => {
|
2024-03-18 09:14:46 -05:00
|
|
|
// The two tests that need the server to run are skipped
|
|
|
|
// devPreview = await fixture.preview();
|
2024-01-25 11:17:31 -05:00
|
|
|
});
|
|
|
|
after(async () => {
|
2024-03-18 09:14:46 -05:00
|
|
|
await devPreview?.stop();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('stays alive after offshoot promise rejections', async () => {
|
|
|
|
// this test needs to happen in a worker because node test runner adds a listener for unhandled rejections in the main thread
|
|
|
|
const worker = new Worker('./test/fixtures/errors/dist/server/entry.mjs', {
|
|
|
|
type: 'module',
|
2024-03-18 09:15:46 -05:00
|
|
|
env: { ASTRO_NODE_LOGGING: 'enabled' },
|
2024-03-18 09:14:46 -05:00
|
|
|
});
|
2024-03-18 09:15:46 -05:00
|
|
|
|
2024-03-18 09:14:46 -05:00
|
|
|
await new Promise((resolve, reject) => {
|
2024-03-18 09:15:46 -05:00
|
|
|
worker.stdout.on('data', (data) => {
|
|
|
|
setTimeout(() => reject('Server took too long to start'), 1000);
|
2024-03-18 09:14:46 -05:00
|
|
|
if (data.toString().includes('Server listening on http://localhost:4321')) resolve();
|
|
|
|
});
|
|
|
|
});
|
2024-03-18 09:15:46 -05:00
|
|
|
|
|
|
|
await fetch('http://localhost:4321/offshoot-promise-rejection');
|
2024-03-18 09:14:46 -05:00
|
|
|
|
|
|
|
// if there was a crash, it becomes an error here
|
|
|
|
await worker.terminate();
|
2024-01-25 11:17:31 -05:00
|
|
|
});
|
2024-02-01 02:02:40 -05:00
|
|
|
|
2024-02-16 09:02:18 -05:00
|
|
|
it(
|
|
|
|
'rejected promise in template',
|
|
|
|
{ skip: true, todo: 'Review the response from the in-stream' },
|
|
|
|
async () => {
|
|
|
|
const res = await fixture.fetch('/in-stream');
|
|
|
|
const html = await res.text();
|
|
|
|
const $ = cheerio.load(html);
|
2023-05-01 09:08:18 -05:00
|
|
|
|
2024-02-16 09:02:18 -05:00
|
|
|
assert.equal($('p').text().trim(), 'Internal server error');
|
|
|
|
}
|
|
|
|
);
|
2024-02-01 02:03:42 -05:00
|
|
|
|
2024-02-16 09:02:18 -05:00
|
|
|
it(
|
|
|
|
'generator that throws called in template',
|
|
|
|
{ skip: true, todo: 'Review the response from the generator' },
|
|
|
|
async () => {
|
|
|
|
const result = ['<!DOCTYPE html><h1>Astro</h1> 1', 'Internal server error'];
|
2024-02-01 16:24:21 -05:00
|
|
|
|
2024-02-16 09:02:18 -05:00
|
|
|
/** @type {Response} */
|
|
|
|
const res = await fixture.fetch('/generator');
|
|
|
|
const reader = res.body.getReader();
|
|
|
|
const decoder = new TextDecoder();
|
|
|
|
const chunk1 = await reader.read();
|
|
|
|
const chunk2 = await reader.read();
|
|
|
|
const chunk3 = await reader.read();
|
|
|
|
assert.equal(chunk1.done, false);
|
|
|
|
console.log(chunk1);
|
|
|
|
console.log(chunk2);
|
|
|
|
console.log(chunk3);
|
|
|
|
if (chunk2.done) {
|
|
|
|
assert.equal(decoder.decode(chunk1.value), result.join(''));
|
|
|
|
} else if (chunk3.done) {
|
|
|
|
assert.equal(decoder.decode(chunk1.value), result[0]);
|
|
|
|
assert.equal(decoder.decode(chunk2.value), result[1]);
|
|
|
|
} else {
|
|
|
|
throw new Error('The response should take at most 2 chunks.');
|
|
|
|
}
|
2024-02-01 16:24:21 -05:00
|
|
|
}
|
2024-02-16 09:02:18 -05:00
|
|
|
);
|
2023-05-01 09:08:18 -05:00
|
|
|
});
|