Skip to content

Commit

Permalink
Log a warning if the user adds a test to an unexpected group
Browse files Browse the repository at this point in the history
I'm having a hard time thinking of a case where someone would do this deliberately

...I guess you could make an argument that tests should always be added to the immediate scope, and to the root canary instance if there was none? idk I'm not convinced. the code looks ugly to me when I do that. I think "this" is more natural and easy to understand at a glance
  • Loading branch information
pineapplemachine committed Mar 29, 2018
1 parent b5c66cd commit bfc0b95
Showing 1 changed file with 17 additions and 29 deletions.
46 changes: 17 additions & 29 deletions canary.js
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,16 @@ class CanaryTest{
test.isSilent = this.isSilent;
test.isVerbose = this.isVerbose;
test.logFunction = this.logFunction;
// Log a verbose warning for a very possible mistake
const currentGroup = CanaryTest.currentlyExpandingGroup;
if(currentGroup && currentGroup !== this){
this.log(yellow(
`Warning: Adding test "${name}" to a group other than ` +
`"${currentGroup.getTitle()}" even though the operation is ` +
`taking place in that group's body function. This is ` +
`probably unintended!`
));
}
// All done! Return the produced CanaryTest instance.
return test;
}
Expand Down Expand Up @@ -767,7 +777,12 @@ class CanaryTest{
if(this.isGroup && !this.isExpandedGroup && this.body){
this.expandTime = this.getTime();
this.isExpandedGroup = true;
// Expand the group by evaluating the body function, and record
// whose body function this is in while doing so.
const previousExpandingGroup = CanaryTest.currentlyExpandingGroup;
CanaryTest.currentlyExpandingGroup = this;
this.bodyReturnedValue = this.body(this);
CanaryTest.currentlyExpandingGroup = previousExpandingGroup;
this.logVerbose(
`Test group "${this.name}" has ${this.children.length} ` +
`child tests after expansion.`
Expand Down Expand Up @@ -1235,33 +1250,6 @@ canary.Callback = CanaryTestCallback;
canary.Error = CanaryTestError;
canary.Test = CanaryTest;

module.exports = canary;

// canary.group("Test a thing", function(){
// // this.onBegin("reset the database", canary.resetDatabase);
// // this.onBegin("register and login", canary.registerAndLogin);

// this.logVerbose("Doing the stuff");
// this.tags("someTag");
// this.onBegin("stuff", () => this.logVerbose("begin"));
// this.onEnd("other stuff", () => this.logVerbose("end"));
// this.onEachBegin("more stuff", () => this.logVerbose("begin each"));
// this.onEachEnd("additional stuff", () => this.logVerbose("end each"));
// this.test("Test another thing", function(){
// this.logVerbose("first thing");
// });
// this.test("Only test if the last thing passed", function(){
// this.logVerbose("second thing");
// });
// });
CanaryTest.currentlyExpandingGroup = undefined;

// (async function(){await canary.doReport();})();

// (async function(){
// await canary.doReport({
// // verbose: true,
// paths: ["/Users/pineapple"]
// });
// })();

// canary.doReport();
module.exports = canary;

0 comments on commit bfc0b95

Please sign in to comment.