testplane icon indicating copy to clipboard operation
testplane copied to clipboard

fix(component-testing): ability to import esm modules

Open DudaGod opened this issue 4 months ago • 0 comments

What is done

In component testing user can import esm modules in the test file, because it executes in the browser, which support them. But test files also import in nodejs environment which supports only cjs. Because of this all esm only modules throw ERR_REQUIRE_ESM error in nodejs env.

So i implement replacing import esm module to variable with proxy. Like this:

// From:
import esmPkg from "esm-module";
// To:
const esmPkg = new Proxy({}, {
    get: function (target, prop) {
        return prop in target ? target[prop] : new Proxy(() => {}, this);
    },
    apply: function () {
        return new Proxy(() => {}, this);
    }
});

This proxy gives ability to call every method or getter that user want.

Need review only second commit.

DudaGod avatar Oct 09 '24 22:10 DudaGod