-
Notifications
You must be signed in to change notification settings - Fork 80
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
Problem with combine() and Mocks #228
Comments
Hey @HyperCharlie, sorry for the delay. I've just started a new job so I haven't had much time to groom issues here. I'll have a look this week. |
Not a problem. Fortunately I have a workaround that's acceptable; I just wanted to let you know and hopefully save others the same headaches! |
Sorry to be a bother, but I was wondering if you might be able to link me to a codesandbox or some other service / repo with a working repro. That would help a lot! |
Sure, I can set up something like that in the morning. |
Hey @HyperCharlie, any update on creating a small repro? Otherwise I will close this issue! |
Hey Gio, I have not forgotten this, just been super busy. One of our
engineers announced he was leaving and I've had to scramble to get projects
he was on past the parts he was critical for.
…On Wed, Feb 17, 2021 at 3:46 PM Gio ***@***.***> wrote:
Hey @HyperCharlie <https://github.com/HyperCharlie>, any update on
creating a small repro? Otherwise I will close this issue!
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#228 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ANC7E2UJ2AZGBT7YLXW5HELS7RIMHANCNFSM4WUNIXGA>
.
|
Thanks for the update! I'll leave it open. |
bump |
I spent some time this morning trying to figure out why this issue is occurring. @HyperCharlie provided a potential solution, but I first wanted to understand the cause of the problem. I played around with the emitted output of neverthrow's v4.2.2 source code using this repro repository: https://github.com/supermacro/neverthrow-combine-issue-repro With the above repo I then started playing with the neverthrow code inside of node_modules. // inside of node_modules/neverthrow/dist/index.cjs.js
var combineResultList = function (resultList) {
console.log(resultList[0].value) // <---- value is _NOT_ undefined here
var resultList2 = resultList.map((result) => {
console.log('> ' + result.value) // <----- value is undefined here
return result
})
return resultList2.reduce(function (acc, result) {
console.log('Val: ' + result.value) // <----- value is undefined here
return acc.isOk()
? result.isErr()
? err(result.error)
: acc.map(appendValueToEndOfList(result.value))
: acc;
}, ok([]));
}; Notice a few things:
As it stands, I don't yet know the cause of the issue. Therefore I do not know what a good solution is at this moment. Any thoughts @delitescere? |
const neverthrow = require('neverthrow');
var results = [neverthrow.ok('a'), neverthrow.ok('b')];
console.log(JSON.stringify(results));
var combined = neverthrow.combine(results);
console.log(JSON.stringify(combined));
assert(results[0].value === combined.value[0]); The functors are |
I've gone ahead and made an interactive sample of the above code on repl.it: https://replit.com/@gDelgado14/WhichShortServerapplication#index.js That said, can you elaborate on the point you are trying to make @delitescere? Not sure how this relates to the fact that iterating methods such as |
$75 USD bounty for anyone that can solve this |
Bounty increased to $150 USD |
TLDRThis will be a bit lengthy, the first snippet is about the undefined logging in the maps, but the final two snippets are the ones where changes were made that matter for the original issue. The rest was just exploratory stuff incase it helps prompt more thoughts if I'm missing something important I just wanted to play around and I seem to have gotten a case that works for what was shown above but I hope someone might be able to help verify this. Undefined logging of snippets provided aboveSo repo you posted above (not the repl.it) I think the issue with the lines you've mentioned logging // inside of node_modules/neverthrow/dist/index.cjs.js
var combineResultList = function (resultList) {
console.log(resultList[0].value) // <---- value is _NOT_ undefined here
var resultList2 = resultList.map((result) => {
// Note using the comma, not the +
console.log('> ', result.value) // <----- value is undefined here no longer!
return result
})
// ... other stuff
}; Theres no undefined logs coming out, infact logging out the 'mock' variable will show just an output of Something weird I did notice is that the mock value seems to change after the combine? describe("Debugging and basic info tests", () => {
it("combine works with TestDouble mocks of interfaces", async () => {
// Arrange
const mock = td.object<ITestInterface>()
console.log('check mock 1', mock);
// Act
const result = await combine([
okAsync(mock),
okAsync({ name: 'giorgio' }),
okAsync(123),
] as const)
console.log('check mock 2: ', mock); // second mock now is different
// Assert
expect(result).toBeDefined()
expect(result.isErr()).toBeFalsy()
const unwrappedResult = result._unsafeUnwrap()
expect(unwrappedResult.length).toBe(2)
expect(unwrappedResult[0]).toBe(mock)
})
}) the "check mock 2" log now has a value of:
Possible Solution(it gets the tests passing, but I don't have full explanation as to why and am looking more) As the author originally pointed out that its related to the concat method, im assuming its related back to the proxy implementation of this, but overall (and im not sure if theres any impacts to these changes so take a look and see if it prompts anything in anyone who knows this better than me, because its weird that this "worked"?) but it seems like it was trying to unfurl the objects as well (like it would with an array) so:
resulted in all of these assertions working
|
Hey @kieran-osgood, sorry for the delay in responding. I'll be sinking my teeth into this issue and your response once again this week. |
I took some time to try and understand why this proposed solution works, and I'm not yet sure why it is the case. I had a hypothesis that And interestingly, the test passed... Thus, I think that something specific to the |
@supermacro Yeah I definitely hesitated to call what I shared a proposed solution, really just initial findings from having played with it 😅 but I do think I've narrowed down the problem further! I agree with what you're saying regarding it being related to the way they handle proxying the objects as that was what I was also thinking, and there was tvcutsem/harmony-reflect#19 which indicated that internally
Seems to get all tests running fine and removes the need to wrap arrays This would also explain why my initial solution worked for the proxy issue, as the condition will allow the proxy through (and really any other object), and due to wrapping the value with |
This is great! If you're willing to submit a fix PR with some tests (perhaps installing |
Awesome! I'll get a PR and tests set up either tonight or tomorrow and pop it over 😊 |
…concat internals issue: supermacro#228
Merged. Contacting you to send over bounty! |
@supermacro That's great! How do you usually do this, I can give you my twitter to speak easier? https://twitter.com/kieranbosgood |
I just spent the last few days chasing my tail on this one. I was using several mocking libraries, notably ts-mockito and testdouble, and got the same behavior. If I created a mock object, wrapped it with okAsync(), and tossed that into combine(), I would get nothing in the return- the mock object was being dropped. Here's an example test in jest that would fail:
If you run this, you'd find that the last two expect calls would fail, because combine would return an empty array! It would only do this with mocks, not with normal objects. So I dug into it, and I found a solution in the combine() code. You are using array.concat(nonArray) in the reduce() of combineResults(). I rolled my own combine and it works even with mocks. I added typing support for heterogenous lists as well, but it looks like you're working on a slicker solution in another issue. Here's my "fixed" combine:
The text was updated successfully, but these errors were encountered: