chaijs / chai-factories

Factories over fixtures. Chai Assertion Library.
23 stars 2 forks source link

extend() does not recursively deep copy #5

Open agmcleod opened 8 years ago

agmcleod commented 8 years ago

Something that recently bit me, was we use a serialized json coloumn on a number of our backend models. So to replicate that in the front end, we have a number of factories like this:

chai.factory('user', {
  id: 1,
  name: 'user1',
  data: {},
});

The data column will then be used in various specs. Because of the way extend works, the collection map holds on to the default factory that contains a reference to data. As new factories are created, that same data in memory gets updated instead of replaced with a fresh object.

One potential solution is to update it to do a recursive clone, something like this: http://stackoverflow.com/a/728694/645132. Where it checks the typs, either does a return or creates a new object/array.