Skip to content

Commit

Permalink
feat: move to ESM-only (#287)
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 13, 2024
1 parent 0335b67 commit 4b6fa17
Show file tree
Hide file tree
Showing 17 changed files with 199 additions and 195 deletions.
69 changes: 35 additions & 34 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,43 @@
name: Node.js CI

on:
push:
branches: master
pull_request:
branches: master
push:
branches: master
pull_request:
branches: master

jobs:
lint:
runs-on: ubuntu-latest
lint:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: latest
- run: npm ci
- run: npm run lint
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: latest
- run: npm ci
- run: npm run lint
build:
runs-on: ubuntu-latest

strategy:
matrix:
node-version:
- 18 # to be removed 2025-04-30
- 20 # to be removed 2026-04-30
- latest
# See supported Node.js release schedule at https://github.com/nodejs/release#release-schedule
chai-version:
- "^4.0.0"
strategy:
matrix:
node-version:
- 18 # to be removed 2025-04-30
- 20 # to be removed 2026-04-30
- latest
# See supported Node.js release schedule at https://github.com/nodejs/release#release-schedule
chai-version:
- '^4.0.0'
- '^5.0.0'

steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- name: Install chai ${{ matrix.chai-version }}
run: npm install chai@${{ matrix.chai-version }}
- run: npm run test
steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- name: Install chai ${{ matrix.chai-version }}
run: npm install chai@${{ matrix.chai-version }}
- run: npm run test
62 changes: 31 additions & 31 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,37 @@
name: Publish to npm

on:
release:
types: [created]
release:
types: [created]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 22
- run: npm ci
- run: npm run lint
- run: npm test
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 22
- run: npm ci
- run: npm run lint
- run: npm test

publish-npm:
needs: build
runs-on: ubuntu-latest
permissions:
id-token: write
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 22.x
registry-url: "https://registry.npmjs.org"
cache: "npm"
- run: npm ci
- run: npm version ${TAG_NAME} --git-tag-version=false
env:
TAG_NAME: ${{ github.ref_name }}
- run: npm publish --provenance --access public
env:
NODE_AUTH_TOKEN: ${{ secrets.npm_secret }}
publish-npm:
needs: build
runs-on: ubuntu-latest
permissions:
id-token: write
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 22.x
registry-url: 'https://registry.npmjs.org'
cache: 'npm'
- run: npm ci
- run: npm version ${TAG_NAME} --git-tag-version=false
env:
TAG_NAME: ${{ github.ref_name }}
- run: npm publish --provenance --access public
env:
NODE_AUTH_TOKEN: ${{ secrets.npm_secret }}
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);
}
Loading

0 comments on commit 4b6fa17

Please sign in to comment.