Add more unit tests for PyButton and BaseEvalElement#711
Conversation
marimeireles
left a comment
There was a problem hiding this comment.
This is great effort!
Thank you so much @FabioRosado :)
My comment on the test with the error is because I'm thinking well, mostly about maintainability... Should we create tests that we know it fails?
@antocuni will probably have an input here as he introduced this discussion on our previous PR.
But if anyone else wants to chime in, please!
| it('BaseEvalElement instantiates correctly', async () => { | ||
| expect(instance).toBeInstanceOf(BaseEvalElement); | ||
| }); | ||
|
|
||
| // TODO: This test is a bit silly, but the idea is to keep it here until we fix this | ||
| // issue | ||
| it("should fail with: Cannot use 'in' operator to search for 'runPythonAsync' in undefined", async () => { | ||
| let thrownError; | ||
| let expectedTypeError = new TypeError("Cannot use 'in' operator to search for 'runPythonAsync' in undefined"); | ||
| try { | ||
| instance.runAfterRuntimeInitialized(async () => { | ||
| return; | ||
| }); | ||
| } catch (error) { | ||
| thrownError = error; | ||
| } | ||
| expect(thrownError).toEqual(expectedTypeError); | ||
| }); |
There was a problem hiding this comment.
I'm wondering there's no other way to test this functionality? Or to ignore this error?
There was a problem hiding this comment.
That's a great question(s)!
We could mock runAfterRuntimeInitialized similar to what I've done in pybutton.test.ts so this exception won't impact the tests.
This test was added mostly because I was poking at the exception and trying to figure out what's happening and thought it could be good to include in the PR so that when we fix the issue, his test will fail and we can remove it. Maybe that's a bad idea? 😄
Happy to remove it if you think is best
There was a problem hiding this comment.
I think it might be the best approach for now. But we can get back to it once Antonio is back on vacation and I hear what he has to say =)
There was a problem hiding this comment.
This test was added mostly because I was poking at the exception and trying to figure out what's happening and thought it could be good to include in the PR so that when we fix the issue, his test will fail and we can remove it. Maybe that's a bad idea?
I think it's a very good idea! +1
|
Thank you, if this seems like a good approach I'll start adding more tests to cover other parts 😄 |
|
Yeah sure! This is a great approach. |
|
Also, seems like there are some small issues with the tests. |
|
I think this might be happening because I haven't merged main into this branch yet 🤔 Let me try doing that and run the tests again just to see |
|
Hi @FabioRosado you might wanna rebase on |
|
Thanks Madhur and Mariana! I've rebased main into the branch and fixed the imports, I've also marked the integration test |
|
Thanks for the work Fabio! ✨ |
|
I was trying to reproduce the |
|
Thank you Mariana! |
antocuni
left a comment
There was a problem hiding this comment.
Sorry for the late review! @FabioRosado thank you for this PR, I'm always happy when I see new tests :)
To answer your question:
I wonder if this is the right approach to testing each component and perhaps some internals.
[cut]
Please let me know if you would prefer to test these differently
JS unit tests are good, and our current situation is so bad that every progress in this area is very welcome.
Personally, I think that integration tests are more important, though.
In my view, unit tests are more an internal tool which is useful for us to ensure the correctness and quality of our code.
On the other hand, integration tests represent our contract with the end users: they declare (and check!) what happens when you use pyscript in your HTML page and that when we refactor the code we don't accidentally break legitimate usage of it. For every user-visible functionality of pyscript, we surely want to have at least one integration test which checks that it keep working as expected.
In the long run both are important, but if I have to choose, in the short term I'd probably prefer to increase the coverage of integration tests. But this is just my personal opinion which might or might not be shared by the rest of the team
| } | ||
| expect(thrownError).toEqual(expectedTypeError); | ||
| }); | ||
|
|
There was a problem hiding this comment.
Disclaimer: this is literally the first time ever which I see a Javascript/Jest unit test so I don't really know about the idioms. If I understand correctly, this is basically the equivalent of pytest.raises, isn't it?
Is it there a more direct way to do it? A quick googling tells me about expect().toThrow() but I don't really know whether it applies here.
Also, small nitpick: it would have been nicer to put the issue number in the comment, so that it will be easier to understand why the test fails once the issue it's fixed.
There was a problem hiding this comment.
Yeah my idea was to write a test similar to pytest.raises 😄
Oh I didn't knew about the toThrow check, I'm still getting used writing js/jest tests haha
That's a good point that didn't occur to me. I'm planning to write more tests over the weekend so I'll make sure to add the issue number on this one 😄
| it('BaseEvalElement instantiates correctly', async () => { | ||
| expect(instance).toBeInstanceOf(BaseEvalElement); | ||
| }); | ||
|
|
||
| // TODO: This test is a bit silly, but the idea is to keep it here until we fix this | ||
| // issue | ||
| it("should fail with: Cannot use 'in' operator to search for 'runPythonAsync' in undefined", async () => { | ||
| let thrownError; | ||
| let expectedTypeError = new TypeError("Cannot use 'in' operator to search for 'runPythonAsync' in undefined"); | ||
| try { | ||
| instance.runAfterRuntimeInitialized(async () => { | ||
| return; | ||
| }); | ||
| } catch (error) { | ||
| thrownError = error; | ||
| } | ||
| expect(thrownError).toEqual(expectedTypeError); | ||
| }); |
There was a problem hiding this comment.
This test was added mostly because I was poking at the exception and trying to figure out what's happening and thought it could be good to include in the PR so that when we fix the issue, his test will fail and we can remove it. Maybe that's a bad idea?
I think it's a very good idea! +1
|
Thank you for the review Antonio, that makes sense, I'll keep adding both integration tests and unit ones too now that I know this is a good approach 😄 |
This PR is part of the effort to increase test coverage (#679). Two new test files were added:
I wonder if this is the right approach to testing each component and perhaps some internals. Many of these tests are just asserting default behaviours and checking that things changed as we expect when calling functions.
Please let me know if you would prefer to test these differently or if you think it's good then I'll keep writing tests for the other files 😄