Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow simpler tests for error.code #166

Open
kanongil opened this issue Nov 5, 2021 · 2 comments
Open

Allow simpler tests for error.code #166

kanongil opened this issue Nov 5, 2021 · 2 comments
Labels
feature New functionality or improvement

Comments

@kanongil
Copy link
Contributor

kanongil commented Nov 5, 2021

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():

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.

expect(() => process.chdir(42)).to.throw(TypeError, { code: 'ERR_INVALID_ARG_TYPE' });
@kanongil kanongil added the feature New functionality or improvement label Nov 5, 2021
@devinivy
Copy link
Member

devinivy commented Nov 5, 2021

This would be great 👍 We might also look for compatibility with bounce's error matching.

@Nargonath
Copy link
Member

I agree good idea. Compatibility with system and boom type matching, if that's what you meant @devinivy, also sounds good. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New functionality or improvement
Projects
None yet
Development

No branches or pull requests

3 participants