DEV Community

ryosukes
ryosukes

Posted on

4 1

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.

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay