It's intended behaviour. When you test a game, all the unsaved changes are saved automatically, since GameMaker needs to compile an executable for that, also given that compilation happens using the project saved on the disk.
For testing purposes, you should comment your code out rather than replacing it with new one. You can also use source control for safety which allows you to go back to any instant in your project history at any time.