Core: Fix the exports setup to make bundlers work with ESM & CommonJS
[jquery.git] / test / bundler_smoke_tests / run-jsdom-tests.js
blobba7100c44e6fd580a37426ccbff0e3b1cb02c00e
1 import fs from "node:fs/promises";
2 import jsdom, { JSDOM } from "jsdom";
3 import path from "node:path";
4 import { fileURLToPath } from "node:url";
5 import { runRollupEsmAndCommonJs, runRollupPureEsm } from "./lib/run-rollup.js";
6 import { runWebpack } from "./lib/run-webpack.js";
7 import { cleanTmpBundlersDir } from "./lib/utils.js";
9 const dirname = path.dirname( fileURLToPath( import.meta.url ) );
11 async function runJSDOMTest( { title, folder } ) {
12         console.log( "Running bundlers tests:", title );
14         const template = await fs.readFile( `${ dirname }/test.html`, "utf-8" );
15         const scriptSource = await fs.readFile(
16                 `${ dirname }/tmp/${ folder }/main.js`, "utf-8" );
18         const html = template
19                 .replace( /@TITLE\b/, () => title )
20                 .replace( /@SCRIPT\b/, () => scriptSource );
22         const virtualConsole = new jsdom.VirtualConsole();
23         virtualConsole.sendTo( console );
24         virtualConsole.on( "assert", ( success, message ) => {
25                 if ( !success ) {
26                         process.exitCode = 1;
27                 }
28         } );
30         new JSDOM( html, {
31                 resources: "usable",
32                 runScripts: "dangerously",
33                 virtualConsole
34         } );
36         if ( process.exitCode === 0 || process.exitCode == null ) {
37                 console.log( "Bundlers tests passed for:", title );
38         } else {
39                 console.error( "Bundlers tests failed for:", title );
40         }
43 async function buildAndTest() {
44         await cleanTmpBundlersDir();
46         await Promise.all( [
47                 runRollupPureEsm(),
48                 runRollupEsmAndCommonJs(),
49                 runWebpack()
50         ] );
52         await Promise.all( [
53                 runJSDOMTest( {
54                         title: "Rollup with pure ESM setup",
55                         folder: "rollup-pure-esm"
56                 } ),
58                 runJSDOMTest( {
59                         title: "Rollup with ESM + CommonJS",
60                         folder: "rollup-commonjs"
61                 } ),
63                 runJSDOMTest( {
64                         title: "Webpack",
65                         folder: "webpack"
66                 } )
67         ] );
69         // The directory won't be cleaned in case of failures; this may aid debugging.
70         await cleanTmpBundlersDir();
73 await buildAndTest();