1
1
Fork 0
CCNA_Autofill_Userscript/test/fetch.test.js
Snoweuph 47b5bd601c
All checks were successful
QS / Javadoc (push) Successful in 8s
Setup Testing
2024-05-25 12:05:23 +02: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();
});
});
};