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

Does not work with other plugins when chai loaded multiple times #70

Closed
jreichenberg opened this issue Nov 13, 2014 · 3 comments
Closed

Comments

@jreichenberg
Copy link

Hi,

I've created my own chai plugin with custom assertions. In order to get it working with chai-as-promised I found I needed to load my custom plugin first in my test spec (Mocha via NodeJs) and then chai-as-promised:

var chai = require('chai'),
    myCustomAssertions = require('...');

chai.use(myCustomAssertions);
chai.use(require('chai-as-promised'));

The above works fine if its the only spec running. If I switch the order of the use statements then chai-as-promised does not hook my custom assertions and they get fed unresolved promises.

The problem is that if I load multiple spec files, and the ones loaded first use chai-as-promised but not my custom assertions, then chai.use() does nothing as the chai-as-promised library has already done its stuff and my custom assertions are no longer hooked and modified by chai-as-promised.

Spec 1 in Node/Mocha process:

var chai = require('chai');
chai.use(require('chai-as-promised'));

Spec 2 in same Node/Mocha process:

var chai = require('chai'),
    myCustomAssertions = require('...');

chai.use(myCustomAssertions);
chai.use(require('chai-as-promised'));

Suggestions? Make sense?

@domenic
Copy link
Collaborator

domenic commented Nov 13, 2014

This is working as intended. Generally you should only add a plugin once in a given Node program.

@jreichenberg
Copy link
Author

Thanks for the reply, and makes sense and agree in general...and chai itself maintains a list of already added plugins to make subsequent use() calls on the same plugin a no-op. So I get your point and we can close out this issue.

However, chai-as-promised seems a little different from other plugins in that it modifies the behavior of existing assertions.

I wonder if anyone using this project can suggest a best-practice for a test spec that has chai-as-promised + another plugin and can be run on its own and with other test specs...all the options I can think of are inelegant. It makes sense to me for each test spec to require the plugins it uses rather than some wrapping thing have fore-knowledge of what chai plugins would be needed in the aggregate.

@Atlas7
Copy link

Atlas7 commented Nov 14, 2017

chai.use plugin newbie here. Say if I want to use the following two plugins together, is that possible / discouraged?

chai.use(chaiAsPromised)
chai.use(chaiImmutable)

I haven't really tried it yet but on top of my mind what if in a Mocha test, we have async functions (that requires chaiAsPromised), and immutable data structures (that requires chaiImmutable)?

Apologies if this question sounds very dumb - in advance!

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

No branches or pull requests

3 participants