Skip to content

Commit

Permalink
Remove process/browser polyfill in tests, replace all process.nextTic…
Browse files Browse the repository at this point in the history
…k by setTimeout (#5643)
  • Loading branch information
vincentfretin authored Jan 25, 2025
1 parent 253d772 commit 84d4607
Show file tree
Hide file tree
Showing 20 changed files with 68 additions and 73 deletions.
4 changes: 1 addition & 3 deletions src/utils/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,8 @@ export function isLandscape () {
* Check if running in a browser or spoofed browser (bundler).
* We need to check a node api that isn't mocked on either side.
* `require` and `module.exports` are mocked in browser by bundlers.
* `window` is mocked in node.
* `process` is also mocked by webpack running with karma, but has custom properties like process.browser.
*/
export var isBrowserEnvironment = typeof process === 'undefined' || process.browser === true;
export var isBrowserEnvironment = typeof process === 'undefined';

/**
* Check if running in node on the server.
Expand Down
4 changes: 2 additions & 2 deletions tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ suite('module/component/custom element', function () {
* An asynchronous test case.
*/
test('does that', function (done) {
process.nextTick(function () {
setTimeout(function () {
assert.notEqual(1, 2);
done(); // Use `done` to tell when finished in asynchronous test.
});
Expand All @@ -113,5 +113,5 @@ suite('module/component/custom element', function () {
'loaded' event.
- karma may sometimes misreport test failures to other unit tests. Run one test
suite or case at a time to isolate the test failure.
- Use `process.nextTick` after doing DOM manipulations. However, we will not
- Use `setTimeout` after doing DOM manipulations. However, we will not
need to do this after `Entity.setAttribute` as we have made that synchronous.
12 changes: 6 additions & 6 deletions tests/components/cursor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ suite('cursor', function () {
assert.ok(el.is('cursor-hovering'));
assert.ok(intersectedEl.is('cursor-hovered'));
el.removeAttribute('cursor');
process.nextTick(function () {
setTimeout(function () {
assert.notOk(el.is('cursor-hovering'));
assert.notOk(intersectedEl.is('cursor-hovered'));
done();
Expand All @@ -59,15 +59,15 @@ suite('cursor', function () {
});
assert.ok(el.is('cursor-fusing'));
el.removeAttribute('cursor');
process.nextTick(function () {
setTimeout(function () {
assert.notOk(el.is('cursor-fusing'));
done();
});
});

test('removes intersection listener', function (done) {
el.removeAttribute('cursor');
process.nextTick(function () {
setTimeout(function () {
el.emit('raycaster-intersection', {
intersections: [intersection],
els: [intersectedEl]
Expand Down Expand Up @@ -262,7 +262,7 @@ suite('cursor', function () {
els: [furtherIntersectedEl]
});

process.nextTick(function () {
setTimeout(function () {
assert.equal(el.components.cursor.intersectedEl, nearerIntersectedEl);
done();
});
Expand Down Expand Up @@ -405,7 +405,7 @@ suite('cursor', function () {
event.clientY = 5;
el.setAttribute('cursor', 'rayOrigin', 'mouse');
el.sceneEl.canvas.dispatchEvent(event);
process.nextTick(function () {
setTimeout(function () {
var raycaster = el.getAttribute('raycaster');
assert.notEqual(raycaster.direction.x, 0);
done();
Expand All @@ -417,7 +417,7 @@ suite('cursor', function () {
event.touches = {item: function () { return {clientX: 5, clientY: 5}; }};
el.setAttribute('cursor', 'rayOrigin', 'mouse');
el.sceneEl.canvas.dispatchEvent(event);
process.nextTick(function () {
setTimeout(function () {
var raycaster = el.getAttribute('raycaster');
assert.notEqual(raycaster.direction.x, 0);
done();
Expand Down
2 changes: 1 addition & 1 deletion tests/components/gltf-model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ suite('gltf-model', function () {

this.sinon.replace(THREE, 'GLTFLoader', function MockGLTFLoader () {
this.load = function (url, onLoad) {
process.nextTick(onLoad.bind(null, gltfMock));
setTimeout(onLoad.bind(null, gltfMock));
};
this.setDRACOLoader = function () {};
});
Expand Down
10 changes: 5 additions & 5 deletions tests/components/look-controls.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ suite('look-controls', function () {
var lookControls = el.camera.el.components['look-controls'];
lookControls.hasPositionalTracking = false;
lookControls.previousHMDPosition.set(1, 2, 3);
process.nextTick(function () {
setTimeout(function () {
assert.ok(lookControls.previousHMDPosition.length() === 0);
done();
});
Expand All @@ -33,7 +33,7 @@ suite('look-controls', function () {

test('adds grabbing style to scene canvas on mousedown', function (done) {
var canvasEl = this.sceneEl.canvas;
process.nextTick(function () {
setTimeout(function () {
assert.ok(canvasEl.style.cursor === 'grabbing');
canvasEl.style.cursor = '';
done();
Expand All @@ -46,7 +46,7 @@ suite('look-controls', function () {
test('removes grabbing style from scene el canvas on document body mouseup', function (done) {
var canvasEl = this.sceneEl.canvas;
canvasEl.style.cursor = 'grabbing';
process.nextTick(function () {
setTimeout(function () {
assert.notOk(canvasEl.style.cursor === 'grabbing');
done();
});
Expand All @@ -60,7 +60,7 @@ suite('look-controls', function () {
var requestPointerLock = this.sinon.spy(canvasEl, 'requestPointerLock');
cameraEl.setAttribute('look-controls', {pointerLockEnabled: true});

process.nextTick(function () {
setTimeout(function () {
assert.ok(requestPointerLock.called);
canvasEl.style.cursor = '';
done();
Expand All @@ -80,7 +80,7 @@ suite('look-controls', function () {

cameraEl.setAttribute('look-controls', {pointerLockEnabled: false});

process.nextTick(function () {
setTimeout(function () {
assert.notOk(requestPointerLock.called);
canvasEl.style.cursor = '';
done();
Expand Down
2 changes: 1 addition & 1 deletion tests/components/scene/device-motion-permission-ui.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ suite('device-orientation-permission-ui', function () {

test('appends permission dialog', function (done) {
var scene = this.el;
process.nextTick(function () {
setTimeout(function () {
PERMISSION_DIALOG_CLASSES.forEach(function (uiClass) {
assert.equal(scene.querySelectorAll(uiClass).length, 1);
done();
Expand Down
2 changes: 1 addition & 1 deletion tests/components/scene/xr-mode-ui.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ suite('xr-mode-ui', function () {

scene.enterVR();

process.nextTick(function () {
setTimeout(function () {
UI_CLASSES.forEach(function (uiClass) {
assert.include(scene.querySelector(uiClass).className, 'a-hidden');
});
Expand Down
8 changes: 4 additions & 4 deletions tests/components/sound.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ suite('sound', function () {
var sceneEl = this.el.sceneEl;
var assetsEl = sceneEl.querySelector('a-assets');
sceneEl.removeChild(assetsEl);
process.nextTick(function () {
setTimeout(function () {
assetsEl = document.createElement('a-assets');
var audioEl = document.createElement('audio');
audioEl.setAttribute('src', 'base/tests/assets/test.ogg');
Expand All @@ -260,7 +260,7 @@ suite('sound', function () {
var sceneEl = this.el.sceneEl;
var assetsEl = sceneEl.querySelector('a-assets');
sceneEl.removeChild(assetsEl);
process.nextTick(function () {
setTimeout(function () {
assetsEl = document.createElement('a-assets');
var audioEl = document.createElement('audio');
audioEl.setAttribute('src', 'base/tests/assets/test.ogg');
Expand All @@ -284,15 +284,15 @@ suite('sound', function () {
var sceneEl = this.el.sceneEl;
var assetsEl = sceneEl.querySelector('a-assets');
sceneEl.removeChild(assetsEl);
process.nextTick(function () {
setTimeout(function () {
assetsEl = document.createElement('a-assets');
var assetItemEl = document.createElement('a-asset-item');
assetItemEl.setAttribute('src', 'base/tests/assets/test.ogg');
assetItemEl.setAttribute('id', 'testogg');
assetItemEl.setAttribute('response-type', 'arraybuffer');
assetsEl.appendChild(assetItemEl);
sceneEl.appendChild(assetsEl);
process.nextTick(function () {
setTimeout(function () {
var el = document.createElement('a-entity');
el.setAttribute('sound', 'src', '#testogg');
el.addEventListener('sound-loaded', function () {
Expand Down
8 changes: 4 additions & 4 deletions tests/core/a-entity.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ suite('a-entity', function () {
assert.notEqual(el.sceneEl.behaviors.test.tick.array.indexOf(el.components.test), -1);
assert.notEqual(el.sceneEl.behaviors.test.tock.array.indexOf(el.components.test), -1);
parentEl.removeChild(el);
process.nextTick(function () {
setTimeout(function () {
assert.ok('test' in el.components);
assert.equal(el.sceneEl.behaviors.test.tick.array.indexOf(el.components.test), -1);
assert.equal(el.sceneEl.behaviors.test.tock.array.indexOf(el.components.test), -1);
Expand Down Expand Up @@ -668,7 +668,7 @@ suite('a-entity', function () {
var bLoaded = false;
el.appendChild(a);
el.appendChild(b);
process.nextTick(function () {
setTimeout(function () {
a.isNode = false;
a.hasLoaded = false;
b.isNode = false;
Expand Down Expand Up @@ -696,7 +696,7 @@ suite('a-entity', function () {
var bLoaded = false;
el.appendChild(a);
el.appendChild(b);
process.nextTick(function () {
setTimeout(function () {
a.isNode = false;
a.hasLoaded = false;
b.isNode = false;
Expand Down Expand Up @@ -1320,7 +1320,7 @@ suite('a-entity', function () {

test('merges component properties from mixin', function (done) {
mixinFactory('box', {geometry: 'primitive: box'});
process.nextTick(function () {
setTimeout(function () {
el.setAttribute('mixin', 'box');
el.setAttribute('geometry', {depth: 5, height: 5, width: 5});
assert.shallowDeepEqual(el.getAttribute('geometry'), {
Expand Down
2 changes: 1 addition & 1 deletion tests/core/a-mixin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ suite('a-mixin', function () {
mixinEl.addEventListener('loaded', function () {
assert.equal(el.getAttribute('geometry').primitive, 'ring');
mixinEl.setAttribute('geometry', 'primitive: circle');
process.nextTick(function () {
setTimeout(function () {
assert.equal(el.getAttribute('geometry').primitive, 'circle');
done();
});
Expand Down
Loading

0 comments on commit 84d4607

Please sign in to comment.