chai icon indicating copy to clipboard operation
chai copied to clipboard

Cannot call use inside use any more

Open tpluscode opened this issue 1 year ago • 6 comments

I found that in v5 this is no longer possible

/** type {ChaiPlugin} */
export default function(chai, utils) {
  chai.use(anotherPlugin)
}

use is no longer included in the argument. Can we add it to match what was possible with v4?

export function use(fn) {
  const exports = {
+   use,
    AssertionError,
    util,
    config,
    expect,
    assert,
    Assertion,
    ...should
  };

  if (!~used.indexOf(fn)) {
    fn(exports, util);
    used.push(fn);
  }

  return exports;
};

tpluscode avatar Sep 25 '24 07:09 tpluscode

Probably makes sense to avoid weird situations with multiple versions of chai (since if you didn't care about that, you would just import use?)

Let's see what @keithamus or @koddsson thinks though

43081j avatar Sep 25 '24 07:09 43081j

/** type {ChaiPlugin} */
export default async function(chai, utils) {
  const { use } = await import('chai')
  use(jestSnapshotPlugin())
}

This appears to work but I'm not sure if making the plugin function async is exactly supported. And there may always be the risk of importing wrong chai in case of duplicate resolutions

tpluscode avatar Sep 25 '24 08:09 tpluscode

Yep, it would be a lot cleaner if you wrote your plugin as esm of course too

I think you're right though and we should add this

43081j avatar Sep 25 '24 08:09 43081j

The plugin is esm. But it includes another to use the extra assertions and I called use inside so that end user does not have to

tpluscode avatar Sep 25 '24 08:09 tpluscode

What I mean is if you did import use, you could just do it normally in a regular import statement (rather than a dynamic import)

That would be pretty clean code but wouldn't work well if for some reason there's two chai running at once

43081j avatar Sep 25 '24 08:09 43081j

Ah, gotcha. Of course that's an option but I try to avoid adding chai as a dependency to plugins. Especially now, that plugins should work no problem with both v4 and v5

tpluscode avatar Sep 25 '24 08:09 tpluscode