gemini-testing / testplane

Testplane (ex-hermione) browser test runner based on mocha and wdio
https://testplane.io
MIT License
720 stars 63 forks source link

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

Closed DudaGod closed 1 month ago

DudaGod commented 1 month ago

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.