Skip to content

Commit

Permalink
Merge pull request #1722 from davidkpiano/davidkpiano/1718
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkpiano authored Dec 14, 2020
2 parents 7aad3c6 + 1257187 commit 4c342de
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/purple-papayas-begin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@xstate/inspect': minor
---

The `@xstate/inspect` tool now uses [`fast-safe-stringify`](https://www.npmjs.com/package/fast-safe-stringify) for internal JSON stringification of machines, states, and events when regular `JSON.stringify()` fails (e.g., due to circular structures).
41 changes: 40 additions & 1 deletion packages/xstate-inspect/test/inspect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createMachine, interpret } from 'xstate';
import { createDevTools, inspect } from '../src';

describe('@xstate/inspect', () => {
it('should handle circular structures', (done) => {
it('should handle circular structures in context', (done) => {
const circularStructure = {
get cycle() {
return circularStructure;
Expand Down Expand Up @@ -38,4 +38,43 @@ describe('@xstate/inspect', () => {
// and will throw an error if circular structures are not handled.
expect(() => devTools.register(service)).not.toThrow();
});

it('should handle circular structures in events', (done) => {
const circularStructure = {
get cycle() {
return circularStructure;
}
};

const machine = createMachine({
initial: 'active',
states: {
active: {}
}
});

const devTools = createDevTools();

devTools.onRegister((inspectedService) => {
inspectedService.onTransition((state) => {
if (state.event.type === 'CIRCULAR') {
done();
}
});
});

inspect({
iframe: false,
devTools
});

const service = interpret(machine).start();

service.send({
type: 'CIRCULAR',
value: circularStructure
});

expect(() => devTools.register(service)).not.toThrow();
});
});

0 comments on commit 4c342de

Please sign in to comment.