hapijs / hapijs/code

Allow simpler tests for error.code

Đang mở
#166 2 bình luận 0 reaction 0 người được giao Xem trên GitHub
feature
Ngôn ngữ chính
JavaScript
Star
231
Fork
75
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

#### Support plan

* *is this issue currently blocking your project?* (yes/no): no
* *is this issue affecting a production system?* (yes/no): no

#### Context

* *node version*: any
* *module version*: 8.0.3
* *environment* (e.g. node, browser, native): node
* *used with* (e.g. hapi application, another framework, standalone, ...):
* *any other relevant information*:

#### What problem are you trying to solve?

To test for the `.code` property attached to node errors (instead of `.message`) that are not fixed).

Currently I have to do this manually, eg. for `throw()`:

```js
const Code = require('code');
const expect = Code.expect;

const err = expect(() => process.chdir(42)).to.throw(TypeError);
expect(err).to.contain({ code: 'ERR_INVALID_ARG_TYPE' });

// or as one-liner
expect(expect(() => process.chdir(42)).to.throw(TypeError)).to.contain({ code: 'ERR_INVALID_ARG_TYPE' });
```

#### Do you have a new or modified API suggestion to solve the problem?

Add a third `has` parameter to `throw()` and `reject()`, which takes an `object` which is passed to a `contain()` test. Also make the `message` optional (works since it uses a string and type must be `instanceof Error`). Eg.

```js
expect(() => process.chdir(42)).to.throw(TypeError, { code: 'ERR_INVALID_ARG_TYPE' });
```

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.