aboutsummaryrefslogtreecommitdiffstats
path: root/test/bundler_smoke_tests/run-jsdom-tests.js
blob: ba7100c44e6fd580a37426ccbff0e3b1cb02c00e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import fs from "node:fs/promises";
import jsdom, { JSDOM } from "jsdom";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { runRollupEsmAndCommonJs, runRollupPureEsm } from "./lib/run-rollup.js";
import { runWebpack } from "./lib/run-webpack.js";
import { cleanTmpBundlersDir } from "./lib/utils.js";

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

async function runJSDOMTest( { title, folder } ) {
	console.log( "Running bundlers tests:", title );

	const template = await fs.readFile( `${ dirname }/test.html`, "utf-8" );
	const scriptSource = await fs.readFile(
		`${ dirname }/tmp/${ folder }/main.js`, "utf-8" );

	const html = template
		.replace( /@TITLE\b/, () => title )
		.replace( /@SCRIPT\b/, () => scriptSource );

	const virtualConsole = new jsdom.VirtualConsole();
	virtualConsole.sendTo( console );
	virtualConsole.on( "assert", ( success, message ) => {
		if ( !success ) {
			process.exitCode = 1;
		}
	} );

	new JSDOM( html, {
		resources: "usable",
		runScripts: "dangerously",
		virtualConsole
	} );

	if ( process.exitCode === 0 || process.exitCode == null ) {
		console.log( "Bundlers tests passed for:", title );
	} else {
		console.error( "Bundlers tests failed for:", title );
	}
}

async function buildAndTest() {
	await cleanTmpBundlersDir();

	await Promise.all( [
		runRollupPureEsm(),
		runRollupEsmAndCommonJs(),
		runWebpack()
	] );

	await Promise.all( [
		runJSDOMTest( {
			title: "Rollup with pure ESM setup",
			folder: "rollup-pure-esm"
		} ),

		runJSDOMTest( {
			title: "Rollup with ESM + CommonJS",
			folder: "rollup-commonjs"
		} ),

		runJSDOMTest( {
			title: "Webpack",
			folder: "webpack"
		} )
	] );

	// The directory won't be cleaned in case of failures; this may aid debugging.
	await cleanTmpBundlersDir();
}

await buildAndTest();