Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
chai-as-promised
Advanced tools
The chai-as-promised npm package extends Chai with a fluent language for asserting facts about promises. It allows developers to work with promises in their test assertions in a more natural and expressive way, which is particularly useful when dealing with asynchronous operations in tests.
Asserting a promise is fulfilled
This feature allows you to assert that a promise will be fulfilled. In other words, it checks that the promise will eventually resolve.
expect(promise).to.be.fulfilled;
Asserting a promise is rejected
This feature allows you to assert that a promise will be rejected. This is useful for testing error handling in asynchronous code.
expect(promise).to.be.rejected;
Asserting a promise is rejected with a specific error
With this feature, you can assert that a promise is not only rejected but also that it is rejected with a specific type of error.
expect(promise).to.be.rejectedWith(Error);
Asserting a promise's fulfillment value
This feature allows you to assert what value a promise will be resolved with. The 'eventually' chain is used to wait for the promise to resolve before making the assertion.
expect(promise).to.eventually.equal('foo');
Chaining assertions
chai-as-promised supports chaining assertions, allowing for more complex assertions on the resolved value of a promise.
expect(promise).to.eventually.have.property('bar').that.is.a('string');
Jest is a testing framework that includes its own assertion library and has built-in support for testing asynchronous code. It can handle promises and async/await syntax without the need for additional plugins.
Expect is an assertion library that can be used independently or with Jest. It provides a way to make assertions about promises, similar to chai-as-promised, but it is more tightly integrated with Jest when used in that context.
Chai as Promised extends Chai with a fluent language for asserting facts about promises.
Instead of manually wiring up your expectations to a promise's fulfilled and rejected handlers:
doSomethingAsync().then(
function (result) {
result.should.equal("foo");
done();
},
function (err) {
done(err);
}
);
you can write code that expresses what you really mean:
doSomethingAsync().should.eventually.equal("foo").then(done, done);
or if you have a testing framework that follows the UncommonJS specification for handling promises, simply
return doSomethingAsync().should.eventually.equal("foo");
The most powerful extension provided by Chai as Promised is the eventually
property. With it, you can transform any
existing Chai assertion into one that acts on a promise:
(2 + 2).should.equal(4);
// becomes
return promiseFor(2 + 2).should.eventually.equal(4);
({ foo: "bar" }).should.have.property("foo");
// becomes
return promiseFor({ foo: "bar" }).should.eventually.have.property("foo");
There are also a few promise-specific extensions, grouped here as synonymic blocks:
return promise.should.be.fulfilled;
return promise.should.be.rejected;
return promise.should.be.broken;
return promise.should.eventually.eql("foo");
return promise.should.become("foo");
return promise.should.be.rejected.with(Error);
return promise.should.be.broken.with(Error);
// Note: other variants of Chai's existing `throw` assertion work too.
Do an npm install chai-as-promised
to get up and running. Then:
var chai = require("chai");
var chaiAsPromised = require("chai-as-promised");
chai.use(chaiAsPromised);
You can of course put this code in a common test fixture file; for an example using [Mocha][mocha], see the Chai as Promised tests themselves.
Chai as Promised supports being used as an AMD module, registering itself anonymously (just like Chai). So,
assuming you have configured your loader to map the Chai and Chai as Promised files to the respective module IDs
"chai"
and "chai-as-promised"
, you can use them as follows:
define(function (require, exports, module) {
var chai = require("chai");
var chaiAsPromised = require("chai-as-promised");
chai.use(chaiAsPromised);
});
<script>
tagIf you include Chai as Promised directly with a <script>
tag, it creates a window.chaiAsPromised
global (again,
just like Chai). Then your setup code becomes:
window.chai.use(window.chaiAsPromised);
FAQs
Extends Chai with assertions about promises.
The npm package chai-as-promised receives a total of 0 weekly downloads. As such, chai-as-promised popularity was classified as not popular.
We found that chai-as-promised demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.