Skip to content

Commit 4b6fa17

Browse files
authored
feat: move to ESM-only (#287)
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); ```
1 parent 0335b67 commit 4b6fa17

17 files changed

+199
-195
lines changed

.github/workflows/ci.yml

+35-34
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,43 @@
44
name: Node.js CI
55

66
on:
7-
push:
8-
branches: master
9-
pull_request:
10-
branches: master
7+
push:
8+
branches: master
9+
pull_request:
10+
branches: master
1111

1212
jobs:
13-
lint:
14-
runs-on: ubuntu-latest
13+
lint:
14+
runs-on: ubuntu-latest
1515

16-
steps:
17-
- uses: actions/checkout@v4
18-
- uses: actions/setup-node@v4
19-
with:
20-
node-version: latest
21-
- run: npm ci
22-
- run: npm run lint
23-
build:
24-
runs-on: ubuntu-latest
16+
steps:
17+
- uses: actions/checkout@v4
18+
- uses: actions/setup-node@v4
19+
with:
20+
node-version: latest
21+
- run: npm ci
22+
- run: npm run lint
23+
build:
24+
runs-on: ubuntu-latest
2525

26-
strategy:
27-
matrix:
28-
node-version:
29-
- 18 # to be removed 2025-04-30
30-
- 20 # to be removed 2026-04-30
31-
- latest
32-
# See supported Node.js release schedule at https://github.com/nodejs/release#release-schedule
33-
chai-version:
34-
- "^4.0.0"
26+
strategy:
27+
matrix:
28+
node-version:
29+
- 18 # to be removed 2025-04-30
30+
- 20 # to be removed 2026-04-30
31+
- latest
32+
# See supported Node.js release schedule at https://github.com/nodejs/release#release-schedule
33+
chai-version:
34+
- '^4.0.0'
35+
- '^5.0.0'
3536

36-
steps:
37-
- uses: actions/checkout@v4
38-
- name: Use Node.js ${{ matrix.node-version }}
39-
uses: actions/setup-node@v4
40-
with:
41-
node-version: ${{ matrix.node-version }}
42-
- run: npm ci
43-
- name: Install chai ${{ matrix.chai-version }}
44-
run: npm install chai@${{ matrix.chai-version }}
45-
- run: npm run test
37+
steps:
38+
- uses: actions/checkout@v4
39+
- name: Use Node.js ${{ matrix.node-version }}
40+
uses: actions/setup-node@v4
41+
with:
42+
node-version: ${{ matrix.node-version }}
43+
- run: npm ci
44+
- name: Install chai ${{ matrix.chai-version }}
45+
run: npm install chai@${{ matrix.chai-version }}
46+
- run: npm run test

.github/workflows/npm-publish.yml

+31-31
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,37 @@
44
name: Publish to npm
55

66
on:
7-
release:
8-
types: [created]
7+
release:
8+
types: [created]
99

1010
jobs:
11-
build:
12-
runs-on: ubuntu-latest
13-
steps:
14-
- uses: actions/checkout@v4
15-
- uses: actions/setup-node@v4
16-
with:
17-
node-version: 22
18-
- run: npm ci
19-
- run: npm run lint
20-
- run: npm test
11+
build:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v4
15+
- uses: actions/setup-node@v4
16+
with:
17+
node-version: 22
18+
- run: npm ci
19+
- run: npm run lint
20+
- run: npm test
2121

22-
publish-npm:
23-
needs: build
24-
runs-on: ubuntu-latest
25-
permissions:
26-
id-token: write
27-
steps:
28-
- uses: actions/checkout@v4
29-
- uses: actions/setup-node@v4
30-
with:
31-
node-version: 22.x
32-
registry-url: "https://registry.npmjs.org"
33-
cache: "npm"
34-
- run: npm ci
35-
- run: npm version ${TAG_NAME} --git-tag-version=false
36-
env:
37-
TAG_NAME: ${{ github.ref_name }}
38-
- run: npm publish --provenance --access public
39-
env:
40-
NODE_AUTH_TOKEN: ${{ secrets.npm_secret }}
22+
publish-npm:
23+
needs: build
24+
runs-on: ubuntu-latest
25+
permissions:
26+
id-token: write
27+
steps:
28+
- uses: actions/checkout@v4
29+
- uses: actions/setup-node@v4
30+
with:
31+
node-version: 22.x
32+
registry-url: 'https://registry.npmjs.org'
33+
cache: 'npm'
34+
- run: npm ci
35+
- run: npm version ${TAG_NAME} --git-tag-version=false
36+
env:
37+
TAG_NAME: ${{ github.ref_name }}
38+
- run: npm publish --provenance --access public
39+
env:
40+
NODE_AUTH_TOKEN: ${{ secrets.npm_secret }}

README.md

+17-15
Original file line numberDiff line numberDiff line change
@@ -107,21 +107,25 @@ return promise.then(null, null, progressSpy).then(function () {
107107
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:
108108

109109
```js
110-
chaiAsPromised.transferPromiseness = function (assertion, promise) {
110+
import {setTransferPromiseness} from 'chai-as-promised';
111+
112+
setTransferPromiseness(function (assertion, promise) {
111113
assertion.then = promise.then.bind(promise); // this is all you get by default
112114
assertion.finally = promise.finally.bind(promise);
113115
assertion.done = promise.done.bind(promise);
114-
};
116+
});
115117
```
116118

117119
### Transforming Arguments to the Asserters
118120

119121
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:
120122

121123
```js
122-
chaiAsPromised.transformAsserterArgs = function (args) {
124+
import {transformAsserterArgs} from 'chai-as-promised';
125+
126+
setTransformAsserterArgs(function (args) {
123127
return args.map(function (x) { return x + 1; });
124-
}
128+
});
125129

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

136-
chaiAsPromised.transformAsserterArgs = function (args) {
140+
setTransformAsserterArgs(function (args) {
137141
return Promise.all(args);
138-
};
142+
});
139143

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

215219
```javascript
216-
var chai = require("chai");
217-
var chaiAsPromised = require("chai-as-promised");
220+
import * as chai from 'chai';
221+
import chaiAsPromised from 'chai-as-promised';
218222

219223
chai.use(chaiAsPromised);
220224

221225
// Then either:
222-
var expect = chai.expect;
226+
const expect = chai.expect;
223227
// or:
224-
var assert = chai.assert;
228+
const assert = chai.assert;
225229
// or:
226230
chai.should();
227231
// according to your preference of assertion style
@@ -231,14 +235,12 @@ You can of course put this code in a common test fixture file; for an example us
231235

232236
**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.
233237

234-
### In the Browser
235-
236-
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.
237-
238238
### Karma
239239

240240
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.
241241

242242
### Browser/Node Compatibility
243243

244-
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/).
244+
Chai as Promised requires support for ES modules and modern JavaScript syntax.
245+
If your browser doesn't support this, you will need to transpile it down using
246+
a tool like [Babel](https://babeljs.io/).

lib/chai-as-promised.js

+41-13
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
'use strict';
2-
/* eslint-disable no-invalid-this */
3-
let checkError = require('check-error');
1+
import * as checkErrorDefault from 'check-error';
42

5-
module.exports = (chai, utils) => {
3+
let checkError = checkErrorDefault;
4+
5+
export default function (chai, utils) {
66
const Assertion = chai.Assertion;
77
const assert = chai.assert;
88
const proxify = utils.proxify;
@@ -120,7 +120,7 @@ module.exports = (chai, utils) => {
120120
}
121121
);
122122

123-
module.exports.transferPromiseness(this, derivedPromise);
123+
transferPromiseness(this, derivedPromise);
124124
return this;
125125
});
126126

@@ -147,7 +147,7 @@ module.exports = (chai, utils) => {
147147
}
148148
);
149149

150-
module.exports.transferPromiseness(this, derivedPromise);
150+
transferPromiseness(this, derivedPromise);
151151
return this;
152152
});
153153

@@ -258,7 +258,7 @@ module.exports = (chai, utils) => {
258258
}
259259
);
260260

261-
module.exports.transferPromiseness(this, derivedPromise);
261+
transferPromiseness(this, derivedPromise);
262262
return this;
263263
});
264264

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

349-
return args ? module.exports.transformAsserterArgs(args) : args;
349+
return args ? transformAsserterArgs(args) : args;
350350
})
351351
.then((newArgs) => {
352352
asserter.apply(assertion, newArgs);
@@ -357,7 +357,7 @@ module.exports = (chai, utils) => {
357357
return assertion._obj;
358358
});
359359

360-
module.exports.transferPromiseness(assertion, derivedPromise);
360+
transferPromiseness(assertion, derivedPromise);
361361
return assertion;
362362
}
363363

@@ -413,10 +413,38 @@ module.exports = (chai, utils) => {
413413
return returnedPromise;
414414
};
415415
});
416-
};
416+
}
417417

418-
module.exports.transferPromiseness = (assertion, promise) => {
418+
function defaultTransferPromiseness(assertion, promise) {
419419
assertion.then = promise.then.bind(promise);
420-
};
420+
}
421+
422+
function defaultTransformAsserterArgs(values) {
423+
return values;
424+
}
425+
426+
let customTransferPromiseness;
427+
let customTransformAsserterArgs;
428+
429+
export function setTransferPromiseness(fn) {
430+
customTransferPromiseness = fn || defaultTransferPromiseness;
431+
}
421432

422-
module.exports.transformAsserterArgs = (values) => values;
433+
export function setTransformAsserterArgs(fn) {
434+
customTransformAsserterArgs = fn || defaultTransformAsserterArgs;
435+
}
436+
437+
export function transferPromiseness(assertion, promise) {
438+
if (customTransferPromiseness) {
439+
customTransferPromiseness(assertion, promise);
440+
} else {
441+
defaultTransferPromiseness(assertion, promise);
442+
}
443+
}
444+
445+
export function transformAsserterArgs(values) {
446+
if (customTransformAsserterArgs) {
447+
return customTransformAsserterArgs(values);
448+
}
449+
return defaultTransformAsserterArgs(values);
450+
}

0 commit comments

Comments
 (0)