javascriptjsonnode.jsamazon-web-servicesshould.js

How would I go about deleting a particular field within a JSON for a test, then reloading the JSON in another test without caching the delete?


Okay, so here's my issue:

it('should fail when Sns is not found within record', (done) => {
const policy = require('../main/nacl-002-handler.js');
const eventData = require('./data/event.json');
delete eventData.Records[0].Sns;

policy.handler(eventData, {}, (err, data) => {
  err.should.equal(`No Sns field was found within ${eventData.Records[0]}`);
  should.not.exist(data);
  done();
 });
});

I want to remove particular fields for testing here, and this works fine. But the issue arrises when I try to reload the same module in the next test. When eventData is reloaded, it is missing the field I deleted in the previous test. I think this is due to how Node caches modules but I'd like a way to reload the data entirely in each subsequent test. So in this case, I'd like the Sns field of eventData.Records[0] to be untouched when I operationalize it in any subsequent test(s).

I have "re-required" the data in subsequent tests but the Sns field is still missing/deleted.


Solution

  • You could clone the eventData object immediately after it's loaded so you always have a clean copy available:

    var eventDataCLEAN = JSON.parse(JSON.stringify(eventData));
    

    Then whenever you need a new clean copy, just do this:

    eventData = eventDataCLEAN;