Dredd: Ability to specify order of the transactions

Created on 21 Apr 2016  路  14Comments  路  Source: apiaryio/dredd

current:
Dredd runs the transactions as they鈥檙e specified in blueprint.
expected:
ability to define transactions order. Othervise 2 blueprints which are sematicaly the same could behave differently when sharing data across the transactions

Most helpful comment

I ran into the same issue today, I ended up using the beforeAll hook

var order = [
    // Your API names in the wanted order of execution
]

hooks.beforeAll(function(transactions, done) {
    transactions.sort(function(a, b) {
        var aIdx = order.indexOf(a.name)
        var bIdx = order.indexOf(b.name)
        return aIdx - bIdx
    })
    done()
})

All 14 comments

@tu1ly Cool idea :) Did this cause an issue for you? Would it be difficult to keep track of two lists of transactions鈥攐ne in the blueprint and one in your Dredd hooks/configuration?

Would the ability to randomize the tests be a solution here?

Maybe the "only" option's array order could be used as first implementation?

I ran into the same issue today, I ended up using the beforeAll hook

var order = [
    // Your API names in the wanted order of execution
]

hooks.beforeAll(function(transactions, done) {
    transactions.sort(function(a, b) {
        var aIdx = order.indexOf(a.name)
        var bIdx = order.indexOf(b.name)
        return aIdx - bIdx
    })
    done()
})

@Souler This is an interesting solution!

Just to add to the discussion, Dredd is primarily a tool designed to perform isolated tests and hooks are here to help you to isolate each test. See docs on this.

If you need to order your transactions, it's a sign you're not creating isolated tests. YMMV if that's good or bad.

@Souler Thank you very much! You saved my day!

I ran into the same issue today, I ended up using the beforeAll hook

var order = [
    // Your API names in the wanted order of execution
]

hooks.beforeAll(function(transactions, done) {
    transactions.sort(function(a, b) {
        var aIdx = order.indexOf(a.name)
        var bIdx = order.indexOf(b.name)
        return aIdx - bIdx
    })
    done()
})

Not working for me @dredd v12.0.7

@shivshankar3578 Could you share more in a separate issue with a full bug report? This should work.

let testFlow = [
  "GET /services/ug/{id}",
  "POST /services/ug",
  "POST /services/account/add_user",
  "POST /services/ug/user",
  ]


hooks.beforeAll(function(transactions, done) {
  hooks.log("before all");
  transactions = _(transactions)
  .map(item=> {
    item.name = item.request.method+" "+item.origin.resourceName
    return item
  })
  .filter(item=>{
    return testFlow.indexOf(item.name) >= 0
  })
  .sortBy(item=>{
    return testFlow.indexOf(item.name)
  })
  .value()

  done();
});

And in

hooks.beforeEach(function(transaction, done) {

I'm not getting GET /services/ug/{id} First
Tried with done(transactions), done(null, transactions), hooks.configuration.only = transactionsNames but nothing works

What does _(transactions) do? Does it copy the array? You need to modify the original transactions array.

No, it doesn't and I'm reassigning transactions again after modification. It's not immutable also.The Array functions (filter, sort or map) return a new modified array so I need to reassigning it again.

It cannot be reassigned. This is a hack which counts with the fact that you modify the original array, the one transactions variable references to. So transactions = won't work, because ten transactions will reference to a different array in memory. This could help you to achieve what you need.

Got it, but it's a bit confusing, Either you should freeze the array or impl. it using immutable way

It is confusing, it is a kind of a hack. As you say, ideally there would be a nice immutable interface to achieve this. We're working on decoupling things in Dredd internally which would allow that in the long run.

Was this page helpful?
0 / 5 - 0 ratings