Skip to content

Cannot call use inside use any more #1638

Closed

Description

@tpluscode

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;
};

Activity

  1. 43081j commented on Sep 25, 2024

    @43081j
    Contributor

    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

  2. tpluscode commented on Sep 25, 2024

    @tpluscode
    ContributorAuthor
    /** 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

  3. 43081j commented on Sep 25, 2024

    @43081j
    Contributor

    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

  4. tpluscode commented on Sep 25, 2024

    @tpluscode
    ContributorAuthor

    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

  5. 43081j commented on Sep 25, 2024

    @43081j
    Contributor

    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

  6. tpluscode commented on Sep 25, 2024

    @tpluscode
    ContributorAuthor

    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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions