You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
3.6 KiB

4 years ago
  1. # assert
  2. [![Build Status](https://travis-ci.org/browserify/commonjs-assert.svg?branch=master)](https://travis-ci.org/browserify/commonjs-assert)
  3. This module is used for writing unit tests for your applications, you can access it with `require('assert')`.
  4. It aims to be fully compatibe with the [node.js assert module](http://nodejs.org/api/assert.html), same API and same behavior, just adding support for web browsers.
  5. The API and code may contain traces of the [CommonJS Unit Testing 1.0 spec](http://wiki.commonjs.org/wiki/Unit_Testing/1.0) which they were based on, but both have evolved significantly since then.
  6. A `strict` and a `legacy` mode exist, while it is recommended to only use `strict mode`.
  7. ## Strict mode
  8. When using the `strict mode`, any `assert` function will use the equality used in the strict function mode. So `assert.deepEqual()` will, for example, work the same as `assert.deepStrictEqual()`.
  9. It can be accessed using:
  10. ```js
  11. const assert = require('assert').strict;
  12. ```
  13. ## Legacy mode
  14. > Deprecated: Use strict mode instead.
  15. When accessing `assert` directly instead of using the `strict` property, the
  16. [Abstract Equality Comparison](https://tc39.github.io/ecma262/#sec-abstract-equality-comparison) will be used for any function without a
  17. "strict" in its name (e.g. `assert.deepEqual()`).
  18. It can be accessed using:
  19. ```js
  20. const assert = require('assert');
  21. ```
  22. It is recommended to use the `strict mode` instead as the Abstract Equality Comparison can often have surprising results. Especially
  23. in case of `assert.deepEqual()` as the used comparison rules there are very lax.
  24. E.g.
  25. ```js
  26. // WARNING: This does not throw an AssertionError!
  27. assert.deepEqual(/a/gi, new Date());
  28. ```
  29. ## assert.fail(actual, expected, message, operator)
  30. Throws an exception that displays the values for actual and expected separated by the provided operator.
  31. ## assert(value, message), assert.ok(value, [message])
  32. Tests if value is truthy, it is equivalent to assert.equal(true, !!value, message);
  33. ## assert.equal(actual, expected, [message])
  34. Tests shallow, coercive equality with the equal comparison operator ( == ).
  35. ## assert.notEqual(actual, expected, [message])
  36. Tests shallow, coercive non-equality with the not equal comparison operator ( != ).
  37. ## assert.deepEqual(actual, expected, [message])
  38. Tests for deep equality.
  39. ## assert.deepStrictEqual(actual, expected, [message])
  40. Tests for deep equality, as determined by the strict equality operator ( === )
  41. ## assert.notDeepEqual(actual, expected, [message])
  42. Tests for any deep inequality.
  43. ## assert.strictEqual(actual, expected, [message])
  44. Tests strict equality, as determined by the strict equality operator ( === )
  45. ## assert.notStrictEqual(actual, expected, [message])
  46. Tests strict non-equality, as determined by the strict not equal operator ( !== )
  47. ## assert.throws(block, [error], [message])
  48. Expects block to throw an error. error can be constructor, regexp or validation function.
  49. Validate instanceof using constructor:
  50. ```javascript
  51. assert.throws(function() { throw new Error("Wrong value"); }, Error);
  52. ```
  53. Validate error message using RegExp:
  54. ```javascript
  55. assert.throws(function() { throw new Error("Wrong value"); }, /value/);
  56. ```
  57. Custom error validation:
  58. ```javascript
  59. assert.throws(function() {
  60. throw new Error("Wrong value");
  61. }, function(err) {
  62. if ( (err instanceof Error) && /value/.test(err) ) {
  63. return true;
  64. }
  65. }, "unexpected error");
  66. ```
  67. ## assert.doesNotThrow(block, [message])
  68. Expects block not to throw an error, see assert.throws for details.
  69. ## assert.ifError(value)
  70. Tests if value is not a false value, throws if it is a true value. Useful when testing the first argument, error in callbacks.