-
Notifications
You must be signed in to change notification settings - Fork 2
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
bug: dates are wrong type in Jest #187
Comments
Which dates on which entity types? We're quite extensively working with dates at the moment and haven't seen that issue. |
The |
Ah, right. I forgot that all our queries are done using the option { decodeDatesAsIso: true }, which uses the upcoming default behaviour of being strings. The types are, as you say, incorrect unless you use that option. They currently come back as Moment object otherwise. We looked a bit at setting the type depending on the options used, but it felt messy (And is one of the reasons we're moving to a single return type as we're discussing in #71). |
Aha. I think it's OK for us to use that option as well. Thank you for clearing that up! Closing in favor of #71 then. |
Wait, we just tried this option. It doesn't work. I suppose it means that the option is not yet out until #71 is done? Will re-open this. What alternatives to we have for now? |
That option has been available since quite a while. The option is on a query/mutation level, the second parameter in any query or mutation takes an options object. |
Ah interesting. I think with that, we'd be afraid to forget to specify it. Can we make it support setting query-level default options in the constructor of the session? |
There's already a way to globally add |
Great. Thank you for that update. I agree. Maybe this issue can serve as that? |
We actually took a decision about the 2.0 release earlier today, so your PR had great timing. We tried getting the 2.0 out this week or the next, but we found internal dependencies that would break with the planned 2.0 changes. They were already scheduled to be removed in the coming month, so it made no sense to rebuild them. We also don't want to release the JavaScript API to a state where we wouldn't be using the latest version. We'll get your PR reviewed and released ASAP. |
Awesome stuff! Great news, and thanks for keeping us posted! |
Can we reopen this @gismya? We just tried setting that option, and it doesn't work. The strong-typing types it as |
Note that we tried setting it in the constructor, so perhaps it's my fix that doesn't work. Not sure. |
Looks like it should work. Make sure you're running the latest version (1.8.3) and initializing the constructor settings correctly
If you still have issues, send a code sample I can tests that has the issue and I'll try to find any bugs. |
It's so weird. That's exactly what we're doing. And we have that latest version. I'll see if I can make a better repro. |
@gismya it is indeed still happening. The type of (for instance) Repro: const session = new Session(
ftrackServerUrl,
ftrackApiUser,
ftrackApiKey,
{ decodeDatesAsIso: true },
);
await session.initializing;
const project = await session.ensure<Project>('Project', {
end_date: '2023-12-31T00:00:00',
name: "some-random-project-name",
});
//here, project.end_date is of type string according to TypeScript.
//but as you can see in the error below, the runtime type is different.
expect(project.end_date).toBe('2023-12-31T00:00:00'); Error:
The above error is for the |
Based on the above, can we re-open as well? |
Thanks. Also, I'm not sure if this is a bug in the typings or the issue should be moved to the API client project. |
Sounds like an API client error, but I'll look into it. |
That's not a working reproduction example. Are you using mocking in your tests, or is that running towards an instance? Because the returned value looks incorrect, there should be no |
No mocking involved - it's an integration test running directly towards a real Ftrack instance, running version 4.12. However, the types were generated waaaaaay back. Just right after the TS generator was published, and back then, we were using Ftrack 4.10 (so almost 2 year old version). Could this have been the cause? But then, what I don't get is that the type is wrong at runtime. When I did the repro, it was just that example I gave you. No modifications. It still fails, towards a real Ftrack instance. Very strange that you're not getting the same results! |
It seems like the result you get from the server is incorrect, which is super weird. This is an on prem instance? Also, I saw that the GitHub formatting incorrectly picked up the keys I mentioned in the last post: the issue is that the But the issue isn't with the type or the API client from what I can tell. |
Interesting. Yes, it's an on-prem instance. Hmmm is that what causes the schema type generator to generate wrong types then, or? |
It would cause the API client to not convert it to a string (That the schema says it is). Can you try to verify the data coming from the server somehow, it could also be the test runner, the pasting to github or something else that removes the
and inspect the response in the network tab. It should look like
|
It was indeed GitHub ruining the formatting. I changed from a quote style block into a code block now. I could also see when I edited the comment that the code was indeed I saved the modification to the comment now, so it reflects the latest. Does that change anything? I'll try the given line in the console soon. |
Running the command on my trial cloud account gives me this result: {"action":"query","data":[{"__entity_type__":"Project","id":"33b1e354-8b3d-11eb-8e99-c2ffbce28b68","context_type":"show","start_date":"2023-10-02T00:00:00.000Z"}],"metadata":{"next":{"offset":1}}} |
The command on our on-prem cluster yields a similar result: {"action":"query","data":[{"context_type":"show","__entity_type__":"Project","id":"000877aa-6e5e-11ee-a4cd-a260f9cca335","start_date":"2020-01-01T00:00:00.000Z"}],"metadata":{"next":{"offset":1}}} |
Does it matter if it is run from Node or web perhaps? We are running it from Node. |
Still can't find anything wrong either through the browser or node ,on any instance I have or create. I'll close this until I get a reproducible example. From what I see now, my best guess is that the test runner is doing something weird with the value before comparing. Make sure the value you get from a query really is not a string. |
That's the weird part. Our error also occurs in production. I will try to see if one of our other devs can repro, and then get back to you. |
I found the cause @gismya! But not sure how to solve it. I think it has to do with differences in the value of But not sure how that is triggered. Is it a setting somewhere in Ftrack? |
I don't know how the server settings are set up for on prem. Send an email to support for further help with that. |
Alright, will ping them. However, actually, some further thoughts... Should that setting even affect the decoding? Why is that check actually there? In my naive viewpoint, decoding of times as strings instead of dates should be unrelated to whether timezone support is enabled. It should be supported either way, right? |
It should not break, and I don't know why it does, but since it's a deprecated setting we probably won't make any investigations on it or changes regarding how they are handled in the API client. |
Yeah makes sense. Any plans on dropping support for older Ftrack servers anytime soon? |
Also, I wonder if it changes anything to run it via Jest. |
Also, this line could be suspicious. Is it needed? I understand the check for object, but why the check for the constructor of the object? Maybe that one is somehow different in Jest or whatever? |
Because JavaScript is weird.
But I think you're right that something in the decoding function is behaving differently in your Node environment. Try making a custom build of the API client with a bunch of logs there to try to find the culprit. After we find what behaves differently we can find an alternate solution to the decoding function. |
Awesome, good idea! Will do, and get back. |
I have some interesting findings. I attached a debugger, and landed here after the Then, at that point, I tried evaluating some things in the debug console. This indeed confirms my suspicion! Even though This is from my Jest environment. |
Oh, and |
If I change the function into this, it works: decode(data, identityMap = {}, decodeDatesAsIso = false) {
if (Array.isArray(data)) {
return this._decodeArray(data, identityMap, decodeDatesAsIso);
}
if (!!data && typeof data === "object") {
if (data.__entity_type__) {
return this._mergeEntity(data, identityMap, decodeDatesAsIso);
}
if (data.__type__ === "datetime" && decodeDatesAsIso) {
return this._decodeDateTimeAsIso(data);
} else if (data.__type__ === "datetime") {
return this._decodeDateTimeAsMoment(data);
}
return this._decodePlainObject(data, identityMap, decodeDatesAsIso);
}
return data;
} Although we still don't understand the root cause (my suspicion would be that Jest does some default import mocking where it replaces the Object constructor or whatever), would this code be problematic? The way I see it, it would still do the same, and handle all the cases you described above. Will make a PR for this, just in case. |
Also, I did a separate Jest test with these checks: Object === {}.constructor //evaluates to true
Object === Object.create(null).constructor //evaluates to false
Object === Object.create({}).constructor //evaluates to true That seems to be pretty correct to me. So I don't get why it's different with the Ftrack API. By the way, the file that ends up getting imported is Also, is the behavior of those 3 lines intentional? Not sure how the Ftrack API creates its objects. But may it proves that this check is not the best, and should be excluded altogether or changed? |
And one more thing... I feel like we have enough information now to re-open it 🙏 Perhaps even move it to ftrackhq/ftrack-javascript. |
Good find. You can open this in the ftrack-javascript repository, with a short summary and then you can link to this issue for the details. With this we definitely have enough information to solve this. But that's optional, we can solve it without that if you don't want to create it. |
There's a move button in github's UI though, and that'd move the full discussion too. I think that makes most sense, but only you can do that. If you still think I should make a new issue, I will 🙏 |
Did not know about that. Thank you! |
You're very welcome, and thanks for merging! 😍 |
Dates are typed as
string
, whereas the real type seems to be{ value: string }
.The text was updated successfully, but these errors were encountered: