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

Tests for code in src/util #124

Merged
merged 27 commits into from
Oct 1, 2017
Merged

Tests for code in src/util #124

merged 27 commits into from
Oct 1, 2017

Conversation

Treora
Copy link
Collaborator

@Treora Treora commented Sep 11, 2017

Direct continuation of #116. @reficul31 I did not want to push my changes to your personal repo, if that's even possible, hence a new PR. Feel free (but not obliged) to review my changes, you probably know the art of testing better than I do after all your work.

Work in progress. So far I reviewed and revised:

  • delay
  • event-to-promise
    (I found some arguably hacky way to test delay and event-to-promise more extensively, by using bogus await statements to check if any promises resolved.)
  • make-range-transform
  • nice-time (no changes)
  • pouchdb-update
  • random-string (no changes)
  • short-url (no changes)
  • sync-location-hashes
  • tab-events
  • tree-walker
  • webextension-rpc

Copy link
Contributor

@reficul31 reficul31 left a comment

Choose a reason for hiding this comment

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

@Treora the tests look more complete now! Thanks for taking the time for tweaking the changes.
We should probably close the redundant PR.
Also I am not too sure about the await null implementation. Could you maybe expand a bit on the usage of that particular code?

expect(rejectOpts.event.listeners.length).toBe(0)
})

describe('should resolve with given value when a resolve-event occurs', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Just a design suggestion. It would be more consistent to use it instead of using describe. Since in the original testing plan describe is used to denote collections of test belonging to modules.
So we could have a structure like
->describe
--->test
----->it
Or we could interchange it and test. Whichever is more suitable. I notice now that it is not present in the official documentation, but the implementation works for sure.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Your design sounds prettier indeed. I just followed the jest documentation, which mentions nesting describe blocks but not nesting tests. I now see that it is documented too, it is shortly mentioned as being an alias for test, but that means it might not be nestable. Even if nesting tests would technically seem to work, I'd rather follow the documented approaches to avoid possible quirks later on (jest might change its implementation).

@Treora
Copy link
Collaborator Author

Treora commented Sep 12, 2017

Also I am not too sure about the await null implementation. Could you maybe expand a bit on the usage of that particular code?

I am not too sure about it either. The problem is that I would like to assert that a promise does not yet settle before the required timer/event triggers. Every approach based on awaiting a promise (e.g. jest's expect().resolves) is thus unusable here.

Some workarounds are discussed in this QA, e.g. using Node's util.inspect to read whether a promise is in 'pending' state, or using a Promise.race(p, Promise.resolve()) (quite clever, actually).

My reasoning was that it should also be doable by just adding an await statement, which will give control to the main event loop, which should then execute all pending promise handlers before returning to our test function. So after the await, if the callbacks had not been called, the promise must still be pending.

Though this seems to work in these tests, I am not sure if we have a guarantee that the callback would be run before our await statement continues (which itself is just syntactic sugar for setting up a callback). Are promise callbacks (including those from await) always executed in the same order that they were queued? I don't know. I am tempted to keep it for now and try to learn more about this (or find a better approach) sooner or later.

Another small point: even if this approach is correct, there is still the question whether it is reliable in other javascript engines; at least Firefox still has trouble with ordering promises/microtasks. Node/V8 is said to implement the specs correctly though, and is currently the only target for our tests.

@reficul31
Copy link
Contributor

Yeah, I totally agree with everything @Treora stated. I will have a more thorough look into it. Re reading the code I now full understand what we were trying to accomplish as well as why the need for await null was required. I think we should keep it as is now, and change later once a better implementation comes to light.

The two previous tests seemed to test the same thing, so removing one.
Importing pouchdb seems unnecessary, just mocking put and get suffices.
Adding tests for the case of update conflicts.
Not too happy introducing complexity in the mock.. I added a test for
the mock itself.
Use a simple graph to test if it gives the correct results.
@Treora
Copy link
Collaborator Author

Treora commented Oct 1, 2017

I think it's good enough. Time to merge and move on!

@Treora Treora merged commit 3e9330e into master Oct 1, 2017
@Treora Treora deleted the test-util branch October 1, 2017 22:34
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.

2 participants