dequelabs / axe-webdriverjs

Provides a chainable axe API for Selenium's WebDriverJS and automatically injects into all frames.
Mozilla Public License 2.0
130 stars 46 forks source link

Module objects cannot be functions (should use a named export) #74

Closed JoshuaKGoldberg closed 5 years ago

JoshuaKGoldberg commented 6 years ago

This is not valid JavaScript:

import * as AxeBuilder from "axe-webdriverjs";

AxeBuilder(browser);

See this SO answer:

An ES6 module namespace object cannot be invoked as a function or with new.

...so what should happen instead is:

import { AxeBuilder } from "axe-webdriverjs";

AxeBuilder(browser);

🙃.

Fortunately, this can be a purely additive change: somewhere in lib/index.js, you can add:

AxeBuilder.AxeBuilder = AxeBuilder;

...so that both the CommonJS-style const AxeBuilder = require("axe-webdriverjs"); and ES-style above or const AxeBuilder = require("axe-webdriverjs").AxeBuilder; will work.

stephenmathieson commented 6 years ago

I think I'm missing context here. Why are you importing this package that way?

JoshuaKGoldberg commented 6 years ago

@stephenmathieson sorry, I don't follow your question. Are you wondering why I'm importing the module like import { AxeBuilder } from "axe-webdriverjs" instead of import * as AxeBuilder from "axe-webdriverjs"? If that's what you intend to ask, it's because the spec for modules explicitly doesn't allow it. An ES6 module namespace object cannot be invoked as a function or with new.

stephenmathieson commented 6 years ago

@JoshuaKGoldberg right... We don't strictly support ES modules here. Instead, we use Node's funky interpretation of CommonJS (where a function/class can take the place of the pseudo "default" export). This was the standard for years, and is still the way many packages are written to date. I think you're probably looking for a TypeScript (or alike) compatible export, which would require us to provide AxeBuilder on exports.default. I'm happy to make this change and can do so tomorrow.

On a side note, wouldn't import Builder = require('axe-builder') work?