Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Tests

on: [push]

jobs:
unit:
runs-on: ubuntu-latest
strategy:
matrix:
node: ['20', '22', '24']
name: Node ${{ matrix.node }}

steps:
- name: Checkout code
uses: actions/checkout@v4
with:
persist-credentials: false

- name: Setup Node.js version
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}

- name: Install dependencies
run: yarn install --frozen-lockfile

- name: Run lint
run: yarn lint

- name: Run tests
run: yarn test:coverage
12 changes: 0 additions & 12 deletions .travis.yml

This file was deleted.

22 changes: 19 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

This module provides a set of errors based on [standard-http-error](https://www.npmjs.com/package/standard-http-error), reducing the boilerplate of adding error classes for the most common HTTP errors.

## Status

[![npm version][npm-image]][npm-url]
[![build status][tests-image]][tests-url]

## Setup

Install **@uphold/http-errors** with yarn:
Expand Down Expand Up @@ -116,13 +121,19 @@ Use the `test` script to run the test suite:
yarn test
```

To test and check coverage use the `cover` script:
To run the tests on watch mode, use the `test:watch` script:

```sh
yarn cover
yarn test:watch
```

A full coverage report will be generated on _test/coverage_ folder.
To test and check coverage use the `test:coverage` script:

```sh
yarn test:coverage
```

A full coverage report will be generated on `/coverage` folder.

## Contributing

Expand All @@ -137,3 +148,8 @@ yarn release [<version> | major | minor | patch]
## License

MIT

[npm-image]: https://img.shields.io/npm/v/@uphold/http-errors.svg
[npm-url]: https://www.npmjs.com/package/@uphold/http-errors
[tests-image]: https://github.com/uphold/uphold-http-errors/actions/workflows/tests.yaml/badge.svg?branch=master
[tests-url]: https://github.com/uphold/uphold-http-errors/actions/workflows/tests.yaml
17 changes: 0 additions & 17 deletions jest.json

This file was deleted.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
"types": "types/index.d.ts",
"scripts": {
"changelog": "github-changelog-generator --future-release=v$npm_package_version > CHANGELOG.md",
"cover": "yarn test -- --coverage",
"lint": "eslint",
"release": "npm version $1 -m 'Release %s'",
"test": "jest --config jest.json",
"test": "node --test test/**/*_test.js",
"test:coverage": "NODE_V8_COVERAGE=coverage node --test --experimental-test-coverage test/**/*_test.js",
"test:watch": "node --test --watch test/**/*_test.js",
"version": "yarn changelog && git add CHANGELOG.md"
},
"pre-commit": [
Expand All @@ -25,11 +26,10 @@
"@uphold/github-changelog-generator": "^0.7.0",
"eslint": "^9.28.0",
"eslint-config-uphold": "^6.5.1",
"jest": "20.0.4",
"pre-commit": "1.2.2",
"prettier": "^3.5.3"
},
"engines": {
"node": ">=4"
"node": ">=20"
}
}
11 changes: 6 additions & 5 deletions test/errors/assertion-failed-error_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@
*/

const { AssertionFailedError } = require('../../src');
const test = require('../utils/test-http-error');
const { describe, it } = require('node:test');
const testHttpError = require('../utils/test-http-error');

/**
* Test "Assertion Failed" error.
* Test `Assertion Failed` error.
*/

describe('AssertionFailedError', () => {
test(AssertionFailedError, 500, 'Assertion Failed', false);
testHttpError(AssertionFailedError, 500, 'Assertion Failed', false);

it('should set given `errors`', () => {
expect(new AssertionFailedError('foo').errors).toBe('foo');
it('should set given `errors`', ({ assert }) => {
assert.equal(new AssertionFailedError('foo').errors, 'foo');
});
});
7 changes: 4 additions & 3 deletions test/errors/bad-request-error_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
*/

const { BadRequestError } = require('../../src');
const test = require('../utils/test-http-error');
const { describe } = require('node:test');
const testHttpError = require('../utils/test-http-error');

/**
* Test "Bad Request" error.
* Test `Bad Request` error.
*/

describe('BadRequestError', () => {
test(BadRequestError, 400, 'Bad Request');
testHttpError(BadRequestError, 400, 'Bad Request');
});
7 changes: 4 additions & 3 deletions test/errors/conflict-error_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
*/

const { ConflictError } = require('../../src');
const test = require('../utils/test-http-error');
const { describe } = require('node:test');
const testHttpError = require('../utils/test-http-error');

/**
* Test "Conflict" error.
* Test `Conflict` error.
*/

describe('ConflictError', () => {
test(ConflictError, 409, 'Conflict');
testHttpError(ConflictError, 409, 'Conflict');
});
7 changes: 4 additions & 3 deletions test/errors/forbidden-error_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
*/

const { ForbiddenError } = require('../../src');
const test = require('../utils/test-http-error');
const { describe } = require('node:test');
const testHttpError = require('../utils/test-http-error');

/**
* Test "Forbidden" error.
* Test `Forbidden` error.
*/

describe('ForbiddenError', () => {
test(ForbiddenError, 403, 'Forbidden');
testHttpError(ForbiddenError, 403, 'Forbidden');
});
7 changes: 4 additions & 3 deletions test/errors/gone-error_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
*/

const { GoneError } = require('../../src');
const test = require('../utils/test-http-error');
const { describe } = require('node:test');
const testHttpError = require('../utils/test-http-error');

/**
* Test "Gone" error.
* Test `Gone` error.
*/

describe('GoneError', () => {
test(GoneError, 410, 'Gone');
testHttpError(GoneError, 410, 'Gone');
});
7 changes: 4 additions & 3 deletions test/errors/http-error_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
*/

const { HttpError } = require('../../src');
const { describe, it } = require('node:test');
const StandardHttpError = require('standard-http-error');

/**
* Test HTTP error.
* Test `HTTP` error.
*/

describe('HttpError', () => {
it('should inherit from `StandardHttpError`', () => {
expect(new HttpError(400)).toBeInstanceOf(StandardHttpError);
it('should inherit from `StandardHttpError`', ({ assert }) => {
assert.ok(new HttpError(400) instanceof StandardHttpError);
});
});
7 changes: 4 additions & 3 deletions test/errors/not-found-error_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
*/

const { NotFoundError } = require('../../src');
const test = require('../utils/test-http-error');
const { describe } = require('node:test');
const testHttpError = require('../utils/test-http-error');

/**
* Test "Not Found" error.
* Test `Not Found` error.
*/

describe('NotFoundError', () => {
test(NotFoundError, 404, 'Not Found');
testHttpError(NotFoundError, 404, 'Not Found');
});
7 changes: 4 additions & 3 deletions test/errors/not-implemented-error_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
*/

const { NotImplementedError } = require('../../src');
const test = require('../utils/test-http-error');
const { describe } = require('node:test');
const testHttpError = require('../utils/test-http-error');

/**
* Test "Not Implemented" error.
* Test `Not Implemented` error.
*/

describe('NotImplementedError', () => {
test(NotImplementedError, 501, 'Not Implemented', false);
testHttpError(NotImplementedError, 501, 'Not Implemented', false);
});
7 changes: 4 additions & 3 deletions test/errors/service-unavailable-error_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
*/

const { ServiceUnavailableError } = require('../../src');
const test = require('../utils/test-http-error');
const { describe } = require('node:test');
const testHttpError = require('../utils/test-http-error');

/**
* Test "Service Unavailable" error.
* Test `Service Unavailable` error.
*/

describe('ServiceUnavailableError', () => {
test(ServiceUnavailableError, 503, 'Service Unavailable');
testHttpError(ServiceUnavailableError, 503, 'Service Unavailable');
});
7 changes: 4 additions & 3 deletions test/errors/too-many-requests-error_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
*/

const { TooManyRequestsError } = require('../../src');
const test = require('../utils/test-http-error');
const { describe } = require('node:test');
const testHttpError = require('../utils/test-http-error');

/**
* Test "TooManyRequestsError" error.
* Test `TooManyRequestsError` error.
*/

describe('TooManyRequestsError', () => {
test(TooManyRequestsError, 429, 'Too Many Requests');
testHttpError(TooManyRequestsError, 429, 'Too Many Requests');
});
7 changes: 4 additions & 3 deletions test/errors/unauthorized-error_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
*/

const { UnauthorizedError } = require('../../src');
const test = require('../utils/test-http-error');
const { describe } = require('node:test');
const testHttpError = require('../utils/test-http-error');

/**
* Test "Unauthorized" error.
* Test `Unauthorized` error.
*/

describe('UnauthorizedError', () => {
test(UnauthorizedError, 401, 'Unauthorized');
testHttpError(UnauthorizedError, 401, 'Unauthorized');
});
11 changes: 6 additions & 5 deletions test/errors/validation-failed-error_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@
*/

const { ValidationFailedError } = require('../../src');
const test = require('../utils/test-http-error');
const { describe, test } = require('node:test');
const testHttpError = require('../utils/test-http-error');

/**
* Test "Validation Failed" error.
* Test `Validation Failed` error.
*/

describe('ValidationFailedError', () => {
test(ValidationFailedError, 400, 'Validation Failed', false);
testHttpError(ValidationFailedError, 400, 'Validation Failed', false);

it('should set given `errors`', () => {
expect(new ValidationFailedError('foo').errors).toBe('foo');
test('should set given `errors`', ({ assert }) => {
assert.strictEqual(new ValidationFailedError('foo').errors, 'foo');
});
});
23 changes: 12 additions & 11 deletions test/utils/test-http-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,32 @@
*/

const { HttpError } = require('../../src');
const { it } = require('node:test');

/**
* Test HTTP error.
* Test `HTTP` error.
*/

module.exports = (Error, code, message, testArguments = true) => {
it('should inherit from `HttpError`', () => {
expect(new Error()).toBeInstanceOf(HttpError);
it('should inherit from `HttpError`', ({ assert }) => {
assert.ok(new Error() instanceof HttpError);
});

it(`should have a ${code} code`, () => {
expect(new Error().code).toBe(code);
it(`should have a ${code} code`, ({ assert }) => {
assert.equal(new Error().code, code);
});

it(`should have a "${message}" message`, () => {
expect(new Error().message).toBe(message);
it(`should have a "${message}" message`, ({ assert }) => {
assert.equal(new Error().message, message);
});

if (testArguments) {
it('should override default message', () => {
expect(new Error('foo').message).toBe('foo');
it('should override default message', ({ assert }) => {
assert.equal(new Error('foo').message, 'foo');
});

it('should add custom properties', () => {
expect(new Error({ foo: 'bar' }).foo).toBe('bar');
it('should add custom properties', ({ assert }) => {
assert.equal(new Error({ foo: 'bar' }).foo, 'bar');
});
}
};
Loading