I need for my Cypress tests two slightly differed sets of data. As the dataset isn't short and the variation deviates from the original only slightly - I thought to just duplicate the object and mutate it the way I need it. But well, variables in ES6 Imports are bindings so mutating the copy results in mutating the original as well.
To demonstrate this behavior:
//test.json
{
"foo":{
"bar":"XXX"
}
}
import test from './test.json'
const firstTest = {...test}
const secondTest = {...test}
secondTest.foo.bar = '@@@'
console.log(firstTest.foo.bar)
// Output: @@@
Spread operator obviously doesn't work there as well as Object.assign()
What works for me to get rid of the bindings is:
import test from './test.json'
const firstTest = JSON.parse(JSON.stringify(test)) //<---
const secondTest = JSON.parse(JSON.stringify(test)) //<---
secondTest.foo.bar = '@@@'
console.log(firstTest.foo.bar)
// Output: XXX
Is there a better way to achieve this or just get rid of the bindings in a different way?