0

I am seeing unexpected results with the following Mocha tests, and I cannot figure out why. I have a bunch of objects from a web store that get translated into an Order object. I'm trying to run many relatively simple attribute comparisons, so I decided to loop over my tests.

import assert from 'assert'
import {expect} from 'chai'
...
const test_orders = [ <a bunch of objects> ]

function order_test(order, expected_values) {
    describe(`Order#new ${expected_values.expected_package_type}`, () => {
    ...
    })
}
for (var i = 0; i < test_orders.length; i++){
    let translated_order = {}, order = {};
    translated_order = new TranslateOrder(test_orders[i].untranslated_params)
    order = new Order(translated_order)
    order_test(order, test_orders[i]);
}

The problem is that I get different test results depending on the order in which the objects appear in test_orders, which tells me I am getting unexpected side effects. I just can't figure out where they are coming from. Any help would be much appreciated!

If the

translated_order = new TranslateOrder(test_orders[i].untranslated_params)
order = new Order(translated_order)

bit is the culprit, as I suspect, I would love to know what to do differently. It's a bit involved, but in one sentence, TranslateOrder creates an object from a JSON string, and Order is the model object that I use in my React app downstream.

I did try order = new Order(Object.assign({}, translated_order)) thinking that was the origin of the side effects, but the results were the same.

1 Answers1

0

It turns out there was a side effect inside my Order object. Inside the Order object, I was importing a data structure object and altering its values. I should have been making a copy of the data structure with var data = Object.assign({}, imported_data) instead of var data = imported_data.

Rookie mistake! If you've gotten here and you don't understand, see this question.