Skip to content

Commit 7ae3a9f

Browse files
authored
Tests: Add a new esmodules flag, remove the raw one
Migrating from AMD to ES modules in gh-343 made the `raw` testing mode break as it relied on loading raw source files. This change removes the `raw` mode in favor of the new `esmodules` one. Setup is different than in jQuery as jQuery uses AMD to load tests and Migrate just loads test files directly. When Migrate is loaded via its source ES modules, test files are loaded as modules as well. This is necessary as module scripts behave as if loaded with the `defer` attribute so tests need to be deferred as well. Iframe tests are skipped in `esmodules` mode as their setup is incompatible with the above considerations. In addition to the above, Karma tests now also run in esmodules mode for easier verification this mode is not broken by PRs. Closes gh-468 Ref gh-343
1 parent 69a2441 commit 7ae3a9f

8 files changed

+210
-135
lines changed

Gruntfile.js

+82-28
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,43 @@ module.exports = function( grunt ) {
88

99
const oldNode = /^v10\./.test( process.version );
1010

11-
const karmaFilesExceptJQuery = [
11+
const karmaQunitConfig = {
12+
showUI: true,
13+
testTimeout: 5000,
14+
15+
// We're running `QUnit.start()` ourselves in
16+
// test/data/qunit-start.js
17+
autostart: false
18+
};
19+
20+
const karmaFilesExceptJQueryAndMigrate = [
21+
22+
// In esmodules mode only object entries of this array with `type: "js"`
23+
// get the `type: "module"` tweak. npo is incompatible with being loaded as
24+
// a module so leverage this hack to prevent loading it in this way.
1225
"external/npo/npo.js",
13-
"dist/jquery-migrate.min.js",
14-
"test/data/compareVersions.js",
15-
16-
"test/data/testinit.js",
17-
"test/data/test-utils.js",
18-
"test/unit/migrate.js",
19-
"test/unit/jquery/core.js",
20-
"test/unit/jquery/ajax.js",
21-
"test/unit/jquery/attributes.js",
22-
"test/unit/jquery/css.js",
23-
"test/unit/jquery/data.js",
24-
"test/unit/jquery/deferred.js",
25-
"test/unit/jquery/effects.js",
26-
"test/unit/jquery/event.js",
27-
"test/unit/jquery/manipulation.js",
28-
"test/unit/jquery/offset.js",
29-
"test/unit/jquery/serialize.js",
30-
"test/unit/jquery/traversing.js",
31-
32-
{ pattern: "dist/jquery-migrate.js", included: false, served: true },
26+
27+
{ pattern: "test/data/compareVersions.js", type: "js", nocache: true },
28+
29+
{ pattern: "test/data/testinit.js", type: "js", nocache: true },
30+
{ pattern: "test/data/test-utils.js", type: "js", nocache: true },
31+
{ pattern: "test/unit/migrate.js", type: "js", nocache: true },
32+
{ pattern: "test/unit/jquery/core.js", type: "js", nocache: true },
33+
{ pattern: "test/unit/jquery/ajax.js", type: "js", nocache: true },
34+
{ pattern: "test/unit/jquery/attributes.js", type: "js", nocache: true },
35+
{ pattern: "test/unit/jquery/css.js", type: "js", nocache: true },
36+
{ pattern: "test/unit/jquery/data.js", type: "js", nocache: true },
37+
{ pattern: "test/unit/jquery/deferred.js", type: "js", nocache: true },
38+
{ pattern: "test/unit/jquery/effects.js", type: "js", nocache: true },
39+
{ pattern: "test/unit/jquery/event.js", type: "js", nocache: true },
40+
{ pattern: "test/unit/jquery/manipulation.js", type: "js", nocache: true },
41+
{ pattern: "test/unit/jquery/offset.js", type: "js", nocache: true },
42+
{ pattern: "test/unit/jquery/serialize.js", type: "js", nocache: true },
43+
{ pattern: "test/unit/jquery/traversing.js", type: "js", nocache: true },
44+
45+
{ pattern: "test/data/qunit-start.js", type: "js", nocache: true },
46+
47+
{ pattern: "dist/jquery-migrate.js", included: false, served: true, nocache: true },
3348
{ pattern: "test/**/*.@(js|json|css|jpg|html|xml)", included: false, served: true }
3449
];
3550

@@ -182,14 +197,12 @@ module.exports = function( grunt ) {
182197
frameworks: [ "qunit" ],
183198
files: [
184199
"https://releases.jquery.com/git/jquery-3.x-git.min.js",
185-
...karmaFilesExceptJQuery
200+
"dist/jquery-migrate.min.js",
201+
...karmaFilesExceptJQueryAndMigrate
186202
],
187203
client: {
188204
clearContext: false,
189-
qunit: {
190-
showUI: true,
191-
testTimeout: 5000
192-
}
205+
qunit: karmaQunitConfig
193206
},
194207
reporters: [ "dots" ],
195208
autoWatch: false,
@@ -207,11 +220,51 @@ module.exports = function( grunt ) {
207220
options: {
208221
files: [
209222
"https://releases.jquery.com/git/jquery-3.x-git.slim.min.js",
210-
...karmaFilesExceptJQuery
223+
"dist/jquery-migrate.min.js",
224+
...karmaFilesExceptJQueryAndMigrate
211225
]
212226
}
213227
},
214228

229+
esmodules: {
230+
browsers: [ "ChromeHeadless", "FirefoxHeadless" ],
231+
options: {
232+
files: [
233+
"https://releases.jquery.com/git/jquery-3.x-git.slim.min.js",
234+
{ pattern: "src/migrate.js", type: "module" },
235+
236+
// Silence console warnings to avoid flooding the console.
237+
{ pattern: "src/migratemute.js", type: "module" },
238+
239+
// Only object entries with `type: "js"` get
240+
// the `type: "module"` tweak.
241+
...karmaFilesExceptJQueryAndMigrate.map( file => {
242+
if ( file && typeof file === "object" && file.type === "js" ) {
243+
return {
244+
...file,
245+
type: "module"
246+
};
247+
} else {
248+
return file;
249+
}
250+
} ),
251+
252+
{
253+
pattern: "src/**",
254+
included: false,
255+
type: "module",
256+
served: true
257+
}
258+
],
259+
client: {
260+
qunit: {
261+
...karmaQunitConfig,
262+
plugin: "esmodules"
263+
}
264+
}
265+
}
266+
},
267+
215268
// To debug tests with Karma:
216269
// 1. Run 'grunt karma:chrome-debug' or 'grunt karma:firefox-debug'
217270
// (any karma subtask that has singleRun=false)
@@ -247,7 +300,8 @@ module.exports = function( grunt ) {
247300
// Just an alias
248301
grunt.registerTask( "test", [
249302
"karma:main",
250-
"karma:jquery-slim"
303+
"karma:jquery-slim",
304+
"karma:esmodules"
251305
] );
252306

253307
grunt.registerTask( "lint", [

package-lock.json

+7-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"karma-qunit": "4.1.2",
4949
"load-grunt-tasks": "5.1.0",
5050
"native-promise-only": "0.8.1",
51-
"qunit": "2.18.0",
51+
"qunit": "2.19.1",
5252
"rollup": "2.70.1",
5353
"testswarm": "1.1.2"
5454
},

test/data/compareVersions.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Returns 0 if v1 == v2, -1 if v1 < v2, 1 if v1 > v2
2-
function compareVersions( v1, v2 ) {
2+
window.compareVersions = function compareVersions( v1, v2 ) {
33
var i,
44
rVersionParts = /^(\d+)\.(\d+)\.(\d+)/,
55
v1p = rVersionParts.exec( v1 ) || [ ],
@@ -14,8 +14,8 @@ function compareVersions( v1, v2 ) {
1414
}
1515
}
1616
return 0;
17-
}
17+
};
1818

19-
function jQueryVersionSince( version ) {
19+
window.jQueryVersionSince = function jQueryVersionSince( version ) {
2020
return compareVersions( jQuery.fn.jquery, version ) >= 0;
21-
}
21+
};

test/data/qunit-start.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
QUnit.start();

test/data/test-utils.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* exported expectWarning, expectNoWarning */
22

3-
function expectWarning( assert, name, expected, fn ) {
3+
window.expectWarning = function expectWarning( assert, name, expected, fn ) {
44
var result;
55
if ( !fn ) {
66
fn = expected;
@@ -41,10 +41,10 @@ function expectWarning( assert, name, expected, fn ) {
4141
check();
4242
return Promise.resolve();
4343
}
44-
}
44+
};
4545

46-
function expectNoWarning( assert, name, expected, fn ) {
46+
window.expectNoWarning = function expectNoWarning( assert, name, expected, fn ) {
4747

4848
// Expected is present only for signature compatibility with expectWarning
4949
return expectWarning( assert, name, 0, fn || expected );
50-
}
50+
};

0 commit comments

Comments
 (0)