Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent error when calling upload() early #938

Merged

Conversation

mmustafa-tse
Copy link
Contributor

Summary

  • github issue with description here

Testing Plan

  • [Y] Was this tested locally? If not, explain why.
  • Tested with local overrides

Reference Issue (For mParticle employees only. Ignore if you are an outside contributor)

@mmustafa-tse mmustafa-tse changed the title fix: prevent crash when calling upload() early fix: prevent error when calling upload() early Oct 22, 2024
@@ -590,7 +590,7 @@ export default function mParticleInstance(instanceName) {
Constants.NativeSdkPaths.Upload
);
} else {
self._APIClient.uploader.prepareAndUpload(false, false);
self._APIClient.uploader?.prepareAndUpload(false, false);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you write a test that catches this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just added the test case, thnx!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there exists a case where _APIClient won't yet be available, when there is a /config call in a self hosted world, so let's add a ? to _APIClient also:

Suggested change
self._APIClient.uploader?.prepareAndUpload(false, false);
self._APIClient?.uploader?.prepareAndUpload(false, false);

expect(uploader).to.equal(null)

// calling upload manually on an a null uploader should not result in an error
window.mParticle.upload();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should add an assertion to verify that an error actually doesn't throw.

IIRC the format should be something like:
expect(() => { window.mParticle.upload()}).not.to.throw(new Error('<ERROR MESSAGE>'))
or
expect(() => { window.mParticle.upload()}).not.to.throw('<ERROR MESSAGE>')

I think the gist is to make sure we're actually catching the right error message or to make sure throwing an error doesn't suspend other testing.

I would timebox this unless @rmi22186 feels strongly about it as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea - i actually was thinking the same thing, assuming that if an error threw, perhaps the test would still pass since there is no expect. however, i did pull the code down yesterday to check and it throws an error and the test fails, so at least it's not a silent error.

here's a recommendation i found online as an example of how this would work, so try this, and if it doesn't work, I think we are good to go with what you currently have, and just add a comment above it that says something like

const { expect } = require('chai');
const sinon = require('sinon');

describe('MyFunction', () => {
    it('should not throw an error', () => {
        const myFunction = sinon.stub().returns(true); // Stub the function
        
        expect(() => myFunction()).to.not.throw();
    });
});

and if that doesn't work, then put a comment explaining why there is not expect

 // the following does not throw an error, but if it did, the test would fail because the error is not silent
or something like that. 

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was able to get it working and not throw the error with a regex matching a part of the message while specifying the error type to be TypeError, tested on both before and after the fix and it worked as expected

Copy link

sonarcloud bot commented Oct 31, 2024

Quality Gate Failed Quality Gate failed

Failed conditions
B Maintainability Rating on New Code (required ≥ A)

See analysis details on SonarCloud

Catch issues before they fail your Quality Gate with our IDE extension SonarLint

Copy link
Member

@rmi22186 rmi22186 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pulled the code down to run the new test undoing the source code change, and it works great

@rmi22186 rmi22186 merged commit 7fa9897 into mParticle:development Nov 5, 2024
24 of 26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants