Skip to content

Commit

Permalink
feat: move to ESM-only
Browse files Browse the repository at this point in the history
Migrates to an ESM-only package structure.

The module must now be imported like so:

```ts
import chaiAsPromised from 'chai-as-promised';
import * as chai from 'chai';

chai.use(chaiAsPromised);
```
  • Loading branch information
43081j committed May 5, 2024
1 parent 7012feb commit 27d2404
Show file tree
Hide file tree
Showing 15 changed files with 133 additions and 130 deletions.
32 changes: 17 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,21 +107,25 @@ return promise.then(null, null, progressSpy).then(function () {
By default, the promises returned by Chai as Promised's assertions are regular Chai assertion objects, extended with a single `then` method derived from the input promise. To change this behavior, for instance to output a promise with more useful sugar methods such as are found in most promise libraries, you can override `chaiAsPromised.transferPromiseness`. Here's an example that transfer's Q's `finally` and `done` methods:

```js
chaiAsPromised.transferPromiseness = function (assertion, promise) {
import {setTransferPromiseness} from 'chai-as-promised';

setTransferPromiseness(function (assertion, promise) {
assertion.then = promise.then.bind(promise); // this is all you get by default
assertion.finally = promise.finally.bind(promise);
assertion.done = promise.done.bind(promise);
};
});
```

### Transforming Arguments to the Asserters

Another advanced customization hook Chai as Promised allows is if you want to transform the arguments to the asserters, possibly asynchronously. Here is a toy example:

```js
chaiAsPromised.transformAsserterArgs = function (args) {
import {transformAsserterArgs} from 'chai-as-promised';

setTransformAsserterArgs(function (args) {
return args.map(function (x) { return x + 1; });
}
});

Promise.resolve(2).should.eventually.equal(2); // will now fail!
Promise.resolve(3).should.eventually.equal(2); // will now pass!
Expand All @@ -133,9 +137,9 @@ The transform can even be asynchronous, returning a promise for an array instead
// This will normally fail, since within() only works on numbers.
Promise.resolve(2).should.eventually.be.within(Promise.resolve(1), Promise.resolve(6));

chaiAsPromised.transformAsserterArgs = function (args) {
setTransformAsserterArgs(function (args) {
return Promise.all(args);
};
});

// But now it will pass, since we transformed the array of promises for numbers into
// (a promise for) an array of numbers
Expand Down Expand Up @@ -213,15 +217,15 @@ This will pass any failures of the individual promise assertions up to the test
Do an `npm install chai-as-promised` to get up and running. Then:

```javascript
var chai = require("chai");
var chaiAsPromised = require("chai-as-promised");
import * as chai from 'chai';
import chaiAsPromised from 'chai-as-promised';

chai.use(chaiAsPromised);

// Then either:
var expect = chai.expect;
const expect = chai.expect;
// or:
var assert = chai.assert;
const assert = chai.assert;
// or:
chai.should();
// according to your preference of assertion style
Expand All @@ -231,14 +235,12 @@ You can of course put this code in a common test fixture file; for an example us

**Note when using other Chai plugins:** Chai as Promised finds all currently-registered asserters and promisifies them, at the time it is installed. Thus, you should install Chai as Promised _last_, after any other Chai plugins, if you expect their asserters to be promisified.

### In the Browser

To use Chai as Promised in environments that don't support Node.js-like CommonJS modules, you'll need to use a bundling tool like [browserify](http://browserify.org/). See also the note below about browser compatibility.

### Karma

If you're using [Karma](https://karma-runner.github.io/), check out the accompanying [karma-chai-as-promised](https://github.com/vlkosinov/karma-chai-as-promised) plugin.

### Browser/Node Compatibility

Chai as Promised requires Node v4+ or a browser with equivalent support for modern JavaScript syntax. If your browser doesn't support modern JavaScript syntax, you'll need to transpile it down using a tool like [Babel](http://babeljs.io/).
Chai as Promised requires support for ES modules and modern JavaScript syntax.
If your browser doesn't support this, you will need to transpile it down using
a tool like [Babel](https://babeljs.io/).
54 changes: 41 additions & 13 deletions lib/chai-as-promised.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';
/* eslint-disable no-invalid-this */
let checkError = require('check-error');
import * as checkErrorDefault from 'check-error';

module.exports = (chai, utils) => {
let checkError = checkErrorDefault;

export default function (chai, utils) {
const Assertion = chai.Assertion;
const assert = chai.assert;
const proxify = utils.proxify;
Expand Down Expand Up @@ -120,7 +120,7 @@ module.exports = (chai, utils) => {
}
);

module.exports.transferPromiseness(this, derivedPromise);
transferPromiseness(this, derivedPromise);
return this;
});

Expand All @@ -147,7 +147,7 @@ module.exports = (chai, utils) => {
}
);

module.exports.transferPromiseness(this, derivedPromise);
transferPromiseness(this, derivedPromise);
return this;
});

Expand Down Expand Up @@ -258,7 +258,7 @@ module.exports = (chai, utils) => {
}
);

module.exports.transferPromiseness(this, derivedPromise);
transferPromiseness(this, derivedPromise);
return this;
});

Expand Down Expand Up @@ -346,7 +346,7 @@ module.exports = (chai, utils) => {
assertion._obj = value;
utils.flag(assertion, 'eventually', false);

return args ? module.exports.transformAsserterArgs(args) : args;
return args ? transformAsserterArgs(args) : args;
})
.then((newArgs) => {
asserter.apply(assertion, newArgs);
Expand All @@ -357,7 +357,7 @@ module.exports = (chai, utils) => {
return assertion._obj;
});

module.exports.transferPromiseness(assertion, derivedPromise);
transferPromiseness(assertion, derivedPromise);
return assertion;
}

Expand Down Expand Up @@ -413,10 +413,38 @@ module.exports = (chai, utils) => {
return returnedPromise;
};
});
};
}

module.exports.transferPromiseness = (assertion, promise) => {
function defaultTransferPromiseness(assertion, promise) {
assertion.then = promise.then.bind(promise);
};
}

function defaultTransformAsserterArgs(values) {
return values;
}

let customTransferPromiseness;
let customTransformAsserterArgs;

export function setTransferPromiseness(fn) {
customTransferPromiseness = fn || defaultTransferPromiseness;
}

module.exports.transformAsserterArgs = (values) => values;
export function setTransformAsserterArgs(fn) {
customTransformAsserterArgs = fn || defaultTransformAsserterArgs;
}

export function transferPromiseness(assertion, promise) {
if (customTransferPromiseness) {
customTransferPromiseness(assertion, promise);
} else {
defaultTransferPromiseness(assertion, promise);
}
}

export function transformAsserterArgs(values) {
if (customTransformAsserterArgs) {
return customTransformAsserterArgs(values);
}
return defaultTransformAsserterArgs(values);
}
76 changes: 30 additions & 46 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "chai-as-promised",
"type": "module",
"description": "Extends Chai with assertions about promises.",
"keywords": [
"chai",
Expand All @@ -16,6 +17,9 @@
"license": "WTFPL",
"repository": "domenic/chai-as-promised",
"main": "./lib/chai-as-promised.js",
"exports": {
".": "./lib/chai-as-promised.js"
},
"files": [
"lib"
],
Expand All @@ -26,14 +30,14 @@
"format": "prettier --write lib test"
},
"dependencies": {
"check-error": "^1.0.2"
"check-error": "^2.0.0"
},
"peerDependencies": {
"chai": ">= 2.1.2 < 6"
},
"devDependencies": {
"c8": "^9.1.0",
"chai": "^4.4.1",
"chai": "^5.1.0",
"eslint": "^8.57.0",
"mocha": "^10.4.0",
"prettier": "^3.2.5"
Expand Down
9 changes: 3 additions & 6 deletions test/assert-eventually.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
'use strict';
require('./support/setup.js');
const shouldPass = require('./support/common.js').shouldPass;
const shouldFail = require('./support/common.js').shouldFail;
const assert = require('chai').assert;
const expect = require('chai').expect;
import './support/setup.js';
import {shouldPass, shouldFail} from './support/common.js';
import {assert, expect} from 'chai';

describe('Assert interface with eventually extender:', () => {
let promise = null;
Expand Down
8 changes: 3 additions & 5 deletions test/assert-promise-specific.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict';
require('./support/setup.js');
const shouldPass = require('./support/common.js').shouldPass;
const shouldFail = require('./support/common.js').shouldFail;
const assert = require('chai').assert;
import './support/setup.js';
import {shouldPass, shouldFail} from './support/common.js';
import {assert} from 'chai';

describe('Assert interface:', () => {
let promise = null;
Expand Down
Loading

0 comments on commit 27d2404

Please sign in to comment.