1

Essentially trying to test the result of a function that contains 3 irrelevant string properties (irrelevant to this question). But a 4th property that is a ISO 8601 string that gets created when the function gets executed. I have no way of knowing what this exact value will be, so I want to test the property via regex. Usually I'd do something like this (which would work but not as clean):

expect(desiredProperty).toMatch(ISOPattern); // ISOPattern = regex i made that works

But doing it this way would mean i need to write this expect for every single property, whereas something like this is more clean and easier to read:

expect(result).toEqual(
      expect.objectContaining({
        id: mockEmail,
        otpPassword: mockOtpPassword,
        expire: 1000 * 60 * 60,
        expire_at: expect.toMatch(ISOPattern), // NOT WORKING, FAILS
      })
    );

So I am wondering, is doing something like the above possible at all? I could just not check that property in my expect.objectContaining function, and add another expect to do what i mentioned above. But again, wondering if I could have the best of both worlds.

jdev
  • 21
  • 2
  • You should update the question with the function you are trying to test – doublethink13 Jul 12 '22 at 16:33
  • Maybe I did not phrase it right but essentially I am trying to test the full object (as shown above), instead of doing a expect on every single property in the object. But for the datetime one, I wont know the value I just want to test to make sure it passes a regex. `toMatch` is what youd use outside of `objectContaining`, im trying to see if theres a way to test regex inside of `objectContaining`. Hope that clarifies. – jdev Jul 12 '22 at 19:30
  • Like I said, showing your code should always be the first thing you do. Maybe you regex is wrong. I can't definitely assess the object and its properties' types that you are trying to test. You can also follow a different approach, and mock the Date object. This should make all your dates predictable. You can check my answer [here](https://stackoverflow.com/a/72802768/13294769) or follow on of the approaches [here](https://stackoverflow.com/questions/28504545/how-to-mock-a-constructor-like-new-date) – doublethink13 Jul 12 '22 at 20:40
  • I am already showing my code. I'm not sure I follow on what is missing, I've provided all the relevant code. The question isnt about whether or not the regex is right, im not sure if you noticed but i included a comment saying `NOT WORKING FAILS`, maybe i could have worded that better but it wont compile. Its not broken as in the regex is wrong, its broken as in im not sure HOW to do this, i just put that there to illustrate what i am trying to accomplish/what i already tried. – jdev Jul 12 '22 at 20:42
  • worst case scenario ill just make 2 expects, one to check the object then one other one just to check the date like this `expect(result.expire_at).toMatch(ISOPattern)`, this I know works already I was just trying to see if theres a way to not have to do that extra expect and combine that with the `objectContaining` one I put above. – jdev Jul 12 '22 at 20:44
  • Like I said above, had you provided all relevant code, I could have replicated your issue in my workspace. Comments are evil (a simple Google search will make my point better than I could ever do) so I hardly ever read them. You say your regex works, but, without looking at it, no one other than you can confirm that (even if that's not the point of your question). The problem with your code was that you were trying to assign the return value of the `toMatch` function to a property. That won't work, since your property is a `string` (probably), not `void`, the `toMatch` return type. – doublethink13 Jul 13 '22 at 11:17
  • As for your question, I've also given you a solution. Your tests should be predictable. That means their result should be the same every time. You can only do that if your dates are also predictable. You can do that by mocking the `Date` object. I would use `toStrictEqual`, comparing the result of you function to your expected result, after making the dates predictable. – doublethink13 Jul 13 '22 at 11:19
  • @doublethink Im not trying to be rude but im not sure how else to explain to you that the regex works and is irrelevant to my issue. Whether the regex works or not, is not the issue. I am trying to see if I can make my code below cleaner, again, literally nothing to do with the regex working or not. Not sure how else I can explain this. Im asking if theres a comparable way to achieve this and used `toMatch` as an example of how I do this outside of the context of `objectContaining` – jdev Jul 13 '22 at 15:54
  • Also you did not give a solution, you gave a workaround. I want to test that the code I am running is actually generating the date correctly in ISO format. Your solution works but i don't think thats relevant to what I am asking. Again not trying to be rude here but I think you're misunderstanding what I am asking and therefore you're confused. – jdev Jul 13 '22 at 15:55
  • Also I am aware my code doesnt work, again, I am trying to see if theres a way to do what I am doing in the solution I provided below (maybe this will help understand), but without the extra `expect`. Thats it, again, the regex working is not relevant to what I am asking. I hope this is more clear now. – jdev Jul 13 '22 at 15:56

1 Answers1

0

Not sure if I formatted my post right but essentially this is what I ended up doing (i mentioned this in the OP):

expect(result).toEqual(
      expect.objectContaining({
        id: mockEmail,
        otpPassword: mockOtpPassword,
        expire: 1000 * 60 * 60
      })
);

expect(result.expire_at).toMatch(ISOPattern);

So the idea here was to get rid of the extra expect call (if you notice here I am checking against the same object result, but I wont know the value of the date and its not important anyways so I just want to check some regex I wrote (whether this works or not is irrelevant although the regex works for sure), and somehow do the regex check inside the expect.objectContaining function, but I dont think I did a good job at formatting my question right so thats probably on me.

Open to anyones help if they see a way to do what im talking about.

The code in this solution is what I am using now, the question I am trying to ask is if theres a better way to do this. The regex working is not relevant, although I know 100% it does work. I did not provide this regex because its literally not what I am asking or relevant to the solution I am seeking.

jdev
  • 21
  • 2