Skip to content

Conversation

@jasonkarns
Copy link
Contributor

Summary

Fixes cross-test pollution of global XHR object.

The global XHR object was being stubbed in some tests but not others. But the tests that didn't stub weren't failing because the tests that were stubbing weren't being cleaned up! So some test suites were depending (implicitly) on side effects from other test files.

Issues

partially fixes #488

XMLHttpRequest is stubbed by sinon but never assigned to the global!
Thus, the stubbing is never used anywhere in this test suite _except_
that it relies on the stubbing done in _other_ test files that aren't
cleaned up.

This ensures the stubbing is set and used within this test file. (But
does not clean up the bad global stubbings that are left intact after
each test run.)

This partially fixes optimizely#488
This stubbing is created and replaces the global value, but was not
being cleaned up after each run! Thus, the stubbing side effect was
leaking to other tests. :(

This ensures the stubbing is removed after each test so subsequent tests
are run cleanly.
Any variable that is declared outside of a test will leak state between
tests. This is bad and dangerous. Variables that are necessary to
reference between both setup and test callbacks should be defined on the
test context, which mocha creates fresh for each test. This way,
cross-test pollution is minimized. (This is part of the problem that
caused: optimizely#488)
There is no need to "restore" a stubbing via sinon when the entire thing
will be deleted.
Also, many of these tests aren't asserting on the requests array so
there is no use for it.
@jasonkarns jasonkarns requested a review from a team as a code owner May 21, 2020 14:57
@jasonkarns jasonkarns changed the title Fix tests Fix test stubbings of global XHR object May 21, 2020
@mjc1283 mjc1283 merged commit 95cd8a0 into optimizely:master May 28, 2020
@jasonkarns jasonkarns deleted the fix-tests branch May 29, 2020 00:58
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.

optimizely-sdk test suite has cross-test-file pollution

2 participants