阿西河

所有教程

公众号
🌙
阿西河前端的公众号

我的收藏

    最近访问  (文章)

      教程列表

      抓包专区
      测试专区

      Node.js assert.fail(废弃)

      Node.js assert.fail(actual, expected[, message[, operator[, stackStartFn]]]) (废弃)

      版本历史

      版本变更
      v10.0.0Calling assert.fail() with more than one argument is deprecated and emits a warning.
      v0.1.21新增于: v0.1.21
      • actual < any>
      • expected < any>
      • message < string> | < Error>
      • operator < string> Default: ‘!=’
      • stackStartFn < Function> Default: assert.fail

      If message is falsy, the error message is set as the values of actual and expected separated by the provided operator. If just the two actual and expected arguments are provided, operator will default to ‘!=’. If message is provided as third argument it will be used as the error message and the other arguments will be stored as properties on the thrown object. If stackStartFn is provided, all stack frames above that function will be removed from stacktrace (see Error.captureStackTrace). If no arguments are given, the default message Failed will be used.

      const assert = require('assert').strict;
      
      assert.fail('a', 'b');
      // AssertionError [ERR_ASSERTION]: 'a' != 'b'
      
      assert.fail(1, 2, undefined, '>');
      // AssertionError [ERR_ASSERTION]: 1 > 2
      
      assert.fail(1, 2, 'fail');
      // AssertionError [ERR_ASSERTION]: fail
      
      assert.fail(1, 2, 'whoops', '>');
      // AssertionError [ERR_ASSERTION]: whoops
      
      assert.fail(1, 2, new TypeError('need array'));
      // TypeError: need array
      

      In the last three cases actual, expected, and operator have no influence on the error message.

      Example use of stackStartFn for truncating the exception’s stacktrace:

      function suppressFrame() {
        assert.fail('a', 'b', undefined, '!==', suppressFrame);
      }
      suppressFrame();
      // AssertionError [ERR_ASSERTION]: 'a' !== 'b'
      //     at repl:1:1
      //     at ContextifyScript.Script.runInThisContext (vm.js:44:33)
      //     ...
      

      更多选项请参考:Node.js assert 断言测试,或者通过 点击对应菜单 进行查看;


      目录
      本文目录
      目录