aboutsummaryrefslogtreecommitdiff
path: root/packages/integrations/node/test/errors.test.js
blob: 76bff13265edce10b8d7509f75cfa006bfb260e1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import assert from 'node:assert/strict';
import { describe, it, before, after } from 'node:test';
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();
	});
	let devPreview;

	before(async () => {
		devPreview = await fixture.preview();
	});
	after(async () => {
		await devPreview.stop();
	});

	it('rejected promise in template', async () => {
		const res = await fixture.fetch('/in-stream');
		const html = await res.text();
		const $ = cheerio.load(html);

		assert.equal($('p').text().trim(), 'Internal server error');
	});

	it('generator that throws called in template', async () => {
		/** @type {Response} */
		const res = await fixture.fetch('/generator');
		const reader = res.body.getReader();
		const decoder = new TextDecoder();
		const expect = async ({ done, value }) => {
			const result = await reader.read();
			assert.equal(result.done, done);
			if (!done) assert.equal(decoder.decode(result.value), value);
		};
		await expect({ done: false, value: '<!DOCTYPE html><h1>Astro</h1> 1Internal server error' });
		await expect({ done: true });
	});
});