javascript 独立的断言库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10472152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 09:59:55  来源:igfitidea点击:

standalone assertion libraries?

javascript

提问by user5321531

I was nearly through porting YUI assertion modules to standalone libraries when the thought popped into mind that I should have perhaps asked on StackOverflow if this was necessary first. However I finished the libs:

当我想到我应该先在 StackOverflow 上询问是否有必要时,我几乎将 YUI 断言模块移植到独立库中。但是我完成了库:

YUIPort: https://github.com/gso/YUIPort

YUIPort:https: //github.com/gso/YUIPort

But thought I would duly ask as well. Are there any good quality standalone libraries of assert functions - these can be very useful, e.g., checking the validity of arguments passed to functions (can save a lot of time invoking the debugger).

但我想我也会适时地问一下。是否有任何高质量的断言函数独立库 - 这些可能非常有用,例如,检查传递给函数的参数的有效性(可以节省调用调试器的大量时间)。

回答by user5321531

As of May 7, 2012

截至 2012 年 5 月 7 日

After some brief research, starting with the most minimalistic:

经过一些简短的研究,从最简约的开始:

I have to admit that I do tend to agree with a commenter on DailyJS:

我不得不承认,我确实倾向于同意DailyJS的评论者

I don't understand why everyone likes these wordy assertion libraries.

我不明白为什么每个人都喜欢这些冗长的断言库。

Although I'm not a CoffeeScript user, I do tend to gravitate towards the plain assert.js style syntax rather than all this chaining.

尽管我不是 CoffeeScript 用户,但我确实倾向于使用简单的 assert.js 样式语法而不是所有这些链接。

Features that these libraries are developing include:

这些库正在开发的功能包括:

  • plugable with the major test suites (saves having to switch between two libraries when coding)
  • coding server-side (node) and client-side without changing libraries
  • integration into the main web frameworks (e.g. JQuery)
  • 可插入主要测试套件(无需在编码时在两个库之间切换)
  • 在不更改库的情况下编码服务器端(节点)和客户端
  • 集成到主要的 Web 框架中(例如 JQuery)

Some of the major test suites include:

一些主要的测试套件包括:

The major web frameworksoften include their own test suites.

主要的Web框架通常包括他们自己的测试套件。

Logging wise, there is a discussion on libraries available. They don't, at a glance, seem to have caught up with server-side JS as of yet.

明智地记录日志,有关于可用库的讨论。乍一看,他们似乎还没有赶上服务器端 JS。

回答by Raynos

function assert(condition, message) {
    if (!condition) throw new Error(message)
}