Multiple 'from' and 'to' states for a single event gives error in node cmd with jasmine. #88

NimishChandola opened this Issue Apr 14, 2016 · 5 comments


None yet

4 participants


Initializing the state machine like below works properly in browser but not in node console when writing jasmine unit test cases.
var fsm = StateMachine.create({
initial: 'hungry',
events: [
{ name: 'rest', from: ['hungry', 'satisfied', 'full', 'sick'], to: 'hungry' },

When i am creating a jasmine unit test and run it in node console then it throws error because
in console it generate "map" json object like {"hungry,satisfied,'full','sick'" : "hungry"}
and map[event].hasOwnProperty(this.current) is failing at line 79

If we check it in browser "map" object will be like
{'hungry':'hungry', 'satisfied':'hungry', 'full':'sick', 'sick':'hungry'} which is correct.


@jakesgordon Even i am also facing same problem while writing an unit for it using jasmine :(


@NimishChandola & @ArunKumar-MS

I also faced the same problem.

Then I changed the code as below

var fsm = StateMachine.create({
initial: 'hungry',
events: [
{name: 'rest', from: 'hungry', to: 'hungry' },
{name: 'rest', from: 'satisfied', to: 'hungry' },
{name: 'rest', from: 'full', to: 'hungry' },
{name: 'rest', from: 'sick', to: 'hungry' }

And unit test started working. But I am not sure is this a good idea to do that or not.

@jakesgordon : I believe for writing the unit test case we should not modify the actual implementation .. but I could not find any other way. Is this correct way to do this?


I debug code and get to know that when we use injectr module for unit testing then "instance of Array" is not returning true though there is an array and due to that for multiple 'to' , array generate like
[ ['hungry', 'satisfied', 'full', 'sick']]
check the below code at line 49
var from = (e.from instanceof Array) ? e.from : (e.from ? [e.from] : [StateMachine.WILDCARD]);

@NimishChandola NimishChandola added a commit to NimishChandola/javascript-state-machine that referenced this issue Apr 18, 2016
@NimishChandola NimishChandola Update state-machine.js
To fix the issue for injectr as explained in jakesgordon#88 i have updated the  state-machine.js and my code is running fine

The issue is coming due to injector module, but as part of defensive programming i have updated the condition so that due to some other library issue , our code should not break.

@jakesgordon jakesgordon added a commit that referenced this issue Nov 18, 2016
@jakesgordon Prefer es5 Array.isArray(obj) rather than problematic (obj instanceof…
… Array)

Fixes Issue #88 "Multiple 'from' and 'to' states for a single event gives error in node cmd with jasmine"
jakesgordon commented Nov 18, 2016 edited

This is fixed in (upcoming) v2.4

@jakesgordon jakesgordon added v2.4 and removed v3.0 labels Nov 18, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment