diff --git a/index.js b/index.js index cd4a5ae..597594f 100644 --- a/index.js +++ b/index.js @@ -216,7 +216,12 @@ class Emittery { if (!this.debug.logger) { this.debug.logger = (type, debugName, eventName, eventData) => { - eventData = JSON.stringify(eventData); + try { + // TODO: Use https://github.com/sindresorhus/safe-stringify when the package is more mature. Just copy-paste the code. + eventData = JSON.stringify(eventData); + } catch { + eventData = `Object with the following keys failed to stringify: ${Object.keys(eventData).join(',')}`; + } if (typeof eventName === 'symbol') { eventName = eventName.toString(); diff --git a/test/index.js b/test/index.js index f37fb2b..c646624 100644 --- a/test/index.js +++ b/test/index.js @@ -1210,3 +1210,17 @@ test('isDebug can be turned on for and instance without using the constructor', t.is(eventStore[2].debugName, 'testEmitter'); t.is(eventStore[2].eventData, 'test data'); }); + +test('debug mode - handles circular references in event data', async t => { + const emitter = new Emittery({ + debug: { + name: 'testEmitter', + enabled: true + } + }); + + const data = {}; + data.circular = data; + + await t.notThrowsAsync(emitter.emit('test', data)); +});