Skip to content

Commit

Permalink
hacking node.js QUnit restart runs
Browse files Browse the repository at this point in the history
  • Loading branch information
izelnakri committed Apr 19, 2021
1 parent f0a83fc commit eaefae1
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 36 deletions.
36 changes: 10 additions & 26 deletions lib/commands/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ import { fileURLToPath } from 'url';
import runTestsInNode from './run/tests-in-node.js';
import runTestsInBrowser from './run/tests-in-browser.js';
import fileWatcher from '../setup/file-watcher.js';
import setupNodeJSEnvironment from '../setup/node-js-environment.js';
import WebSocket from 'ws';
// let tree = Object.assign(tree, directoryReader(config, inputs, (file) => {
// import(`${file}`);
// }));
// TODO: make it run an in-memory html if not exists
import clonedeep from 'lodash.clonedeep';
// TODO: make it run an in-memory html if not exists: let tree = Object.assign(tree, directoryReader(config, inputs, (file) => import(`${file}`)));

const __dirname = dirname(fileURLToPath(import.meta.url));

Expand All @@ -24,8 +21,7 @@ export default async function(config) {
let fileName = config.htmlPaths[index];
let html = htmlBuffers[index].toString();

// TODO: here I could do html analysis to see which static js certain html points to? Complex algorithm
if (!html.includes('{{content}}')) {
if (!html.includes('{{content}}')) { // TODO: here I could do html analysis to see which static js certain html points to? Complex algorithm
result.staticHTMLs[fileName] = html;
} else {
result.dynamicContentHTMLs[fileName] = html;
Expand Down Expand Up @@ -55,31 +51,30 @@ export default async function(config) {
}
}

const { server, page, WebSocketServer, browser } = await runTestsInBrowser(config.fileOrFolderInputs, config, cachedContent); // NOTE: cannot do test files individually/requires bundling?
const { server, page, WebSocketServer, browser } = await runTestsInBrowser(config.fileOrFolderInputs, config, cachedContent);

if (config.watch) {
logWatcherAndKeyboardShortcutInfo();

await fileWatcher(config.fileOrFolderInputs, async (file) => {
config.COUNTER = { testCount: 0, failCount: 0, skipCount: 0, passCount: 0 };
// TODO: check if its html then run html otherwise run all html, how??
await runTestsInBrowser([file], config, cachedContent, { server, page, WebSocketServer, browser })
await runTestsInBrowser([file], config, cachedContent, { server, page, WebSocketServer, browser }) // TODO: check if its html then run html otherwise run all html, how??
}, () => {}, config);
}
} else {
global.testTimeout = config.timeout;

await setupNodeJSEnvironment(config); // NOTE = (await import('../setup/node-js-environment.js')).default(config);
await runTestsInNode(config.fileOrFolderInputs, window.QUnit, config); // NOTE: can do test files individually
(await import(`${__dirname}/../setup/node-js-environment.js`)).default(config);

if (config.watch) {
await runTestsInNode(config.fileOrFolderInputs, config); // NOTE: can do test files individually

if (config.watch) {
logWatcherAndKeyboardShortcutInfo();

let something = await fileWatcher(config.fileOrFolderInputs, async (file) => {
config.COUNTER = { testCount: 0, failCount: 0, skipCount: 0, passCount: 0 };
await setupNodeJSEnvironment(config);
await runTestsInNode([file], window.QUnit, config)
// window.QUnit = clonedeep(QUnit);
await runTestsInNode([file], config)
}, () => {}, config);
}
}
Expand All @@ -88,14 +83,3 @@ export default async function(config) {
function logWatcherAndKeyboardShortcutInfo() {
console.log('# Watching files.. Press "qa" to run all the tests, "ql" to run last failing test'); // NOTE: maybe add also qx to exit
}

// Console.log(getEventColor(event), path.split(projectRoot)[1]);
function getEventColor(event) {
if (event === 'change') {
return chalk.yellow('CHANGED:');
} else if (event === 'add' || event === 'addDir') {
return chalk.green('ADDED:');
} else if (event === 'unlink' || event === 'unlinkDir') {
return chalk.red('REMOVED:');
}
}
35 changes: 29 additions & 6 deletions lib/commands/run/tests-in-node.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,39 @@
import clonedeep from 'lodash.clonedeep';
import parseFsInputs from '../../utils/parse-fs-inputs.js';

export default async function runTestsInNode(fileOrFolderInputs, QUnit, config) {
window.QUnit.config = Object.assign({}, window.oldQunitConfig);
window.location = window.location;
export default async function runTestsInNode(fileOrFolderInputs, config) {
global.testTimeout = config.timeout;

// window.QUnit.config.autostart = false;
let oldQUnitConfig = clonedeep(window.QUnit.config);

window.QUnit.stop();
let resolvePromise;
let promise = new Promise((resolve, reject) => {
resolvePromise = resolve;
});
window.QUnit.done(() => resolvePromise());

const fsTree = await parseFsInputs(fileOrFolderInputs, {}, async(targetPath, fsEntry) => {
console.log('importing');
await import(targetPath);
console.log('imported');

fsEntry.executed = true;
}, config);
console.log('TAP version 13');

// globalStartCalled = false;
QUnit.start();
console.log('called start');
console.log('before start');
setTimeout(() => {
window.QUnit.stop();
window.QUnit.start()
}, 1000);
console.log('after start');

await promise;
window.QUnit.config = oldQUnitConfig;
// window.QUnit.config.autostart = false;
// window.QUnit.config.queue.length = 0;

console.log(window.QUnit.config);
}
6 changes: 4 additions & 2 deletions lib/setup/node-js-environment.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import QUnit from '../../index.js';
import yaml from 'js-yaml'
import clonedeep from 'lodash.clonedeep';
import indentString from 'indent-string';
import TAPDisplayFinalResult from '../tap/display-final-result.js';
import TAPDisplayTestResult from '../tap/display-test-result.js';

export default function setupNodeJSEnvironment(config) {
global.window = global;
window.QUnit = null;
window.QUnit = QUnit;
window.QUnit.config.autostart = false;
window.QUnit.config.testTimeout = global.testTimeout;
Expand All @@ -24,6 +24,7 @@ export default function setupNodeJSEnvironment(config) {
TAPDisplayTestResult(config.COUNTER, details)
});
window.QUnit.done((details) => {
console.log('done call');
window.QUNIT_RESULT = Object.assign(details, {
timeTaken: window.QUNIX_TEST_TIME_COUNTER.stop()
});
Expand All @@ -35,7 +36,8 @@ export default function setupNodeJSEnvironment(config) {
});

process.on('uncaughtException', (err) => console.error(err));
window.oldQunitConfig = Object.assign({}, window.QUnit.config);
// global.oldQUnitConfig = global.oldQUnitConfig || Map(window.QUnit.config).toJS();
// window.QUnit.config = Map(global.oldQUnitConfig).toJS();

return window.QUnit;
}
3 changes: 2 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@
"js-yaml": "^3.14.1",
"klaw": "^3.0.0",
"kleur": "^4.1.4",
"lodash.clonedeep": "^4.5.0",
"puppeteer": "^8.0.0",
"qunit": "^2.14.1",
"qunit": "^2.15.0",
"through2": "^4.0.2",
"ws": "^7.4.4"
},
Expand Down

0 comments on commit eaefae1

Please sign in to comment.