From: Michał Gołębiowski-Owczarek Date: Mon, 27 Apr 2020 18:22:39 +0000 (+0200) Subject: Tests: Fix flakiness in the "jQuery.ajax() - JSONP - Same Domain" test X-Git-Tag: 4.0.0-beta~190 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=7b0864d0539bbfbb01d88d9bc95369580ffd99bc;p=jquery.git Tests: Fix flakiness in the "jQuery.ajax() - JSONP - Same Domain" test The "jQuery.ajax() - JSONP - Same Domain" test is firing a request with a duplicate "callback" parameter, something like (simplified): ``` mock.php?action=jsonp&callback=jQuery_1&callback=jQuery_2 ``` There was a difference in how the PHP & Node.js implementations of the jsonp action in the mock server handled situations like that. The PHP implementation was using the latest parameter while the Node.js one was turning it into an array but the code didn't handle this situation. Because of how JavaScript stringifies arrays, while the PHP implementation injected the following code: ```js jQuery_2(payload) ``` the Node.js one was injecting the following one: ```js jQuery_1,jQuery_2(payload) ``` This is a comma expression in JavaScript; it so turned out that in the majority of cases both callbacks were identical so it was more like: ```js jQuery_1,jQuery_1(payload) ``` which evaluates to `jQuery_1(payload)` when `jQuery_1` is defined, making the test go as expected. In many cases, though, especially on Travis, the callbacks were different, triggering an `Uncaught ReferenceError` error & requiring frequent manual re-runs of Travis builds. This commit fixes the logic in the mock Node.js server, adding special handling for arrays. Closes gh-4687 --- diff --git a/test/middleware-mockserver.js b/test/middleware-mockserver.js index 314a6d162..f6196d230 100644 --- a/test/middleware-mockserver.js +++ b/test/middleware-mockserver.js @@ -93,7 +93,9 @@ var mocks = { }, jsonp: function( req, resp, next ) { var callback; - if ( req.query.callback ) { + if ( Array.isArray( req.query.callback ) ) { + callback = Promise.resolve( req.query.callback[ req.query.callback.length - 1 ] ); + } else if ( req.query.callback ) { callback = Promise.resolve( req.query.callback ); } else if ( req.method === "GET" ) { callback = Promise.resolve( req.url.match( /^.+\/([^\/?.]+)\?.+$/ )[ 1 ] );