Skip to content

add after.always and afterEach.always hooks - fixes #474 #806

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 8, 2016
Merged

add after.always and afterEach.always hooks - fixes #474 #806

merged 10 commits into from
May 8, 2016

Conversation

cgcgbcbc
Copy link
Contributor

@cgcgbcbc cgcgbcbc commented May 3, 2016

This pull request aims at #474 .
The new tests added in test/hooks.js passed, but these changes lead to other tests' failure.
Still working on figuring out why, comments are welcome!

@mention-bot
Copy link

By analyzing the blame information on this pull request, we identified @BarryThePenguin, @naptowncode and @sotojuan to be potential reviewers

@cgcgbcbc cgcgbcbc changed the title [WIP] add after.always, afterEach.always hook, resolve #474 add after.always, afterEach.always hook, resolve #474 May 3, 2016
});
});

test('after each not run if serial tests failed', function (t) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

after each => afterEach

@jamestalmage
Copy link
Contributor

We should add a test verifying what happens with afterEach.always if a beforeEach fails.

I'm not even sure what that should be!

@cgcgbcbc
Copy link
Contributor Author

cgcgbcbc commented May 4, 2016

@jamestalmage I've added tests verifying that after.always, afterEach.always run if before or beforeEach fails.

@jamestalmage
Copy link
Contributor

Just FYI, we use the new GitHub "squash and merge" feature on almost every PR, so you don't need to self squash or use commit --amend, etc. It actually makes it harder for me to review just the changes you've made in response to my comments. (Not a big deal, though).

// add a always hook
if (metadata.always) {
if (type !== 'after' && type !== 'afterEach') {
throw new Error('"always" cannot be used with a ' + type + ' test');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cgcgbcbc - Can you add a few tests in test/test-collection.js for this throwing behavior?

@jamestalmage
Copy link
Contributor

@cgcgbcbc - You are real close. The implementation looks good to me, we just need a few more tests.

👍

@cgcgbcbc
Copy link
Contributor Author

cgcgbcbc commented May 6, 2016

@jamestalmage Tests added.

The after.always and alfterEach.always will always execute after after hook and afterEach hook respectively , no matter the order they are added, since they are implemented by wrapping bail sequence with non-bail one.

@jamestalmage
Copy link
Contributor

The after.always and afterEach.always will always execute after after hook and afterEach hook respectively

I don't think that's a problem.

@@ -418,9 +418,9 @@ test.todo('will think about writing this later');

AVA lets you register hooks that are run before and after your tests. This allows you to run setup and/or teardown code.

`test.before()` registers a hook to be run before the first test in your test file. Similarly `test.after()` registers a hook to be run after the last test.
`test.before()` registers a hook to be run before the first test in your test file. Similarly `test.after()` registers a hook to be run after the last test. `test.after.always()` registers a hook to be run **always** after the last test, even if before hook/some tests fail.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about:

Use test.after.always() to register an after hook that is called even if tests or other hooks fail.

Copy link
Contributor

@jamestalmage jamestalmage May 6, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use test.after.always() to register a hook that will always run once your tests and other hooks complete. .always() hooks run regardless of whether there were earlier failures, so they are ideal for cleanup tasks.

@novemberborn
Copy link
Member

I think you should check for metadata.always outside of the type !== 'test' condition. Currently AVA won't complain if you use say test.only.always(). It would also be good if the error message conveyed which combinations are valid, e.g.:

if (metadata.always && type !== 'after' && type !== 'afterEach') {
    throw new Error('"always" can only be used with after and afterEach hooks');
}

if (type !== 'test') {
    if (metadata.exclusive) {
        throw new Error('"only" cannot be used with a ' + type + ' test');
    }

    this.hooks[type + (metadata.always ? 'Always' : '')].push(test);
}

Would be good to test this error with test and beforeEach as well.

(Maybe we should change "test" in the "only" message to "hook" too.)


@cgcgbcbc almost there!

I am wondering though whether this should just be the default behavior. Will discuss further in #474.

@jamestalmage
Copy link
Contributor

@cgcgbcbc - All that's really left here are a few minor nits on the docs, etc.

If you can handle those, I think we're good to merge. If anything we've said is ambiguous and you can't get an answer from us right away, just use your best judgement.

@cgcgbcbc
Copy link
Contributor Author

cgcgbcbc commented May 7, 2016

@jamestalmage Updated according to the comments

@sindresorhus sindresorhus changed the title add after.always, afterEach.always hook, resolve #474 add after.always and afterEach.always hooks - fixes #474 May 8, 2016
@sindresorhus
Copy link
Member

Nice work @cgcgbcbc :)

LGTM

@cgcgbcbc
Copy link
Contributor Author

cgcgbcbc commented May 16, 2016

@jamestalmage should I open an issue in eslint-plugin-ava about this modifier?

@jamestalmage
Copy link
Contributor

If one doesn't already exist, yes.

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

Successfully merging this pull request may close these issues.

5 participants