TEST: Add a NS2 test (finally) (#458)

This commit is contained in:
David Walker
2023-04-01 04:45:23 -07:00
committed by GitHub
parent c730d6ed82
commit 8c4b992d59
4 changed files with 78 additions and 20 deletions
+17 -6
View File
@@ -18,6 +18,22 @@ function makeScriptBlob(code: string): Blob {
return new Blob([code], { type: "text/javascript" });
}
// Webpack likes to turn the import into a require, which sort of
// but not really behaves like import. So we use a "magic comment"
// to disable that and leave it as a dynamic import.
//
// However, we need to be able to replace this implementation in tests. Ideally
// it would be fine, but Jest causes segfaults when using dynamic import: see
// https://github.com/nodejs/node/issues/35889 and
// https://github.com/facebook/jest/issues/11438
// import() is not a function, so it can't be replaced. We need this separate
// config object to provide a hook point.
export const config = {
doImport(url: string): Promise<ScriptModule> {
return import(/*webpackIgnore:true*/ url);
},
};
export async function compile(script: Script, scripts: Script[]): Promise<ScriptModule> {
//!shouldCompile ensures that script.module is non-null, hence the "as".
if (!shouldCompile(script, scripts)) return script.module as Promise<ScriptModule>;
@@ -36,12 +52,7 @@ export async function compile(script: Script, scripts: Script[]): Promise<Script
script.url = uurls[uurls.length - 1].url;
// The URL at the top is the one we want to import. It will
// recursively import all the other modules in the urlStack.
//
// Webpack likes to turn the import into a require, which sort of
// but not really behaves like import. Particularly, it cannot
// load fully dynamic content. So we hide the import from webpack
// by placing it inside an eval call.
script.module = new Promise((resolve) => resolve(eval("import(uurls[uurls.length - 1].url)")));
script.module = config.doImport(uurls[uurls.length - 1].url);
script.dependencies = uurls;
return script.module;
}