I will show how to save some time on R.
Tests, which will help us, is a CH GPT extension. The following one is a CH GPT quote.
They also favor blank in for running fusion. This unit, using just that, is named just. Run it, a very good plugin actually.
Let’s navigate into some of the projects. Sode uh, and there’s a function update client here. It has a client ID and some input information about the client that needs to be updated. So we see that the client has a full name and home address. When this business function receives a client, it validates it. It’s quite a simple function for the demonstration, just checks that the full name and email are not empty. If they’re not present, an error is shown.
Next, the update function plans to emulate going into the repository. This client ID takes some update token. If this update token is not present, we receive a 403 error and can’t update the client. If all the validation phases are passed and we have an update token to proceed with this operation, we go to the client repository. We provide this client to be already mapped into the database model format entity and this update token that we have generated on the previous step. After that, we can go and receive the updated client details from the response and return back.
Now, let’s check how the CH GPT extension can help us in writing unit tests for that. In order to understand our code, you can run the index code. Based on the current folder of this study example, after that, CH GPT starts to understand the overall context of the project, what functions exist, and index them. Then we can go back here and write a prompt like ‘Write unit tests for client domain main.ts’. For the following file update client, there is some client ID input, okay, for one name, some mock data, mock update token. Then client DB, DB type response, also simulation of database entry here. The updated entry has an email that has some new email in comparison with the previous one.
What about the update client? You just copy-paste the update client DB fields. Before it, it makes just spy CLI repository get update token. So it mocks the first async function and it mocks the update client async function. So if we look into the code, we see that it’s really a unit test because this pass and this I think pass for mared. After each test, it restores all mocks.
Okay, so all three scenarios are working. In some more sophisticated scenarios, you can ask a follow-up question like ‘Write more tests’ and let’s see what CH GPT will do next.
So, it’s the same working data here. What scenarios should update client and return updated client? It was already discussed. It should throw an error if the input is invalid. It should throw an error if the client is not found. So let’s probably take this to new tests that CH GPT has proposed for based on this prompt here. I asked for a follow-up question and let’s put them here and check what CH GPT proposed for us as some more data sets.
So let’s review its logic. It should throw an error if the client is not found. So if client repository get client Mo return value, but we’re not using get client at all, so this test is a bit broken. So let’s just keep it not interesting because we’re not using get client and update function. But the next test looks a little bit more promising. It should throw an error if update client fails. So first, it creates an error update client failed. It spies client repository update client and mocks rejected value is set up as this error. Then it expects that if update client is called, it should reject and throw this error. So what it means for us is that if this update client function is executed from the domain logic and client repository update client fails, nothing swallows this error. So it should be rethrown. So rejects to throw error and we expect that get token should be called, but update client and update client should be called, and it should throw an error if client repository update client fails.
Thank you guys. I hope it will boost your productivity.