0
votes

I'm seeing a strange interaction when writing unit tests for services in ember.js. It only occurs on arrays.

I'm having a service which has an array as property. The state of array is shared between different tests. I don't see this behavior if property has a simple type (e.g. number). Do you have any hint what's going on?

Here is a ember-twiddle demonstrating the issue: https://ember-twiddle.com/068e0c0fd7240e54c98972002fc2e34f?openFiles=tests.unit.services.my-service-test.js%2C

Update: I noticed it works fine if I'm initialize the array in init() but not if I declare it as property of service object. Are complex types like array and object sharing states between different instances?

So this won't work:

import Ember from 'ember';
export default Ember.Service.extend({
  array: [],
});

Will this is working as expected:

import Ember from 'ember';
export default Ember.Service.extend({
  array: undefined,
  init() {
    this.set('array', []);
  }
});
1

1 Answers

0
votes

Arrays and objects defined directly on any Ember.Object are shared across all instances of that object.

Section from the guide.

You need to define them in your init function. If you don't they share the same object.