Should we source control the .mocks.dart files created by build_runner for mockito

Issue

Because of null safety in dart, we had to update our codes test. We are using mockito for mocking the dependecies for the given class. As per their docs, we decided to use the build_runner method to generate Mock Classes of the dependencies. This is creating files with the extension .mocks.dart in the same folder as that of the test file. Since these are generate files, should we source control them, or put them the .gitignore?

Solution

Actually, that’s up to you, but I would recommend putting them in .gitignore.

Mocks are not sensitive information like access keys or some internal configurations, so no worries from this point of view. However, storing generated files in source control could possibly lead to merge conflicts at some point while working with the team and resolving conflicts on generated files – well, that’s strange and not what you want.

Since the files could be generated, there is no need to add the *.mocks.dart to the source control repository – you should ignore them. Just make sure that you extend your project’s ReadMe file with documentation on how to generate these mocks so all the team members would know what’s happening when tests could not be run. One disadvantage of this is that your team members would probably need to regenerate these mocks every single time before running the tests on newly pulled code, but that’s a drawback you should accept with any code generation.

In my experience, I am using packages like json_serializable, freezed, generating Dart files for localizations, assets using flutter_gen – these also require generating files from source code (e.g. *.g.dart and *.freezed.dart ones). Those files are not stored in source control. Also, when using CI/CD tools for the project, you can just trigger the flutter pub run build_runner build command (or any other that generates your files) before building the code and everything should be good to go.

Answered By – mkobuolys

Answer Checked By – Willingham (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.