fix occasional test failures (#10690)

This commit is contained in:
Kyℓe Hensel
2025-01-23 01:16:45 +11:00
committed by GitHub
parent 1d847e891b
commit 3011acc23c
6 changed files with 36 additions and 11 deletions

View File

@@ -73,7 +73,9 @@ describe('iD.behaviorHash', function () {
context.map().center([-77.0, 38.9]);
context.map().zoom(2.0);
window.setTimeout(function() {
expect(window.location.hash).to.equal('#background=none&map=2.00/38.9/-77.0');
// the hash might contain other things like `disable_features`
expect(window.location.hash).to.include('background=none');
expect(window.location.hash).to.include('map=2.00/38.9/-77.0');
done();
}, 600);
});

View File

@@ -10,9 +10,10 @@ describe('iD.serviceNominatim', function() {
delete iD.services.geocoder;
});
beforeEach(function() {
beforeEach(async function() {
nominatim = iD.services.geocoder;
nominatim.reset();
await iD.localizer.ensureLoaded();
});
afterEach(function() {

View File

@@ -137,7 +137,7 @@ describe('iD.serviceOsm', function () {
});
describe('#loadFromAPI', function () {
var path = '/api/0.6/map.json?bbox=-74.542,40.655,-74.541,40.656';
var path = '/api/0.6/map.json';
var response =
'{' +
' "version":"0.6",' +
@@ -528,7 +528,7 @@ describe('iD.serviceOsm', function () {
done();
});
serverXHR.respondWith('GET', 'https://www.openstreetmap.org/api/0.6/changesets?user=1',
serverXHR.respondWith('GET', 'https://www.openstreetmap.org/api/0.6/changesets\\?user=1',
[200, { 'Content-Type': 'text/xml' }, changesetsXML]);
serverXHR.respond();
});
@@ -557,7 +557,7 @@ describe('iD.serviceOsm', function () {
done();
});
serverXHR.respondWith('GET', 'https://www.openstreetmap.org/api/0.6/changesets?user=1',
serverXHR.respondWith('GET', 'https://www.openstreetmap.org/api/0.6/changesets\\?user=1',
[200, { 'Content-Type': 'text/xml' }, changesetsXML]);
serverXHR.respond();
});
@@ -587,7 +587,7 @@ describe('iD.serviceOsm', function () {
done();
});
serverXHR.respondWith('GET', 'https://www.openstreetmap.org/api/0.6/changesets?user=1',
serverXHR.respondWith('GET', 'https://www.openstreetmap.org/api/0.6/changesets\\?user=1',
[200, { 'Content-Type': 'text/xml' }, changesetsXML]);
serverXHR.respond();
});

View File

@@ -8,6 +8,8 @@ import envs from '../config/envs.mjs';
chai.use(sinonChai);
declare var global: typeof globalThis;
global.before = beforeEach;
global.after = afterEach;
global.fetchMock = fetchMock;
@@ -19,6 +21,19 @@ for (const [key, value] of Object.entries(envs)) {
Reflect.set(global, key, JSON.parse(value));
}
// the 'happen' library explicitly references `window` when creating an event,
// but we need to use jsdom's window, so we have to patch initEvent.
const { initMouseEvent } = MouseEvent.prototype;
MouseEvent.prototype.initMouseEvent = function (...args) {
args[3] = jsdom.window;
return initMouseEvent.apply(this, args);
};
const { initUIEvent } = UIEvent.prototype;
UIEvent.prototype.initUIEvent = function (...args) {
args[3] = jsdom.window;
return initUIEvent.apply(this, args);
};
// vitest has deprecated the done() callback, so we overwrite the `it` function
const _it = it;
Reflect.set(
@@ -26,11 +41,17 @@ Reflect.set(
'it',
Object.assign(
(msg: string, fn: (done?: () => void) => void | Promise<void>) => {
_it(msg, () =>
fn.length ? () => new Promise<void>((resolve) => fn(resolve)) : fn(),
);
_it(msg, () => {
if (fn.length) {
// there is a done callback -> return a promise instead
return new Promise<void>((done) => fn(done));
}
// no done callback -> normal behaviour
return fn();
});
},
{ todo: _it.todo, skip: _it.skip },
{ todo: _it.todo, skip: _it.skip, only: _it.only, each: _it.each },
),
);