Commit graph

75 commits

Author SHA1 Message Date
Ed Sanders
3c2cea4559 build: Update linters and fix errors
Change-Id: Ieb1eff174f5168bf6c9e9aaeb58ec1a1bcd33538
2019-10-23 11:22:41 +01:00
Ed Sanders
a3e3a80329 Always $-prefix jQuery variable names
Change-Id: I82d7788fbd01b73fc8a3255d5af2de75c85279e1
2019-10-08 13:12:17 +01:00
Ed Sanders
9097f95ecc build: Update eslint-config-wikimedia to 0.11.0
Change-Id: Iee025a518962e68c5ec2c07d952f402cd2a7f69b
2019-02-20 23:36:03 +00:00
Ed Sanders
b3e490bba6 build: Update eslint-config-wikimedia to 0.10.0
Change-Id: I2930bcabeeb7b7b2eb36063e77b26e664a691b43
2019-01-08 17:40:11 +00:00
Ed Sanders
3e4e0a5774 build: Use eslint-config-wikimedia v0.9.0 and make pass
Change-Id: I7b5d228a3de4b3006751a427dec907a5bebf2f51
2018-11-23 14:20:24 +00:00
Timo Tijhof
add9bd191f resources: Strip '$' and 'mw' from file closures
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
2018-09-14 00:59:27 +01:00
jdlrobson
1ded08465b Hygiene: Discourage use of $.each
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
2018-09-05 17:21:29 -07:00
Ed Sanders
4e6a6b4871 build: Enable qunit/no-assert-equal and enforce
This is the equivalent of the '===' rule ("eqeqeq") in eslint core.

Change-Id: I1e6ea8bc1e86c6629ebe938f1b4a6bd288bcfcf7
2018-06-06 11:49:12 +01:00
Ed Sanders
5e18d925e7 build: Introduce eslint-plugin-qunit and enforce
Change-Id: Iefe06cc865c458000ca5350c2d9205f5b768bac8
2018-06-05 21:58:25 +01:00
Aleksey Bekh-Ivanov (WMDE)
27fb62af34 qunit: Test before and after hooks in QUnit testrunner
Change-Id: Ib5b5d29e860d37dbec675e5d24c31b57c5e64102
2018-02-01 09:53:20 +00:00
Timo Tijhof
c3217d587d qunit: Refactor and simplify testrunner to fix nested modules
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
2018-01-31 13:27:48 -08:00
Fomafix
1edba8029a Use native ES5 Array prototype methods instead of jQuery
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
2018-01-11 21:06:14 +01:00
Timo Tijhof
2454f51b27 Upgrade QUnit from v1.23.1 to v2.4.0
Source
 https://code.jquery.com/qunit/qunit-2.4.0.js
 https://code.jquery.com/qunit/qunit-2.4.0.css

Changelog
 https://github.com/qunitjs/qunit/blob/2.4.0/History.md

This release makes 'moduleStack' private, which means we can no
longer use it to detect nested modules. Change the method of
detection to be a local 'nested' variable that we toggle in
a wrapped version of any 'executeNow' callback, in which nesting
happens.

Bug: T170515
Change-Id: Ib4ed386990125a6c713cb4351408c8f7ae7c070f
2017-07-25 22:14:09 +00:00
Timo Tijhof
43dc5c1539 qunit: Prepare testrunner for QUnit 2
* 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
2017-07-20 20:45:18 +00:00
Timo Tijhof
5a49381406 qunit: Remove redundant conditional for sandbox teardown
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
2017-07-17 20:25:11 +00:00
Umherirrender
b5cddfb27b Remove empty lines at begin of function, if, foreach, switch
Organize phpcs.xml a bit

Change-Id: Ifb767729b481b4b686e6d6444cf48b1f580cc478
2017-07-01 11:34:16 +00:00
Aleksey Bekh-Ivanov (WMDE)
0a208911a2 Ability to create tests with nested modules
Change-Id: Ie0bf2dcfb63e7dc53cd5afe411e2fbb8d1bbfd73
2017-06-02 11:28:48 +00:00
Timo Tijhof
1073b75139 qunit: Also clear $.timers when stopping unfinished animations
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
2017-04-18 16:52:41 -07:00
Timo Tijhof
d8af25c706 qunit: Improve testrunner logging for pending ajax
* 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
2017-04-14 17:23:16 -07:00
Timo Tijhof
2bbce5264b qunit: Remove experimental CompletenessTest feature
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
2017-03-24 17:33:40 -07:00
Timo Tijhof
96f40f152a qunit: Make eslint config pass on qunit test files
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
2017-02-22 11:15:40 -08:00
Timo Tijhof
5e602c6132 qunit: Remove redundant testCount expectation
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
2017-02-22 01:25:52 +00:00
Timo Tijhof
bc78528179 qunit: Update tests to not assume animations use setTimeout
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
2016-11-21 19:03:33 -08:00
Timo Tijhof
dc0f9b3a3a resourceloader: Deprecate mw.Map
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
2016-10-25 14:14:58 +00:00
Kevin Israel
b0fd084abc Remove "jshint evil:true" where not required
* 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
2016-06-14 23:49:44 -04:00
Timo Tijhof
7c363752ed qunit: Don't require expect() anymore
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
2016-03-04 23:50:04 +00:00
Jan Zerebecki
8f4d5c5d54 qunit: Increase individual test timeout to 60 seconds
Bug: T125484
Change-Id: If4147d678fdef168d79bc2b4d0c606b8c82dc6af
2016-02-04 18:59:36 +01:00
James D. Forrester
e8608b94a5 build: Enable jscs jsDoc rule 'checkTypes' and make pass
Change-Id: I64c3b5bd7afc4686355c80ef68009d978f7b3097
2015-10-01 15:28:06 -07:00
James D. Forrester
51646ddaea build: Enable jscs jsDoc rule 'requireNewlineAfterDescription' and make pass
Change-Id: I14d699f66cef87f0d93d773902c0a5326025a58c
2015-09-07 18:32:17 +00:00
James D. Forrester
b6cdd33376 build: Enable jscs jsDoc rule 'requireParamTypes' and make pass
Change-Id: Id0e10dba3548a981ba6d301bd3cd04adbe143e64
2015-09-07 18:32:10 +00:00
James D. Forrester
aceff2e778 build: Enable jscs rules 'requireSpacesInside*Brackets' and make pass
Change-Id: I1cd5b9b92b6e9419d99d9a15ad88a996253ca4cf
2015-09-05 13:33:47 +00:00
James D. Forrester
42fe11a2e6 build: Enable jscs rule 'requireSpacesInsideParentheses' and make pass
Change-Id: Ie88c89344e54657f007e5101472e667be5408319
2015-09-03 21:35:22 +00:00
Timo Tijhof
ba681a6fec qunit: Improved unfinished ajax detection
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
2015-08-05 10:54:25 -07:00
Bartosz Dziewoński
adce8103fa Use Node.ELEMENT_NODE and Node.TEXT_NODE constants instead of magic numbers
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
2015-07-16 17:32:45 +00:00
Timo Tijhof
3e59344c69 qunit: Restore sinon.sandbox after teardown instead of before
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
2015-07-07 15:50:13 +01:00
Timo Tijhof
43f89ebdb4 qunit: Remove mwlogenv feature
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
2015-04-25 01:19:36 +00:00
Timo Tijhof
52dd931bdb mediawiki.log: Introduce mw.log.error
In addition to mw.log.warn, but for the console.error channel.

Change-Id: I7b09a75da6c985a9e0cc73591f49b21972efd393
2015-04-23 04:20:57 +00:00
Timo Tijhof
e7d4b84f7b qunit: Abort unfinished AJAX requests in test teardown
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
2015-04-16 01:19:54 +01:00
Adrian Heine
edf91cda43 Update QUnit from v1.14.0 to v1.16.0
- Release notes
  https://github.com/jquery/qunit/blob/1.16.0/History.md

- Source of imported files
  https://code.jquery.com/qunit/qunit-1.16.0.js
  https://code.jquery.com/qunit/qunit-1.16.0.css

- Unit tests still pass.
  Tested on [[Special:JavaScriptTest/qunit]] with $wgEnableJavaScriptTest = true

Bug: T85994
Change-Id: Ic2d5e91d4f1e16a87618b18c3a83d31aaa0ab91c
2015-01-07 15:23:10 +00:00
Timo Tijhof
945c1efe37 grunt: Add Karma task for automated QUnit testing in browsers
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
2015-01-05 18:00:13 +00:00
Timo Tijhof
0e9f24a169 Remove $wgJavaScriptTestConfig
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
2014-12-17 00:29:27 +00:00
Timo Tijhof
0615b0f2b9 qunit/testrunner: Log relevant node in unfinished animations and reset
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
2014-11-26 06:27:11 +00:00
Erik Bernhardson
0fce9fd1e5 Increase QUnit.config.testTimeout to 30s
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
2014-11-15 16:38:39 +00:00
Matthew Flaschen
bc5a43aa94 QUnit testrunner: Fix comment regarding animation and AJAX check
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
2014-07-11 22:51:11 +00:00
Timo Tijhof
b90c69cdf7 QUnit: Detect unfinished jQuery effects or ajax requests in teardown
Based on jQuery test suite
https://github.com/jquery/jquery/blob/bcca4f041/test/data/testrunner.js#L113-L125

Change-Id: I1dba146a6795887c23c806f292c6425662ea4aad
2014-07-11 01:32:59 +00:00
Timo Tijhof
0c71217d09 qunit.completenessTest: Ignore mw.loader.moduleRegistry
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
2014-06-29 20:59:43 +00:00
Timo Tijhof
bb71fd2ada qunit: Remove race condition in 'test.mediawiki.qunit.testrunner'
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
2014-04-11 11:40:52 -07:00
Timo Tijhof
4c66b7fb91 qunit: Implement suppressWarnings/restoreWarnings
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
2014-04-08 01:02:47 +00:00
Timo Tijhof
6656612660 qunit: Disable sinonjs-ie as it breaks the clock sandbox
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
2014-03-11 22:35:18 +00:00
Timo Tijhof
9a3e82f919 QUnit.newMwEnvironment: Disable mw.log#warn while copying mw.config
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
2014-02-20 08:38:42 +01:00