Skip to content

Commit

Permalink
Add failing test
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante committed Jan 3, 2025
1 parent 49c7a4f commit fe6c916
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import {Buffer} from 'node:buffer';
import Stream from 'node:stream';
import test from 'ava';
import errorConstructors from './error-constructors.js';
import {serializeError, deserializeError, isErrorLike} from './index.js';
import {
serializeError, deserializeError, isErrorLike, NonError,
} from './index.js';

function deserializeNonError(t, value) {
const deserialized = deserializeError(value);
Expand Down Expand Up @@ -349,6 +351,44 @@ test('should deserialize properties up to `Options.maxDepth` levels deep', t =>
t.deepEqual(levelThree, error);
});

test('should ignore invalid error-like objects', t => {
const errorLike = {
name: 'Error',
message: 'Some error message',
};

const nonErrorLike = {
name: 'Error',
message: (new class Message {}('Bottle')),
};

t.true(deserializeError(errorLike) instanceof Error);
t.true(deserializeError(nonErrorLike) instanceof NonError);
});

test('should ignore nested invalid error-like objects', t => {
const errorLike = {
message: 'Base',
nested: {
name: 'Error',
message: 'Some error message',
stack: 'at <anonymous>:1:13',
},
};

const nonErrorLike = {
message: 'Base',
nested: {
name: 'Error',
message: (new class Message {}('Bottle')),
stack: 'at <anonymous>:1:13',
},
};

t.true(deserializeError(errorLike).nested instanceof Error);
t.false(deserializeError(nonErrorLike).nested instanceof Error);
});

test('should serialize Date as ISO string', t => {
const date = {date: new Date(0)};
const serialized = serializeError(date);
Expand Down

0 comments on commit fe6c916

Please sign in to comment.