-
Notifications
You must be signed in to change notification settings - Fork 44
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
fix: prevent error when calling upload() early #938
Conversation
src/mp-instance.js
Outdated
@@ -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); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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:
self._APIClient.uploader?.prepareAndUpload(false, false); | |
self._APIClient?.uploader?.prepareAndUpload(false, false); |
ec33d3f
to
6529614
Compare
test/src/tests-batchUploader.ts
Outdated
expect(uploader).to.equal(null) | ||
|
||
// calling upload manually on an a null uploader should not result in an error | ||
window.mParticle.upload(); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
Quality Gate failedFailed conditions See analysis details on SonarCloud Catch issues before they fail your Quality Gate with our IDE extension SonarLint |
There was a problem hiding this 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
Summary
Testing Plan
Reference Issue (For mParticle employees only. Ignore if you are an outside contributor)