Follows-up Id6d13bbea6:
- '$': mw.loader.implement does this already.
- 'mw': Use the canonical name directly.
This replaces the following patterns:
File closures (common):
- `( function ( $, mw ) {` => `( function () {`
- `( function ( $ ) {` => `( function () {`
- `( function ( mw ) {` => `( function () {`
- `( function ( mw, $ ) {` => `( function () {`
File closures (rare):
- `( function ( mw, $, OO ) {` => `( function () {`
- `( function ( mw, OO, $ ) {` => `( function () {`
- `( function ( mw, document ) {` => `( function () {`
Combined dom-ready and file closure (rare):
- `jQuery( function ( $ ) {` => `$( function () {
- `jQuery( function () {` => `$( function () {
Remaining references in files without a closure, as found by
the new ESLint setting (rare):
- `jQuery` => `$`
- `mediaWiki` => `mw`
Change-Id: I7cf2426cde597259e8c6f3f6f615a1a81a0ca82b
Even though Array.prototype.forEach only works on arrays, and
$.each is more generic, I think it makes sense to begin discouraging
the usage of $.each now. This can be overriden by ignore lines or
by Array.prototype.forEach compatible lines. This doesn't seem too
much of an ask of engineers and helps future migrations
Bug: T200877
Change-Id: I339cff311a830699c8e32f07cec338a39870c53f
Follows-up 5a49381406, 43dc5c1539, 2454f51b27.
* Merge the three QUnit.module extensions into one.
* Change makeSafeEnv() to use Object.create() instead of creating
a simplified objects so that other properties are still accessible.
The 'testrunner-nested > Dummy' test now actually runs,
previously it was lost.
Change-Id: Id4aeb93582f8cc73b0dffe768a7864002ec85deb
Replace
* $.each( array, function ( index, value ) { ... } ) by
array.forEach( function ( value, index ) { ... } )
* $.grep( array, function ( value ) { ... } ) by
array.filter( function ( value ) { ... } )
* $.map( array, function ( value ) { ... } ) by
array.map( function ( value ) { ... } )
This change is a follow-up to 9d67e9973e.
Change-Id: I8ef9af8c4d2f440faca65ec7c78a977ea7c31ad2
* Nested modules:
- Support for Sinon extension was fixed by Ib17bbbef45b2bd.
- Support for Fixture extension was still broken, masked by the use
of a local variable that made the handler not fail when setup ran twice
in a row. Fixed using the same moduleStack.length check.
- Add regression test.
* beforeEach/afterEach:
- Added in 1.16, with compat for setup/teardown.
Our wrapper adds its own setup/teardown, and preserves any original one.
However, it didn't account for beforeEach/afterEach, so it ends up
sending both but only one is used.
- Fix to support both on the incoming localEnv object, and also switch
our wrapper to use beforeEach/afterEach in prep for QUnit 2.0.
- Fix our wrappers to preserve return value since QUnit 2 allows beforeEach
and afterEach hooks to be asynchronous by returning a Promise, similar
to how one can do from QUnit.test().
- Add regression test.
* Centralise makeSafeEnv logic
- We always create our own env object to pass to orgModule().
Document why this is (to avoid recursion).
- Add regression test.
* Custom assertion methods:
- Use this.pushResult instead of the deprecated QUnit.push() method.
This also improves the in-browser reporting of errors by properly
supporting 'negative' results for notHtmlEqual reporter.
Bug: T170515
Change-Id: If4141df10eae55cbe8a5ca7a26707be1cd7b9217
Follows-up 0a208911a2, which added support for the `executeNow`
parameter to QUnit.module.
To properly support nested modules, we also need to skip registering
a second setup and teardown because nested modules already run the
beforeEach (setup), and afterEach (teardown), of their parent modules.
During setup this would needlessly create two sandboxes and override
the 'sandbox' property on the same 'this' context object. During
teardown it would fail because the inner module's teardown would
have already torn down the sandbox.
Change-Id: Ib17bbbef45b2bd0247979cf0fa8aed17800c54a0
Stopping the animation is essentially the same as pausing, it remains in the
animation registry and will continue to fail all subsequent tests until one
of those later tests starts the animation queue again and also happens to
wait long enough for this unrelated animation to finish.
Fix the testrunner to actually fully stop the pre-existing animations, which
requires clearing $.timers as well. This matches the logic we have for
pending ajax requests.
Bug: T163211
Change-Id: Ic7d848187bc3c800e8347e0650093b2ffce6dddc
* Move `restoreWarnings()` in tearDown() to the mirrored location
of related code in setUp().
* Ensure that accidentally calling `suppressWarnings()` twice will
not wipe out the original reference indefinitely. If it was
already set, subsequent calls should do nothing instead of
overwriting them again so that recovery is still possible.
* Log all ajax requests logged during the test, not just the
one currently still pending. This should avoid situations
where we throw "Pending ajax requests" but no information
is logged about which requests those might be.
Change-Id: I900ad98c4c8520bdd6ae00a24ac82272f3becfee
The CompletenessTest was my attempt at measuring a basic code coverage
using run-time inspection instead of static instrumentation.
Originally added in 540419a82e (2010; MediaWiki 1.17).
It was never finished, remained fairly buggy and disabled by default.
It is also no longer used anywhere.
Bug: T155194
Change-Id: I26e7466426dddb43596f402e31005a89060c1b96
Follows-up c0fb8a8836, I890e6e49b.
* Disable 'qunit' env in general source code. And re-declare
locally in the few src files that use it properly.
* Create separate eslint config for tests/qunit with various
rules disabled (e.g. valid-jsdoc and es3-keywords).
Change-Id: I37ccec2019de55edfee92697eb80478df7cb6220
QUnit used to have bad state management (a few years ago) at which point it
became useful to verify the number of assertions in case an asynchronous
failure happened, as it would likely go unnoticed.
* Errors outside testStart/testEnd weren't caught.
QUnit now monitors window.onerror.
* Assertions could be attributed to the wrong test.
QUnit no longer does this since the assert object is associated with
the current test through lexical scope.
* assert.async()/done() replaced global semaphore (QUnit.start).
* A test could forget to be marked as async and make no assertions.
QUnit now marks a test as failed if it makes 0 assertions.
QUnit also has built-in async tracking for promises.
If a test is not reaching all assertions for some reason, this
will cause an error of some sort that is tracked. If in some
specific scenario this isn't the case, assert.expect() can still
be called (e.g. when expecting 0 assertions), but it'd be worthwhile
to file an upstream bug report in that case.
Follows-up 7c363752, which removed 'QUnit.config.requireExpects' from
our test configuration.
Follows-up c4c7007de6 and various other commits that already removed
the test counts from a subset tests. This commit removes the remainder.
Change-Id: Ie58396ba9c83d27220508481cb97c0fa74487756
As of jQuery 1.12, animations use requestAnimationFrame in modern
browsers, which cannot be forced to finish synchronously by merely
mocking the 'setTimeout' and 'Date' clocks via Sinon.
For jquery.color, reduce duration from 10ms to 3ms (not 10ms which will
now be real time, not 0ms since we do want to test real frames).
Change-Id: Ie147fc2a91d2cd349b4031390f3c59b1bcfb65b5
This has never been used outside mediawiki.js for things like mw.config,
and mw.messages.
The public API of those objects will remain, but the constructor will
no longer be supported in the future in order to allow these to be
backed by native Map in the future (with a minimal polyfill rather than
a complete one).
* Remove 'mw.Map(valuesObject)'. (follows-up 63f8d7b9e5).
This is incompatible with native Map and isn't actually used anywhere.
* Deprecate mw.Map#values. Undocumented and considered private.
A few uses have appeared in the wild so keep a short deprecation period.
* Update the MediaWiki QUnit runner to construct new maps in setup/teardown
instead of swapping the internal values object directly.
Also update the runner to update mediawiki.jqueryMsg's parser default since
it kept a reference to the first mw.messages object.
* Add 'logName' parameter to mw.log.deprecate(). Default logName
of property key made sense for its original use case (deprecation warnings
for global variables, which are a property of the window object). But
for keys of any other object, we'll want to some context for the name.
* Remove redundant test expect numbers. (Follows-up 7c36375, c4c7007d)
* Ensure mw.Map.prototype.constructor points to mw.Map.
This was broken by 5819c37b3, which accidentally made it point to Object.
Bug: T146432
Change-Id: I7ff0b230608f5ca582fc6752b59c4bb998c78ba2
* resources/src/startup.js: No more document.write() since
d7905627fd.
* tests/qunit/data/testrunner.js: No more document.write()
since 05f6edc903, and the TestSwarm agent loading
code was removed entirely in 0e9f24a169.
Change-Id: Iac61874e3ca04e1cad0d0fb2b7cebdc9fd4b237a
This hasn't been useful in QUnit for a while now with the improved
assertion context object and tracking of asynchronous tests without
shared global state.
Change-Id: Icaf865b4d6e85e739bf79c4d1bacb8a71ec5a3da
Sometimes $.active is non-zero but the ajaxRequests array doesn't
contain any pending requests (yet). In that case, log $.active
as well.
Change-Id: I1c181b1234d1f883605de38805bc029c064a1f5b
Updating all non-third-party scripts to depend on 'dom-level2-shim'
and use the constants.
* jquery.highlightText
* jquery.tablesorter
* mediawiki.api.upload
* test.mediawiki.qunit.testrunner
Change-Id: Ib07dded9fa18c749b3c064ab90326852146567b2
This was causing pending (mocked) XHR requests to sometimes be
cleaned up prematurely.
As a general rule, setup always calls its parent first. And teardown
always calls its parent last. The inner callback runs within the
scope of the outer one and should still have acces to the sinon mocks.
Change-Id: Ic4d9243c04af73e529b689e650d35ddabaa7f4b9
I added this in the JSTesting branch back in SVN (r107521, merged in
r107919 for MediaWiki 1.19.0).
It was mostly for debugging the setup/teardown in the branch. It never
had much practical use.
Change-Id: I8d6eb9e06ad070c23cb9c9041b5cf4143ee6ac0b
Similar to what we do with animations already, ensure we give
the next test a clean start by aborting any requests that were
made during the test that are still pending.
Also log the details of the request (ajax options, e.g. url) to
the console.
To test:
* Add "sleep( 1 );" to LocalSettings.php.
* add "$.ajax( mw.util.wikiScript() )" to a test in mediawiki.util.test.js.
* Run Special:JavaScriptTest/qunit/plain?module=mediawiki.util
Change-Id: Iefef89effc092d296baa9df68a86d95497730708
To use, ensure npm modules are up to date by running 'npm install'
in tests/frontend. Then run 'grunt qunit' to run it in Chrome.
To run it in both Firefox and Chrome (locally), run
grunt karma:more
Moved fixture from hardcoded HTML to the testrunner so that our
tests don't depend on arbitrary HTML and thus also pass in browser
contexts where the page only loads QUnit + test suites (e.g. Karma).
Change-Id: I4e96da137340a28789b38940e75d4b6b8bc5d76a
The config variable itself and the documentation property were added
in MediaWiki 1.19 (r107919 / c447423593).
The testswarm-injectjs propert was added in MediaWiki 1.20 (5e590be3d6).
We never actually ended up using TestSwarm, and this variable
is not used anywhere I can see.
Having a configuration variable for a documentation page seems
odd. I can't find another instance of this. As it's tied to development
(not for users of the wiki), link to mediawiki.org direcly.
Change-Id: Ib16607683a293b6d6661ed0411dad9a3ff551a08
Having unfinished animations affect other test can be a problem,
but when it does happen, having just a number isn't helpful in
trying to pinpoint the cause.
Logging a description of the element should make this relatively
straight forward.
Also reset the animations after the test so that the next test has a
clean start (instead of the current situation where every subsequent
test, until the animation finishes, also reports it as a failure).
Example:
Unfinished animation #0 in fx queue on <div class="mw-notification
mw-notification-tag-legacy" style="opacity: 0; "/>
Change-Id: I41058c4cba3383e3ad1456990ccf8dae4ca64951
The default value of 10s is ocasionally failing when loading resources
via RL. Increase to 30s as suggested in the qunit documentation.
http://api.qunitjs.com/QUnit.config/#config-testTimeout
Change-Id: I288db068c8dd1041e0a9be959b3b0c6ddcc7606e
Follows-up b90c69cdf7.
This takes the approach of throwing an error to make the exact issue
obvious.
Unlike other versions (including the code in jQuery it is loosely
based on), this does not attempt to cleanup by stopping the effects
queue.
Change-Id: Ibb6ca0d083d4e8f1556e6f3493da17f7a5f44024
It was iterating over it and considering its 'script' properties
to be methods that had to be tested.
The CompletnessTest before this change is filled with stuff like:
- loader.moduleRegistry.skins.vector.js.script
- loader.moduleRegistry.mediawiki.api.script
- loader.moduleRegistry.mediawiki.Title.script
- ..
And was actually causing it to hit hard limit and thus actual
lots of actual methods were no longer inspected by the test.
Change-Id: I13fbf1224b48aa34a7bb01fdc0543023e502bb4f
Tests should be atomic because when re-running failed tests, they
run out of order (by design).
The thing being asserted here wasn't all that useful as that is
upstream behaviour we ought to be able to rely on.
Change-Id: I2f006e1cf05f5535e8220ca8ffc2da603745d184
There are various tests triggering deprecation warnings because
they are testing deprecated functionality, on purpose.
Surpress these so that the logs aren't filled with false
positives in Jenkins.
Change-Id: I4bb546781a0c89999b2e5df7715abf492a44856d
The ie fixes file assumes running in the global scope. Since it
runs locally in our environment, it ends up destroying its
references to e.g. the Date object.
Then when calling sandbox.useFakeTimers, the Date constructor
will yield an object with no methods and tests will fail at a
random point when e.g. trying to access date.getTime().
* Remove 'clock' from the sinon properties we inject into the
test context. We use the sandbox instead (just like for server).
This also fixes a shadow clash with the tests that stored the
clock instance in `this.clock`.
Change-Id: I6085bccf5038e4751da48ee765fe81560f58c6e8
As of 89a8fe4, mw.log#warn is available in non-debug mode so that
e.g. MWDeprecationWarning are emitted whenever such property is
accessed.
Due to the way QUnit.newMwEnvironment makes a copy of the live
config for the duration of a test run (using $.extend) this
causes all the accessors to be triggered.
As a result the qunit log is inflated to 100s of MWDeprecationWarning
entries before every single test:
http://integration.wikimedia.org/ci/job/mediawiki-core-qunit/16525/console
Using Object.create (with polyfill) doesn't work since mw.Map#get
only resolves own properties.
Change-Id: I7285c56bd1ae7ef2efae15ee0427eeb77bc240ac