Using hardcoded data for comparison in unit tests with toEqual?
Hi there, I have a React app and I want to create a unit test for a function that removes scattered zeros inside of an array. Basically my idea is to have a src\mocks\data\IndexadorDataMock.ts file with hardcoded dirty data, and then the (also hardcoded) clean data - aka the expected output. For example:
And then my test file Helpers.test.ts:
My question is: is this a common practice for React projects? By "this" I mean having a separate file with very long, hardcoded data that you'll use for .toEqual comparisons in your tests
4 Replies
@Muhct Probably you be better using
toMatchInlineSnapshot
instead
@Muhct You save time preparing expected result, it will be generated automatically if you use this method
@Muhct And if something will be messed up in the future you will have the same benefits and if the change is expected you can commit update automatically without the need to manually adjust fileI see... so if you use .toMatchInlineSnapshot() you only have to provide it the hardcoded dirty/input data, and then when you run the test for the first time, it will automatically modify your test file to add the generated clean/output data... right? So at the end of the day you would still have 2 large blocks of hardcoded data
yes
In that case, I would shorten the dummy data and write it directly into the test instead of splitting the file. Do you really need very long dummy data?