2024-02-01 02:02:40 -05:00
|
|
|
import assert from 'node:assert/strict';
|
2024-01-25 11:17:31 -05:00
|
|
|
import { describe, it, before, after } from 'node:test';
|
2023-05-01 09:08:18 -05:00
|
|
|
import nodejs from '../dist/index.js';
|
|
|
|
import { loadFixture } from './test-utils.js';
|
|
|
|
import * as cheerio from 'cheerio';
|
|
|
|
|
|
|
|
describe('Errors', () => {
|
|
|
|
let fixture;
|
|
|
|
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 () => {
|
|
|
|
devPreview = await fixture.preview();
|
|
|
|
});
|
|
|
|
after(async () => {
|
|
|
|
await devPreview.stop();
|
|
|
|
});
|
2024-02-01 02:02:40 -05:00
|
|
|
|
|
|
|
it('rejected promise in template', async () => {
|
2024-01-25 11:17:31 -05:00
|
|
|
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-01-25 11:17:31 -05:00
|
|
|
assert.equal($('p').text().trim(), 'Internal server error');
|
2023-05-01 09:08:18 -05:00
|
|
|
});
|
2024-02-01 02:03:42 -05:00
|
|
|
|
2024-02-01 02:02:40 -05:00
|
|
|
it('generator that throws called in template', async () => {
|
2024-02-01 16:24:21 -05:00
|
|
|
const result = ['<!DOCTYPE html><h1>Astro</h1> 1', 'Internal server error'];
|
|
|
|
|
2024-02-01 02:02:40 -05:00
|
|
|
/** @type {Response} */
|
|
|
|
const res = await fixture.fetch('/generator');
|
|
|
|
const reader = res.body.getReader();
|
|
|
|
const decoder = new TextDecoder();
|
2024-02-01 16:24:21 -05:00
|
|
|
const chunk1 = await reader.read();
|
|
|
|
const chunk2 = await reader.read();
|
|
|
|
const chunk3 = await reader.read();
|
|
|
|
assert.equal(chunk1.done, false);
|
|
|
|
if (chunk2.done) {
|
2024-02-01 16:26:08 -05:00
|
|
|
assert.equal(decoder.decode(chunk1.value), result.join(''));
|
|
|
|
} else if (chunk3.done) {
|
2024-02-01 16:24:21 -05:00
|
|
|
assert.equal(decoder.decode(chunk1.value), result[0]);
|
|
|
|
assert.equal(decoder.decode(chunk2.value), result[1]);
|
2024-02-01 16:26:08 -05:00
|
|
|
} else {
|
2024-02-01 16:24:21 -05:00
|
|
|
throw new Error('The response should take at most 2 chunks.');
|
|
|
|
}
|
2024-02-01 02:02:40 -05:00
|
|
|
});
|
2023-05-01 09:08:18 -05:00
|
|
|
});
|