Guidelines, best practice and Testing Template

I’m new too Phaser, but I was a developer for many year’s, I was tinkering with phaser for a while now, and think I’m now ready to start coding phaser apps, a bit more professional. I found some tempaltes like the " Enclave Phaser Template" or the various OurCade Templates, but still some questions were not answered:

  • Are there some Templates out there, that use/support unit testing?
    • I don’t care which framework (mocha, jest, jasmine, …) and also not if Javascript or Typescript
  • Are there some best/good practice Checklist (except the aforementioned Templates), that one could use as guide?
  • Has anyone some tipps/reomendations for blog post, books, guidelines, videos … on how to approach a structured and stable phaser development?
    • Even “Code”-smells and Bad practices would also helpful for me.

I have some ideas, but I wanted to ask the community before I reinvet the wheel. :innocent:
Any Help is appreciated :hugs:

Well:
1 - In phaser page you have plenty of examples that you can edit and test in the webpage without having to download it. Phaser - Examples - Phaser 3 Examples

2 - You have some tutorials from 0 ground how to do start with a good practise. Tutorials, Videos, Docs and more - Learn - Phaser

3-About videos i have no ideia.

P.S. phaser changes a lot, so pay attention to the vertions, phaser 3 various a lot from phaser 2 in some aspects, so a lot of functions from 2 will not work on 3.

1 Like

Thank you for your reply, it is very helpful. I’m still working through the interesting examples, and learning alot. :+1:
you are right, the versions are a bit tricky. Since I once looked for hours for a “bug”, that was only related to a wrong phaser version, I always double check if I’m using the correct/latest version.

Would you maybe know about unittesting phaser code also?
I just found some older threads like, but none have a template on how to unit test a phaser application:

What you mean by unit test? You mean like a bot testing your program?

Unit testing in Phaser is a bit rare. You may be able to do it with Jest if you mock a lot of things.

I’ve done some integration testing (I guess) with Mocha in the browser.

You dont have the purpose of doing unit testing, you can do integrating ones but phaser is not a heavy platform, so you might face some bugs with sizes and positions, most stuff are not worth testing, you use unit testing more for specific new or modular implementations. I think.

Yes, something like that, but you are wright, it would make much sense and would be to much work.
I will just have to programm abit more modular and just test only important logic block /classes :thinking:.

1 Like

thanks for the insides, I will probablly also opt for integration testing as I’m not a fan of mocking too much code and I just need some test, to prevent regression bugs.
And mocha is more my cup of coffee :wink:

1 Like

Not a “best practice” by any means, but in the development of my game I have two types of tests:

You also have the option of mocking Phaser if your testing code has side effects so that you can assert if you are calling the framework with the expected parameters.

1 Like

Thank you for sharing your experience and code. It is a lot to go through, and I’m looking forward to do a deep dive this weekend. Seems very interesting, and seems to shows case the testing aspects also.

btw.: I’m also pretty interested in the cuccumber specifications, I never saw it used outside of demo code, but the specifications, in the repo are really nice and realistic.

Cucumber-styled specs are good to reason about and make it easier to come up with the spec before coding.
As a personal preference, I don’t like using tools that parse files written in gherkin - that adds the complexity of gherkin syntax and rules to your tests, and you can easily emulate the style with regular functions.
If you are getting started, try to practice with “data comes in, data goes out” checks. If your game has a function to “pick the closest enemy target”, design it in a way that will make it side-effect-free (like receiving an array of enemies and returning an id). This way testing becomes trivial. If you need to sort a list of entities by an attribute (hp, level, speed), write a test to check if it sorts as expected. The more logic you are able to check without launching your game in an actual browser, the better.

1 Like