Repository: sindresorhus/serialize-error (js)

Issue:
Enumerable properties are made non-enumerable
Enumerable properties are made non-enumerable after deserialization.

```typescript
import { deserializeError, serializeError } from 'serialize-error';

class MyError extends Error {
  constructor(public params: any) {
    super('test');
  }
}

const error = new MyError({ name: 'foo' });
const serialized = serializeError(error);
const deserialized = deserializeError(serialized) as MyError;

// properties of deserialized.params are non-enumerable
```

Minimal reproduction: https://stackblitz.com/edit/typescript-dkbp39?file=index.ts

Pull request description:
Stops making non-Error properties non-enumerable
- Fixes #84 
- Related to https://github.com/sindresorhus/serialize-error/pull/101
- Blocked by #100 

`message`, `stack`, `cause` were being made non-enumerable by `deserialize` even when they appeared in objects nested in the main error, like:

```js
const serialized = {
	message: 'error message',
	stack: 'at <anonymous>:1:13',
	name: 'name',
	unrelated: {
		stack: 'Not an error',
		cause: 'Wasn’t me',
	},
};
deserializeError(serialized);
``` 

Patch:
diff --git a/index.js b/index.js
index 525ec3b..376711a 100644
--- a/index.js
+++ b/index.js
@@ -16,7 +16,7 @@ export class NonError extends Error {
 	}
 }
 
-const commonProperties = [
+const errorProperties = [
 	{
 		property: 'name',
 		enumerable: false,
@@ -127,14 +127,16 @@ const destroyCircular = ({
 		to[key] = '[Circular]';
 	}
 
-	for (const {property, enumerable} of commonProperties) {
-		if (from[property] !== undefined && from[property] !== null) {
-			Object.defineProperty(to, property, {
-				value: isErrorLike(from[property]) ? continueDestroyCircular(from[property]) : from[property],
-				enumerable: forceEnumerable ? true : enumerable,
-				configurable: true,
-				writable: true,
-			});
+	if (serialize || to instanceof Error) {
+		for (const {property, enumerable} of errorProperties) {
+			if (from[property] !== undefined && from[property] !== null) {
+				Object.defineProperty(to, property, {
+					value: isErrorLike(from[property]) ? continueDestroyCircular(from[property]) : from[property],
+					enumerable: forceEnumerable ? true : enumerable,
+					configurable: true,
+					writable: true,
+				});
+			}
 		}
 	}
 


Test patch:
diff --git a/test.js b/test.js
index 330718f..2da916f 100644
--- a/test.js
+++ b/test.js
@@ -279,7 +279,7 @@ for (const property of ['cause', 'any']) {
 	});
 }
 
-test('deserialized name, stack, cause and message should not be enumerable, other props should be', t => {
+test('deserialized Error class properties should not be enumerable, other props should be', t => {
 	const object = {
 		message: 'error message',
 		stack: 'at <anonymous>:1:13',
@@ -297,6 +297,10 @@ test('deserialized name, stack, cause and message should not be enumerable, othe
 		errno: 1,
 		syscall: 'syscall',
 		randomProperty: 'random',
+		notAnError: {
+			stack: 'Not an error',
+			cause: 'Wasn’t me',
+		},
 	};
 
 	const deserialized = deserializeError({...object, ...enumerables});
@@ -305,6 +309,11 @@ test('deserialized name, stack, cause and message should not be enumerable, othe
 		Object.keys(enumerables),
 		Object.keys(deserialized),
 	);
+
+	t.deepEqual(
+		Object.keys(enumerables.notAnError),
+		Object.keys(deserialized.notAnError),
+	);
 });
 
 test('should deserialize properties up to `Options.maxDepth` levels deep', t => {