Skip to content

Commit

Permalink
fix occasional test failures
Browse files Browse the repository at this point in the history
  • Loading branch information
k-yle committed Jan 20, 2025
1 parent a03fbde commit d150c17
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 11 deletions.
1 change: 1 addition & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export default [
'sinon': 'readonly',
'happen': 'readonly',
'fetchMock': 'readonly',
'jsdom': 'readonly',
'expect': 'writable'
}
},
Expand Down
4 changes: 3 additions & 1 deletion test/spec/behavior/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
3 changes: 2 additions & 1 deletion test/spec/services/nominatim.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
8 changes: 4 additions & 4 deletions test/spec/services/osm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",' +
Expand Down Expand Up @@ -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();
});
Expand Down Expand Up @@ -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();
});
Expand Down Expand Up @@ -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();
});
Expand Down
29 changes: 25 additions & 4 deletions test/spec_helpers.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,18 +21,37 @@ 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(
global,
'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 },
),
);

Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"target": "esnext",
"module": "nodenext",
"moduleResolution": "nodenext",
"types": ["vitest/globals"]
"types": ["vitest/globals", "vitest/jsdom"]
},
"include": ["modules", "test"]
}

0 comments on commit d150c17

Please sign in to comment.