weixin_39637975
weixin_39637975
2021-01-10 02:05

"stop the world" assertion

Feature request from Yehuda: An assertion method that, when it fails, stops the entire testsuite, preventing any further tests to run.

Usecases: - jshint running in testsuite could stop any further tests from running - enviroment test could prevent tests to run, e.g. don't run anything if page is loaded via file: protocol

该提问来源于开源项目:qunitjs/qunit

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

4条回答

  • weixin_39617006 weixin_39617006 4月前

    Maybe a static method instead of an assertion method, that way it can be the result of any assertion (as opposed to whatever assertion(s) we stick it on), like QUnit.abort instead of assert.equalOrAbort.

    点赞 评论 复制链接分享
  • weixin_39883433 weixin_39883433 4月前

    From an API point of view this would be charming:

     javascript
    test("assertion with stop on failure", function() {
      equal(true, false, "hello world").orAbort("Aborting tests because I feel like it");
    });
    

    This prevents you from cluttering the namespace with more functions ('equal()', 'equalOrAbort()'), is nicer to read and allows the test to pass additional data to the abort-handler. Also this is "future-proof" in the way that you could always add other callback-registries to be executed on failure. As the assertion functions currently have no return value, this should be backward-compatible.

    Some code fragments asked for:

     javascript
    // yes, we're using an Exception for message-passing and deliberate control-flow-ejection. Sue me!
    var AbortTestRun = function(){ /* ... */ };
    
    // container for failure handlers
    var failureHandlers = {
        orAbort: function(message) {
            throw new AbortTestRun(message);
        }
    };
    var voidHandlers = {
        orAbort: function(){}
    };
    
    QUnit.assert.equal = function( actual, expected, message ) {
        var result = expected == actual;
        QUnit.push( result, actual, expected, message );
        return result ? voidHandlers : failureHandlers;
    };
    
    // in Test.prototype.run extend the catch() {} to check for `e instanceof AbortTestRun`,
    // if so, kill the queue and show the abort-message
    
    点赞 评论 复制链接分享
  • weixin_39637975 weixin_39637975 4月前

    The two examples in the description seem saner than the one in the proposed API comment. I'd rather see something like QUnit.abort();

    From Scott on IRC. So for jQuery's testsuite, something like this:

     js
    if ( isLocal ) {
      QUnit.abort( "Cannot run tests over the file protocol." );
    }
    

    The message argument would be output to clarify the abort.

    点赞 评论 复制链接分享
  • weixin_39637975 weixin_39637975 4月前

    We've reviewed this again, more than a year later, and decided not to implement this. There's neither enough interest nor common value.

    点赞 评论 复制链接分享

相关推荐