What is “assert” in JavaScript?

后端 未结 14 690
-上瘾入骨i
-上瘾入骨i 2020-12-04 05:03

What does assert mean in JavaScript?

I’ve seen something like:

assert(function1() && function2() && function3(), \"some          


        
相关标签:
14条回答
  • 2020-12-04 05:11

    assert() is not a native javascript function. It is a custom function someone made. You will have to look for it on your page or in your files and post it for anybody to help determine what it's doing.

    0 讨论(0)
  • 2020-12-04 05:12

    It probably came with a testing library that some of your code is using. Here's an example of one (chances are it's not the same library as your code is using, but it shows the general idea):

    http://chaijs.com/guide/styles/#assert

    0 讨论(0)
  • 2020-12-04 05:14

    Assertion throws error message if first attribute is false, and the second attribute is the message to be thrown.

    console.assert(condition,message);
    

    There are many comments saying assertion does not exist in JavaScript but console.assert() is the assert function in JavaScript The idea of assertion is to find why/where the bug occurs.

    console.assert(document.getElementById("title"), "You have no element with ID 'title'");
    console.assert(document.getElementById("image"), "You have no element with ID 'image'");
    

    Here depending on the message you can find what the bug is. These error messages will be displayed to console in red color as if we called console.error();
    To see more functions in console execute console.log(console);

    0 讨论(0)
  • 2020-12-04 05:17

    check this:http://net.tutsplus.com/tutorials/javascript-ajax/quick-tip-quick-and-easy-javascript-testing-with-assert/

    it is for testing JavaScript. Amazingly, at barely five or six lines, this code provides a great level of power and control over your code, when testing.

    The assert function accepts two parameters:

    outcome: A boolean, which references whether your test passed or failed

    description: A short description of your test.

    The assert function then simply creates a list item, applies a class of either “pass” or “fail,” dependent upon whether your test returned true or false, and then appends the description to the list item. Finally, that block of coded is added to the page. It’s crazy simple, but works perfectly.

    0 讨论(0)
  • 2020-12-04 05:17

    Word or function "assert" is mostly used in testing parts of application.

    Assert functions are a short way of instructing the program to check the condition (also called "assertion") and if the condition is not True, it will throw error.

    So let's see how it would look like in "normal code"

    if (typeof "string" === "array") { throw Error('Error: "string" !== "array"'); }

    With assert you can simply write:

    assert(typeof "string" === "array")

    In Javascript, there's no native assert function, so you have to use one from some library.

    For simple introduction, you can check this article:

    http://fredkschott.com/post/2014/05/nodejs-testing-essentials/

    I hope it helps.

    0 讨论(0)
  • 2020-12-04 05:22

    Previous answers can be improved in terms of performances and compatibility.

    Check once if the Error object exists, if not declare it :

    if (typeof Error === "undefined") {
        Error = function(message) {
            this.message = message;
        };
        Error.prototype.message = "";
    }
    

    Then, each assertion will check the condition, and always throw an Error object

    function assert(condition, message) {
        if (!condition) throw new Error(message || "Assertion failed");
    }
    

    Keep in mind that the console will not display the real error line number, but the line of the assert function, which is not useful for debugging.

    0 讨论(0)
提交回复
热议问题