Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for webpack + ts #15

Merged
merged 1 commit into from
Nov 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
"scripts": {
"build": "gulp build",
"start": "gulp",
"prepack": "gulp build"
"prepack": "gulp build ; npm run test-npm",
"test-npm": "node test-npm/run-tests.js"
},
"browserslist": "> 0.25%, not dead",
"babel": {
Expand Down
92 changes: 92 additions & 0 deletions test-npm/helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// @ts-check

const fs = require("fs");
const cp = require("child_process");

/**
* @param {string} dir
*/
exports.removeDir = async (dir) => {
await fs.promises.rm(dir, { recursive: true, force: true });
}

/**
* @param {string} command
* @param {string[]} args
* @param {string} cwd
*/
exports.spawnSimple = async (command, args, cwd) => {
const { error, status } = cp.spawnSync(command, args, {
stdio: ["inherit", "inherit", "inherit"],
cwd: cwd,
});
if (error) throw error;
if (status !== 0) throw new Error(`Process exited with ${status}`);
}

/**
* @param {typeof import("jsdom")} jsdom
* @param {string} file
* @returns {Promise<{dom: import("jsdom").JSDOM, virtualConsole: import("jsdom").VirtualConsole}>}
*/
exports.loadHtml = async (jsdom, file) => {
const { JSDOM, VirtualConsole } = jsdom;

/** @type {Error[]} */
const errors = [];

const virtualConsole = new VirtualConsole({ captureRejections: true });

virtualConsole.on("debug", (...args) => {
console.debug(...args);
});

virtualConsole.on("log", (...args) => {
console.log(...args);
});

virtualConsole.on("info", (...args) => {
console.info(...args);
});

virtualConsole.on("warn", (...args) => {
console.warn(...args);
});

virtualConsole.on("error", (...args) => {
console.error(...args);
errors.push(new Error(args.join(" ")));
});

return new Promise((resolve, reject) => {
/** @type {{dom: import("jsdom").JSDOM | undefined, loaded: boolean}} */
const data = { dom: undefined, loaded: false };
const check = () => {
if (errors.length > 0) {
reject(errors[0]);
}
if (data.dom !== undefined && (data.dom.window.document.readyState === "complete" || data.loaded)) {
resolve({ dom: data.dom, virtualConsole });
}
};
const domPromise = JSDOM.fromFile(file, {
beforeParse: win => win.addEventListener("error", event => {
console.error(event.message, event.error);
errors.push(event.error instanceof Error ? event.error : new Error(String(event.message || event.error)));
}),
pretendToBeVisual: true,
resources: "usable",
runScripts: "dangerously",
url: `file://${file}`,
virtualConsole,
});
domPromise.then(dom => {
data.dom = dom;
dom.window.addEventListener("load", () => {
data.loaded = true;
check();
});
check();
});
});
}
8 changes: 8 additions & 0 deletions test-npm/run-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
async function main() {
await require("./webpack-ts/run-test.js").main();
}

main().catch(e => {
console.error("Error while running test", e);
process.exit(1);
});
22 changes: 22 additions & 0 deletions test-npm/webpack-ts/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<title></title>
<meta name="author" content="">
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>

<body>
<p>Hello, world!</p>
<p>Hello, world!</p>
<p>Hello, world!</p>

<input type="text" id="coloris">

<script src="./dist/main.js"></script>
</body>

</html>
Loading