aboutsummaryrefslogtreecommitdiff
path: root/packages/integrations/node/test/errors.test.js
blob: 95bb1be854663c7228f1063b194d03b79334f7e4 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import assert from 'node:assert/strict';
import { after, before, describe, it } from 'node:test';
import * as cheerio from 'cheerio';
import nodejs from '../dist/index.js';
import { loadFixture } from './test-utils.js';

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',
		{ 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);

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

	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'];

			/** @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.');
			}
		}
	);
});