Skip to content

Commit ab40725

Browse files
committed
Core: Test all factory use cases from intro.js
There is a lot of logic in intro.js; now we test four cases: 1. (implicitly, via QUnit tests) A real browser with window being the global 2. Browserify where there are both global & window variables. 3. Node with jsdom where window is passed manually to the jQuery factory. 4. Pure Node with incorrect window passed; jQuery should throw then. Previously the second & fourth case was not tested and the third was tested in a way that interfered with the main test environment. We now also test if in the Browserify case we're not creating a jQuery global by default. Fixes gh-2181 Closes gh-2234
1 parent ff18d8e commit ab40725

13 files changed

+152
-44
lines changed

Gruntfile.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,9 @@ module.exports = function( grunt ) {
158158

159159
grunt.registerTask( "lint", [ "jsonlint", "jshint", "jscs" ] );
160160

161-
grunt.registerTask( "test_fast", [ "node_smoke_test" ] );
161+
grunt.registerTask( "test_fast", [ "node_smoke_tests" ] );
162162

163-
grunt.registerTask( "test", [ "test_fast", "promises-aplus-tests" ] );
163+
grunt.registerTask( "test", [ "test_fast", "promises_aplus_tests" ] );
164164

165165
// Short list as a high frequency watch task
166166
grunt.registerTask( "dev", [ "build:*:*", "lint", "uglify", "remove_map_comment", "dist:*" ] );

build/tasks/lib/spawn_test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/* jshint node: true */
2+
3+
"use strict";
4+
5+
// Run Node with provided parameters: the first one being the Grunt
6+
// done function and latter ones being files to be tested.
7+
// See the comment in ../node_smoke_tests.js for more information.
8+
module.exports = function spawnTest( done ) {
9+
var testPaths = [].slice.call( arguments, 1 ),
10+
spawn = require( "win-spawn" );
11+
12+
spawn( "node", testPaths, { stdio: "inherit" } )
13+
.on( "close", function( code ) {
14+
done( code === 0 );
15+
} );
16+
} ;

build/tasks/node_smoke_test.js

Lines changed: 0 additions & 16 deletions
This file was deleted.

build/tasks/node_smoke_tests.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
module.exports = function( grunt ) {
2+
3+
"use strict";
4+
5+
var fs = require( "fs" ),
6+
spawnTest = require( "./lib/spawn_test.js" ),
7+
testsDir = "./test/node_smoke_tests/",
8+
nodeSmokeTests = [];
9+
10+
// Fire up all tests defined in test/node_smoke_tests/*.js in spawned sub-processes.
11+
// All the files under test/node_smoke_tests/*.js are supposed to exit with 0 code
12+
// on success or another one on failure. Spawning in sub-processes is
13+
// important so that the tests & the main process don't interfere with
14+
// each other, e.g. so that they don't share the require cache.
15+
16+
fs.readdirSync( testsDir )
17+
.filter( function( testFilePath ) {
18+
return fs.statSync( testsDir + testFilePath ).isFile();
19+
} )
20+
.forEach( function( testFilePath ) {
21+
var taskName = "node_" + testFilePath.replace( /\.js$/, "" );
22+
23+
grunt.registerTask( taskName, function() {
24+
spawnTest( this.async(), "test/node_smoke_tests/" + testFilePath );
25+
} );
26+
27+
nodeSmokeTests.push( taskName );
28+
} );
29+
30+
grunt.registerTask( "node_smoke_tests", nodeSmokeTests );
31+
};

build/tasks/promises-aplus-tests.js

Lines changed: 0 additions & 20 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module.exports = function( grunt ) {
2+
3+
"use strict";
4+
5+
var spawnTest = require( "./lib/spawn_test.js" );
6+
7+
grunt.registerTask( "promises_aplus_tests", function() {
8+
spawnTest( this.async(),
9+
"./node_modules/.bin/promises-aplus-tests",
10+
"test/promises_aplus_adapter.js"
11+
);
12+
} );
13+
};

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@
5252
"requirejs": "2.1.15",
5353
"sinon": "1.10.3",
5454
"sizzle": "2.2.0",
55-
"testswarm": "1.1.0"
55+
"testswarm": "1.1.0",
56+
"win-spawn": "2.0.0"
5657
},
5758
"scripts": {
5859
"build": "npm install && grunt",
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* jshint node: true */
2+
3+
"use strict";
4+
5+
var ensureGlobalNotCreated = require( "./lib/ensure_global_not_created" ),
6+
jQueryFactory = require( "../../dist/jquery.js" );
7+
8+
try {
9+
jQueryFactory( {} );
10+
console.error( "The jQuery factory should reject window without a document" );
11+
process.exit( 1 );
12+
} catch ( e ) {
13+
if ( e.message === "jQuery requires a window with a document" ) {
14+
ensureGlobalNotCreated( module.exports );
15+
process.exit( 0 );
16+
}
17+
console.error( "An unexpected error thrown; message: ", e.message );
18+
process.exit( 1 );
19+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* jshint node: true */
2+
3+
"use strict";
4+
5+
require( "jsdom" ).env( "", function( errors, window ) {
6+
if ( errors ) {
7+
console.error( errors );
8+
process.exit( 1 );
9+
}
10+
11+
var ensureJQuery = require( "./lib/ensure_jquery" ),
12+
ensureGlobalNotCreated = require( "./lib/ensure_global_not_created" ),
13+
jQuery = require( "../../dist/jquery.js" )( window );
14+
15+
ensureJQuery( jQuery );
16+
ensureGlobalNotCreated( module.exports );
17+
} );
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* jshint node: true */
2+
3+
"use strict";
4+
5+
require( "jsdom" ).env( "", function( errors, window ) {
6+
if ( errors ) {
7+
console.error( errors );
8+
process.exit( 1 );
9+
}
10+
11+
// Pretend the window is a global.
12+
global.window = window;
13+
14+
var ensureJQuery = require( "./lib/ensure_jquery" ),
15+
ensureGlobalNotCreated = require( "./lib/ensure_global_not_created" ),
16+
jQuery = require( "../../dist/jquery.js" );
17+
18+
ensureJQuery( jQuery );
19+
ensureGlobalNotCreated( module.exports, window );
20+
} );

0 commit comments

Comments
 (0)