Skip to content

Commit

Permalink
Try rewriting the test to see if it works in other Node envs
Browse files Browse the repository at this point in the history
  • Loading branch information
sebmarkbage committed Mar 12, 2021
1 parent bbb58f2 commit 91d2ed0
Showing 1 changed file with 26 additions and 22 deletions.
48 changes: 26 additions & 22 deletions packages/react-dom/src/__tests__/ReactDOMFizzServerNode-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,22 @@ describe('ReactDOMFizzServer', () => {
function getTestWritable() {
const writable = new Stream.PassThrough();
writable.setEncoding('utf8');
writable.result = '';
const output = {result: '', error: undefined};
writable.on('data', chunk => {
writable.result += chunk;
output.result += chunk;
});
writable.on('error', error => {
writable.error = error;
output.error = error;
});
return writable;
}

function waitUntilClose(writable) {
return new Promise(resolve => writable.on('close', resolve));
const completed = new Promise(resolve => {
writable.on('finish', () => {
resolve();
});
writable.on('error', () => {
resolve();
});
});
return {writable, completed, output};
}

const theError = new Error('This is an error');
Expand All @@ -54,31 +58,31 @@ describe('ReactDOMFizzServer', () => {

// @gate experimental
it('should call pipeToNodeWritable', () => {
const writable = getTestWritable();
const {writable, output} = getTestWritable();
ReactDOMFizzServer.pipeToNodeWritable(<div>hello world</div>, writable);
jest.runAllTimers();
expect(writable.result).toBe('<div>hello world</div>');
expect(output.result).toBe('<div>hello world</div>');
});

// @gate experimental
it('should error the stream when an error is thrown at the root', async () => {
const writable = getTestWritable();
const {writable, output, completed} = getTestWritable();
ReactDOMFizzServer.pipeToNodeWritable(
<div>
<Throw />
</div>,
writable,
);

await waitUntilClose(writable);
await completed;

expect(writable.error).toBe(theError);
expect(writable.result).toBe('');
expect(output.error).toBe(theError);
expect(output.result).toBe('');
});

// @gate experimental
it('should error the stream when an error is thrown inside a fallback', async () => {
const writable = getTestWritable();
const {writable, output, completed} = getTestWritable();
ReactDOMFizzServer.pipeToNodeWritable(
<div>
<Suspense fallback={<Throw />}>
Expand All @@ -88,15 +92,15 @@ describe('ReactDOMFizzServer', () => {
writable,
);

await waitUntilClose(writable);
await completed;

expect(writable.error).toBe(theError);
expect(writable.result).toBe('');
expect(output.error).toBe(theError);
expect(output.result).toBe('');
});

// @gate experimental
it('should not error the stream when an error is thrown inside suspense boundary', async () => {
const writable = getTestWritable();
const {writable, output, completed} = getTestWritable();
ReactDOMFizzServer.pipeToNodeWritable(
<div>
<Suspense fallback={<div>Loading</div>}>
Expand All @@ -106,9 +110,9 @@ describe('ReactDOMFizzServer', () => {
writable,
);

await waitUntilClose(writable);
await completed;

expect(writable.error).toBe(undefined);
expect(writable.result).toContain('Loading');
expect(output.error).toBe(undefined);
expect(output.result).toContain('Loading');
});
});

0 comments on commit 91d2ed0

Please sign in to comment.