Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot call use inside use any more #1638

Open
tpluscode opened this issue Sep 25, 2024 · 6 comments · May be fixed by #1639
Open

Cannot call use inside use any more #1638

tpluscode opened this issue Sep 25, 2024 · 6 comments · May be fixed by #1639

Comments

@tpluscode
Copy link

tpluscode commented Sep 25, 2024

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;
};
@43081j
Copy link
Contributor

43081j commented Sep 25, 2024

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

@tpluscode
Copy link
Author

tpluscode commented Sep 25, 2024

/** 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

@43081j
Copy link
Contributor

43081j commented Sep 25, 2024

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

@tpluscode
Copy link
Author

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

@43081j
Copy link
Contributor

43081j commented Sep 25, 2024

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

@tpluscode
Copy link
Author

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
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants