You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Nov 8, 2024. It is now read-only.
Repository navigation
This repository was archived by the owner on Nov 8, 2024. It is now read-only.
Allow a hook to be executed for all transactions (beforeEach hook) #63
Currently hooks have to be applied to a specific transaction.
It would be useful to have "global" hooks that execute before each test, something like this:
before "ALL", (transaction) ->
console.log 'Displayed before each test'
For now I am working around this limitation by calling runHooksForTransaction two more times (once for before and once for after) in add-hooks.js and passing in a fake transaction name of "ALL".
I'm templating parts of the blueprint file, substituting place-holders for values. I can't know upfront what endpoints exist as the blueprint file will be modified by other users. I don't want them to have to modify the hook file each time an endpoint is added.
If there's a way of getting the list of endpoints pragmatically, within the hook file, then your approach would be ideal.
Unfortunately it seems that 'transactions' isn't populated until after the hook file is executed.
Hmm, that shouldn't be true - the whole point of exposing the transactions is so that they're available to use in hookfiles. Are you using the latest version of Dredd? Can you post an example of code that doesn't work?
I'm doing something very similar to this, but I set it all up in the beforeAll hook:
varhook=require('hooks');// This patches Dredd so that we can run a function beforeEach.functionsetBeforeEach(){_.each(hook.transactions,function(transaction){varhooked=hook.beforeHooks[transaction.name]||[];hooked.unshift(function(transaction,done){console.log('seen before each');done();});hook.beforeHooks[transaction.name]=hooked;});}hook.beforeAll(function(done){setBeforeEach();});
changed the title [-]Allow a hook to be executed for all transactions[/-][+]Allow a hook to be executed for all transactions (beforeEach hook)[/+]on Feb 23, 2015
In last month Dredd has undergone some refactoring (it also touched the hooks implementation) so the best approach right now is described in hooks documentation where the beforeAll workaround is taken from pieces of code here and there.
I'll keep this issue open until Dredd has direct support for the beroreEach method, ok?
Activity
ecordell commentedon Mar 19, 2014
Can I ask what you need to do in a hook for every endpoint that can't be done with flags?
There's a fairly easy way to do this right now: run with
--names, copy the list of names, make it an array, and then iterate over them.Here's an example of mine:
{before, after} = require 'hooks' endpoints = [ "Authentication > Users Collection > Create a User", "Authentication > Sessions > Create a Session", # all endpoints ... ] for endpoint in endpoints before endpoint, (transaction) -> delete transaction.expected.headers['Location'] delete transaction.expected.headers['X-Total-Count'] delete transaction.expected.headers['Link']You can have multiple hooks for the same name, so it won't affect anything else.
peterstrapp commentedon Mar 19, 2014
I'm templating parts of the blueprint file, substituting place-holders for values. I can't know upfront what endpoints exist as the blueprint file will be modified by other users. I don't want them to have to modify the hook file each time an endpoint is added.
If there's a way of getting the list of endpoints pragmatically, within the hook file, then your approach would be ideal.
ecordell commentedon Mar 19, 2014
Transactions are exposed in
hooksso you can do something like:{before, after, transactions} = require 'hooks' endpoints = transactions.map (currentValue, index, array) -> currentValue.name for endpoint in endpoints before (transaction, done) -> #do something done()peterstrapp commentedon Mar 20, 2014
Thanks for the suggestion, I've just given it a try. Unfortunately it seems that 'transactions' isn't populated until after the hook file is executed.
ecordell commentedon Apr 1, 2014
Hmm, that shouldn't be true - the whole point of exposing the transactions is so that they're available to use in hookfiles. Are you using the latest version of Dredd? Can you post an example of code that doesn't work?
peterstrapp commentedon Apr 2, 2014
Yes, I'm using Dredd v0.3.1.
Executing the below hook file prints an empty object ("{}") therefore, when attempting to execute map on it (as in the code above) it fails.
dredd test.md http://a.domain.com -d -m POST --hookfiles=hook.coffee -l error
BrianHicks commentedon Aug 1, 2014
I'm seeing this too, also on Dredd v0.3.1.
vadim-hbo commentedon Sep 15, 2014
Ditto Dredd 0.3.9
dbryand commentedon Dec 1, 2014
I'm doing something very similar to this, but I set it all up in the
beforeAllhook:conord33 commentedon Dec 8, 2014
Awesome patch @dbryand! I generalized it, in coffeescript, a little more to be used like the before hook.
The method can be called like this
[-]Allow a hook to be executed for all transactions[/-][+]Allow a hook to be executed for all transactions (beforeEach hook)[/+]kuba-kubula commentedon Mar 20, 2015
Hi everyone,
In last month Dredd has undergone some refactoring (it also touched the hooks implementation) so the best approach right now is described in hooks documentation where the
beforeAllworkaround is taken from pieces of code here and there.I'll keep this issue open until Dredd has direct support for the
beroreEachmethod, ok?cc. @conord33 @dbryand @peterstrapp @ecordell @BrianHicks
Merge pull request #63 from apiaryio/honzajavorek/unique-transaction-…