errors.test.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import assert from 'node:assert/strict';
  2. import { describe, it, before, after } from 'node:test';
  3. import nodejs from '../dist/index.js';
  4. import { loadFixture } from './test-utils.js';
  5. import * as cheerio from 'cheerio';
  6. describe('Errors', () => {
  7. let fixture;
  8. before(async () => {
  9. fixture = await loadFixture({
  10. root: './fixtures/errors/',
  11. output: 'server',
  12. adapter: nodejs({ mode: 'standalone' }),
  13. });
  14. await fixture.build();
  15. });
  16. let devPreview;
  17. before(async () => {
  18. devPreview = await fixture.preview();
  19. });
  20. after(async () => {
  21. await devPreview.stop();
  22. });
  23. it(
  24. 'rejected promise in template',
  25. { skip: true, todo: 'Review the response from the in-stream' },
  26. async () => {
  27. const res = await fixture.fetch('/in-stream');
  28. const html = await res.text();
  29. const $ = cheerio.load(html);
  30. assert.equal($('p').text().trim(), 'Internal server error');
  31. }
  32. );
  33. it(
  34. 'generator that throws called in template',
  35. { skip: true, todo: 'Review the response from the generator' },
  36. async () => {
  37. const result = ['<!DOCTYPE html><h1>Astro</h1> 1', 'Internal server error'];
  38. /** @type {Response} */
  39. const res = await fixture.fetch('/generator');
  40. const reader = res.body.getReader();
  41. const decoder = new TextDecoder();
  42. const chunk1 = await reader.read();
  43. const chunk2 = await reader.read();
  44. const chunk3 = await reader.read();
  45. assert.equal(chunk1.done, false);
  46. console.log(chunk1);
  47. console.log(chunk2);
  48. console.log(chunk3);
  49. if (chunk2.done) {
  50. assert.equal(decoder.decode(chunk1.value), result.join(''));
  51. } else if (chunk3.done) {
  52. assert.equal(decoder.decode(chunk1.value), result[0]);
  53. assert.equal(decoder.decode(chunk2.value), result[1]);
  54. } else {
  55. throw new Error('The response should take at most 2 chunks.');
  56. }
  57. }
  58. );
  59. });