Skip to content
This repository was archived by the owner on Nov 8, 2024. It is now read-only.
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

Closed

Description

@peterstrapp

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".

runner.before('executeTransaction', function(transaction, callback) {
    return runHooksForTransaction(hooks.beforeHooks['ALL'], transaction, callback);
});

Activity

  1. ecordell commented on Mar 19, 2014

    @ecordell
    Contributor

    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.

  2. peterstrapp commented on Mar 19, 2014

    @peterstrapp
    Author

    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.

  3. ecordell commented on Mar 19, 2014

    @ecordell
    Contributor

    Transactions are exposed in hooks so 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()
  4. peterstrapp commented on Mar 20, 2014

    @peterstrapp
    Author

    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.

  5. ecordell commented on Apr 1, 2014

    @ecordell
    Contributor

    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?

  6. peterstrapp commented on Apr 2, 2014

    @peterstrapp
    Author

    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

    {before, after, transactions} = require 'hooks'
    console.log transactions
    
  7. BrianHicks commented on Aug 1, 2014

    @BrianHicks

    I'm seeing this too, also on Dredd v0.3.1.

  8. vadim-hbo commented on Sep 15, 2014

    @vadim-hbo

    Ditto Dredd 0.3.9

  9. dbryand commented on Dec 1, 2014

    @dbryand

    I'm doing something very similar to this, but I set it all up in the beforeAll hook:

    var hook  = require('hooks');
    
    // This patches Dredd so that we can run a function beforeEach.
    function setBeforeEach() {
      _.each(hook.transactions, function(transaction) {
        var hooked = 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();
    });
  10. conord33 commented on Dec 8, 2014

    @conord33

    Awesome patch @dbryand! I generalized it, in coffeescript, a little more to be used like the before hook.

    # before each patch
    hooks.beforeEach = (hook) -> 
      hooks.beforeAll (done) ->
        transactionKeys = Object.keys hooks.transactions
        for key in transactionKeys
          hooked = hooks.beforeHooks[key] || []
          hooked.unshift (transaction, done) ->
            hook(transaction)
            done()
          hooks.beforeHooks[key] = hooked
        done()

    The method can be called like this

    hooks.beforeEach (transaction) ->
      hookToRunBeforeEachTest()
  11. 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
  12. kuba-kubula commented on Mar 20, 2015

    @kuba-kubula
    Contributor

    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 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?

    cc. @conord33 @dbryand @peterstrapp @ecordell @BrianHicks

  13. added a commit that references this issue on Oct 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions