Add a test that fails due to valueOf side effects, with proposed patch

See the commit message for the test case for a detailed explanation of what went wrong.

If the test seems convoluted, I can assure you that I actually encountered such a situation yesterday, in the wilds of my actual codebase. In particular, if you cache promise objects for later reuse, it's pretty easy to end up referring a cached promise from another promise that was derived from the cached promise. Since promises are supposed to be these idempotent, immutable things, I believe this pattern should either work or fail with a better error message.

If you have a workaround or a better solution, I'm all ears.