-
Notifications
You must be signed in to change notification settings - Fork 779
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
Nested modules #859
Nested modules #859
Conversation
|
||
QUnit.test( "modules with nested functions does not spread beyond", function( assert ) { | ||
assert.equal( assert.test.module.name, "pre-nested modules" ); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jzaefferer @gibson042 I need opinions on this test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems ok to me. The interaction itself sucks, but the test captures it well.
81427c7
to
d21ecf5
Compare
@gibson042 I made two commits to include arguments on the contained modules callback. I appreciate if you review and criticize. cc @jzaefferer |
|
||
beforeEach( function( assert ) { | ||
assert.ok( true, "beforeEach called" ); | ||
} ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is even better on ES6:
QUnit.module( "foo", ( test, { beforeEach, afterEach } ) => {
beforeEach: () => {
...
};
...
});
Also, a funny fact:
const describe = QUnit.module;
describe( "wat", it => {
it( "works", ( { ok } ) => {
ok( true, "omg" );
});
});
@@ -0,0 +1,312 @@ | |||
QUnit.urlParams = urlParams; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm assuming these .js_
files are accidental additions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
caught in a git add .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure about these particular parameters. As I've noted elsewhere, I'd like In my opinion, it's better to pass nothing at all now than it is to guess wrong about the signature. Since this iteration still requires access to the global QUnit.module("outer", function( outerModule ) {
// environment (prototype for test/submodule context)
this.outer = true;
this.outerBefore = 0;
// test before/after
outerModule.test.beforeEach(function() {
this.outerBefore++;
});
// tests
outerModule.test("foo", function( assert ) {
assert.equal(this.outerBefore, 1, "beforeEach updated environment");
});
outerModule.test("bar", function( assert ) {
assert.equal(this.outerBefore, 1, "sibling tests don't share environments");
});
// submodules
outerModule.module("inner", function( innerModule ) {
// environment (inherits from parent)
this.inner = true;
function assertEnvironment( assert, before ) {
assert.ok(this.outer, "outer module initialized environment");
assert.ok("outerBefore" in this, "outer module beforeEach updated environment");
assert.equal(this.outerBefore, 1, "submodules don't share environments");
assert.ok(this.inner, "inner module initialized environment");
if ( before ) {
return;
}
assert.ok(this.innerBefore, "inner module beforeEach updated environment");
assert.ok(this.tested, "test updated environment");
}
// test before/after
innerModule.test.beforeEach(function( assert ) {
this.innerBefore = true;
assertEnvironment.call( this, assert, true );
});
innerModule.test.afterEach(function( assert ) {
assertEnvironment.call( this, assert );
});
// tests
innerModule.test("bar", function( assert ) {
this.tested = true;
assertEnvironment.call( this, assert );
});
});
}); |
QUnit.module("outer", function( outerModule ) {
// environment (prototype for test/submodule context)
this.outer = true;
this.outerBefore = 0;
// test before/after
outerModule.test.beforeEach(function() {
this.outerBefore++;
});
// tests
outerModule.test("foo", function( assert ) {
assert.equal(this.outerBefore, 1, "beforeEach updated environment");
});
outerModule.test("bar", function( assert ) {
assert.equal(this.outerBefore, 1, "sibling tests don't share environments");
});
... These
And reviewing my code I'll indeed remove the first And following your own example, I would stick with the first argument being an object with the hooks methods and it can later being extended. This way: QUnit.module("outer", function( outerModule ) {
...
outerModule.beforeEach(function() {
...
});
outerModule.afterEach(function() {
...
}); but also, later in further additions: QUnit.module("outer", function( t ) {
t.beforeEach(function() {
...
});
t.test( "the test method", function( assert ) {
...
});
}); and using ES6: QUnit.module("outer", ( { beforeEach } ) => {
beforeEach(function() {
...
});
QUnit.test( "the test method", function( assert ) {
...
});
}); The reason to start with QUnit.module( "outer", {
beforeEach: function( assert ) {
...
},
afterEach: function( assert ) {
...
}
},
function( t ) {
QUnit.test( "the test method", function( assert ) {
...
});
}
); |
@gibson042: both 29f5b84 and 53778e3 has the shared The I also removed the test argument as you can see on the fixup commits. We may then extend the module callback argument to include |
|
||
var env = {}; | ||
if ( parentModule ) { | ||
extend( env, parentModule.testEnvironment ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think Object.create
-style inheritance would be better, but am willing to shelve that for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would love to do this. We still support ES3 until QUnit 2.0.0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Arbitrary inheritance is possible in ES3, too:
var createObject = (function( Base ) {
return function( prototype ) {
Base.prototype = prototype;
return new Base();
};
})( Function() );
…
env = createObject( parentModule.testEnvironment );
The above is similar to a stripped-down version of lodash's baseCreate
, but you could easily go all the way to _.create
if you want this part to look like module.testEnvironment = createObject( parentModule.testEnvironment, testEnvironment )
(removing the need for a separate extend
).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is cool, but I would leave this improvement for later as this requires a whole new review. Does it works for you?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep.
👍 The single callback argument is a decent (if limited) module stand-in and has good room for growth, and you are absolutely right about this:
Let's hope that |
I think this is good to go; module environment inheritance can be addressed with a subsequent issue & PR. |
Opened #869 to follow up with this. |
Ref qunitjs/qunit#859 Fixes qunitjs#109 Closes qunitjs#111
Allows modules to be nested. This is achieved by adding a function argument to the QUnit.module method, which is a function that is called for immediate processing. Also, testEnvironments for parent modules are invoked in recursively for each test (outer-first). Fixes qunitjs#543 Closes qunitjs#800 Closes qunitjs#859 Closes qunitjs#670 Ref qunitjs#858
ce70ce9
to
f47114b
Compare
Replaces/Closes #800
Closes #543
This PR closes #800 with extra additions to the PR to assure a good landing of nested modules.
We might want to observe the issues listed at #858 and add extra commits
cc @gibson042