Skip to content

Conversation

@asafigan
Copy link
Contributor

This is my first pull request so any feedback is welcome.

fixes #948

Added a test in the runner test file which expects an error to be thrown when test is called in a test. I don't know if this is the right file to put this test, but it seem like a good place since my solution only changed the runner.js file.

My solution was to add a new property to runner called 'hasBegunRunning' which is set to true in the run method. The chaining function checks this property and throws an error when it is true.

I don't know if this is a good solution but it is simple and it works. Also, I don't know if I need more tests for this pull request.

lib/runner.js Outdated
var macroArgIndex;

if (this.hasBegunRunning) {
throw new Error('Cannot have nested tests or hooks');
Copy link
Contributor

Choose a reason for hiding this comment

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

This could also happen with:

test(async t => {
  // stuff
});

setImmediate(() => {
  test(t => {
    // not allowed!
  });
});

The message should include something about declaring tests async.

All tests and hooks must be declared synchronously in your test file, and cannot be nested within other tests.

@jamestalmage
Copy link
Contributor

jamestalmage commented Jul 12, 2016

I don't know if I need more tests for this pull request.

Add a second one that tests what I talked about above, and I think you are covered.

// test
var r = new Runner();

r.test(() => Promise.resolve());
r.run()

t.throws(() => {
  r.test(() => {});
}, {message: ...})

This is my first pull request

One of the best first PR's I have seen. Very good job. 🎉

@asafigan
Copy link
Contributor Author

also I didn't know what type of error to throw

@asafigan
Copy link
Contributor Author

Ok, I will add that test and also tweak the error message and the second test

@asafigan
Copy link
Contributor Author

Ok I'm gone with those changes. Let me know if there is anything else I need to change.

@sotojuan
Copy link
Contributor

👍 great job!

@asafigan
Copy link
Contributor Author

thanks!

@jamestalmage
Copy link
Contributor

LGTM

lib/runner.js Outdated

this.results = [];
this.tests = new TestCollection();
this.hasBegunRunning = false;
Copy link
Member

Choose a reason for hiding this comment

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

I would use isRunning instead.

Copy link
Contributor Author

@asafigan asafigan Jul 13, 2016

Choose a reason for hiding this comment

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

I didn't name it isRunning because it isn't reset to false after the tests are done running.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

but I don't like hasBegunRunning either

Copy link
Member

Choose a reason for hiding this comment

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

hasStarted?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I like that better

Copy link
Member

Choose a reason for hiding this comment

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

Let's go with that then.

@sindresorhus sindresorhus changed the title no nested tests #948 prevent nesting tests #948 Jul 13, 2016
@sindresorhus sindresorhus changed the title prevent nesting tests #948 prevent nested tests #948 Jul 13, 2016
@asafigan
Copy link
Contributor Author

Ok, I changed the property name.

@asafigan
Copy link
Contributor Author

Also should we mention in the docs somewhere that you shouldn't have embedded tests or hooks. And also that we have no alternative to describe.

@jamestalmage jamestalmage merged commit 8edd9c2 into avajs:master Jul 14, 2016
@jamestalmage
Copy link
Contributor

Thanks @asafigan!

Also should we mention in the docs somewhere that you shouldn't have embedded tests or hooks. And also that we have no alternative to describe.

That probably belongs in a "transitioning from mocha/tape" recipe. Not sure it belongs in the core docs.

@asafigan asafigan deleted the pr/no-nested-tests-issue-948 branch July 14, 2016 02:52
@asafigan
Copy link
Contributor Author

woot! first contribution! 🎉
Thanks for all of the help on this

@sotojuan
Copy link
Contributor

Awesome work @asafigan!

@sindresorhus
Copy link
Member

Super first PR. Thanks @asafigan! Keep 'em coming ;)

68747470733a2f2f6d656469612e67697068792e636f6d2f6d656469612f695054546a4574313969676e652f67697068792e676966

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.

Nested test functions hide errors

4 participants