diff --git a/packages/core/src/interpreter.ts b/packages/core/src/interpreter.ts index f69b9aac65..fc399f6ea6 100644 --- a/packages/core/src/interpreter.ts +++ b/packages/core/src/interpreter.ts @@ -718,9 +718,7 @@ export class Interpreter< if ( _event.name.indexOf(actionTypes.errorPlatform) === 0 && - !this.state.nextEvents.some( - (nextEvent) => nextEvent.indexOf(actionTypes.errorPlatform) === 0 - ) + !this.state.can(_event as any) ) { throw (_event.data as any).data; } diff --git a/packages/core/test/invoke.test.ts b/packages/core/test/invoke.test.ts index 7fab279f59..ce9558cbb8 100644 --- a/packages/core/test/invoke.test.ts +++ b/packages/core/test/invoke.test.ts @@ -1770,8 +1770,6 @@ describe('invoke', () => { }); it('should call onError only on the state which has invoked failed service', () => { - let errorHandlersCalled = 0; - const errorMachine = Machine({ initial: 'start', states: { @@ -1784,44 +1782,48 @@ describe('invoke', () => { type: 'parallel', states: { first: { - invoke: { - src: () => () => { - throw new Error('test'); - }, - onError: { - target: 'failed', - cond: () => { - errorHandlersCalled++; - return false; + initial: 'waiting', + states: { + waiting: { + invoke: { + src: () => () => { + throw new Error('test'); + }, + onError: { + target: 'failed' + } } - } + }, + failed: {} } }, second: { - invoke: { - src: () => () => { - // empty - }, - onError: { - target: 'failed', - cond: () => { - errorHandlersCalled++; - return false; + initial: 'waiting', + states: { + waiting: { + invoke: { + src: () => () => { + // empty + }, + onError: { + target: 'failed' + } } - } + }, + failed: {} } - }, - failed: { - type: 'final' } } } } }); - interpret(errorMachine).start().send('FETCH'); + const service = interpret(errorMachine).start(); + service.send('FETCH'); - expect(errorHandlersCalled).toEqual(1); + expect(service.state.value).toEqual({ + fetch: { first: 'failed', second: 'waiting' } + }); }); it('should be able to be stringified', () => { @@ -2641,6 +2643,33 @@ describe('invoke', () => { }) .start(); }); + + it('a wildcard transition should be able to receive an error event', (done) => { + const machine = createMachine({ + initial: 'fetching', + states: { + fetching: { + invoke: { + src: () => Promise.reject(new Error('test err')), + onDone: { + target: 'complete' + } + }, + on: { + '*': { + actions: () => { + done(); + } + } + } + }, + complete: { + type: 'final' + } + } + }); + interpret(machine).start(); + }); }); it('invoke `src` should accept invoke source definition', (done) => {