wswebcreation / protractor-image-comparison

npm-module to compare images with protractor
86 stars 38 forks source link

small fix for options parsing in _executeImageComparison #43

Closed Juravenator closed 6 years ago

Juravenator commented 6 years ago

In our testing setup, we encounter an error when running multiple tests with the same options object.

We get an error on this line: https://github.com/wswebcreation/protractor-image-comparison/blob/6fa7b194b1bb1d6ba37946af51b4252170185ed4/index.js#L354

The error states that 'push is not a function', this is because we encountered compareOptions.ignoreRectangles to be the number 1, rather than an array.

The problem lies in the fact that .push is used improperly. From the MDN docs:

The push() method adds one or more elements to the end of an array and returns the new length of the array.

The push method gives back the new array length instead of the array itself.
Replacing push with concat fixes this bug.

The following code demonstrates the bug:

let ignoreRectangles = [];

const compareOptions = {}

function doIt() {
  compareOptions.ignoreRectangles = 'ignoreRectangles' in compareOptions ? compareOptions.ignoreRectangles.push(ignoreRectangles) : ignoreRectangles;
  console.log(compareOptions);
}

doIt();
doIt();
$ node test.js
{ ignoreRectangles: [] }
{ ignoreRectangles: 1 }
wswebcreation commented 6 years ago

@Juravenator

Tnx for the PR, will merge it tomorrow and create a new release