DEV Community

ryosukes
ryosukes

Posted on

Show diff when dredd was failed by unexpected body

If dredd was failed by unexpected body, we can show some message, actual response and expected response on cli. But, diff is not shown on cli then.

currently there is no diff, Dredd displays expected response and the real response and leaves it up to the user to do the diff in their head

https://github.com/apiaryio/dredd/issues/765

If you want to show diff, there is a need to insert diff into transaction.fail like this(using node).

var hooks = require('hooks');
var diff  = require('diff'); // need diff package

hooks.beforeEachValidation(function (transaction) {
    var real     = transaction.real.body;
    var expected = transaction.expected.body;

    if (real !== expected) {
        transaction.fail = diff.createPatch(
            transaction.id + " failed diff",
            JSON.stringify(JSON.parse(real), undefined, 4),
            JSON.stringify(JSON.parse(expected), undefined, 4),
            "real",
            "expected"
        );
    }
});
Enter fullscreen mode Exit fullscreen mode

cf: Failing Tests Programmatically

If there is a better way, please tell me how to do itπŸ™‚

Thank you.

Top comments (0)