CCNA_Autofill/test/fetch.test.js
Snoweuph 83359af04a
All checks were successful
QS / QS (push) Successful in 7s
Moved to Template
2024-12-21 16:09:09 +01:00

32 lines
989 B
JavaScript

const fs = require('fs');
const vm = require('vm');
const { JSDOM } = require('jsdom');
const { diff } = require('deep-diff');
const scriptContent = fs.readFileSync('../src/fetch.js', 'utf8');
/**
* @param testData {string}
* @param expectedData {Array<Answer>}
* @returns {Promise<Array<Answer>>}
*/
module.exports = async function executeFetchTest(testData, expectedData) {
const sandbox = {
window: {},
DOMParser: new JSDOM().window.DOMParser,
console: console
};
vm.createContext(sandbox);
vm.runInContext(scriptContent, sandbox);
const { parseAnswers } = sandbox;
return new Promise((resolve, reject) => {
parseAnswers({ responseText: testData }, results => {
const { diff } = require('deep-diff');
const changes = diff({ data: results }, { data: expectedData });
if (changes !== undefined) {
reject(changes);
}
resolve();
});
});
};