Skip to content
This repository was archived by the owner on Apr 20, 2018. It is now read-only.

Test to verify hooks get chained #117

Merged
merged 1 commit into from
Oct 27, 2016
Merged

Test to verify hooks get chained #117

merged 1 commit into from
Oct 27, 2016

Conversation

daffl
Copy link
Member

@daffl daffl commented Oct 26, 2016

Also adds an error when you try to register an unknown hook type.

@daffl daffl merged commit f9fa513 into master Oct 27, 2016
@daffl daffl deleted the chain-test branch October 27, 2016 00:49
@eddyystop
Copy link
Contributor

eddyystop commented Oct 27, 2016

Run

const feathers = require('feathers');
const hooks = require('feathers-hooks');

const app = feathers()
  .configure(hooks())
  .configure(services);

function services() {
  this.use('/messages', {
    create(data, params) {
      console.log('create. throw', data.throw);
      return data.throw ? Promise.reject(new Error('x')) : Promise.resolve();
    },
  });

  const messages = this.service('messages');

  messages.hooks({
    before: {
      all: () => { console.log('#1 before all'); },
      create: () => { console.log('#1 before create'); },
    },
    after: {
      all: () => { console.log('#1 after all'); },
      create: () => { console.log('#1 after create'); },
    },
    error: {
      all: () => { console.log('#1 error all'); },
    }

  });

  messages.before({
    all: () => { console.log('#1a before all'); },
  });
}

const messages = app.service('messages');

messages.after({
  before: {
    all: () => { console.log('#2 before all'); },
    create: () => { console.log('#2 before create'); },
  },
  after: {
    all: () => { console.log('#2 after all'); },
    create: () => { console.log('#2 after create'); },
  },
  error: {
    all: () => { console.log('#2 error all'); },
  }
});

messages.before({
  all: () => { console.log('#3 before all'); },
  create: () => { console.log('#3 before create'); },
});
messages.after({
  all: () => { console.log('#3 after all'); },
  create: () => { console.log('#3 after create'); },
});

messages.create({ throw: false });

setTimeout(() => {
  console.log('=====');
  messages.create({ throw: true });
}, 100);

and you will see

#1 before all
#1 before create
#1a before all
#3 before all
#3 before create
create. throw false
#1 after all
#1 after create
#3 after all
#3 after create
=====
#1 before all
#1 before create
#1a before all
#3 before all
#3 before create
create. throw true
#1 error all

The hooks logging #2 are not run. Maybe I'm being blind, but I think they should be run.

@daffl
Copy link
Member Author

daffl commented Oct 27, 2016

As we found out in our Slack discussion, the #2 hooks have to be registered via

messages.hooks({
  before: {
    all: () => { console.log('#2 before all'); },
    create: () => { console.log('#2 before create'); },
  },
  after: {
    all: () => { console.log('#2 after all'); },
    create: () => { console.log('#2 after create'); },
  },
  error: {
    all: () => { console.log('#2 error all'); },
  }
});

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants