PlayCanvas scripts testing ideas

This is more of a ideas & discussion post. While developing a game for 6 months, I encountered many pains with inability to write automated tests for playcanvas script components. I am pondering about how it might be possible to utilize some testing frameworks like jest for testing game scripts, which might significantly simplify development and prevent tons of bugs. Obviously, we cannot do that in the browser, at least for now. So maybe there are options for local environments like playcanvas-sync. We would need the engine functionality for it work. Otherwise, we could only test things not related to the engine, which is not very useful in most cases. And this is where I am not sure how to handle it. Maybe we could use the repo somehow to import the functionality of the engine for testing. Like place the scripts inside the local engine so we could import its functionality there for tests. Or perhaps you have any ideas on how we could make it work?

See how we run engine tests here: engine/test at main · playcanvas/engine · GitHub

Engine is directly imported, you could probably import your game scripts similarly, especially if you use MJS scripts which are in beta (you can join it).

We also have some old tests that we’re moving away from that run inside the browser, perhaps something like this would be an option too: engine/tests at main · playcanvas/engine · GitHub

@will @Mark_Lundin might have some other ideas

1 Like

It’s a bit of tough one.

What I would consider is to use a presenter pattern where your PlayCanvas script types are effectively proxies for the logic class

And that way, you can more easily have automated tests for these without involving PlayCanvas at all or mocking the script type interface

I did try some Unit Testing in PlayCanvas using qunit here Automatic code testing in the online editor?

Although Jest is the industry standard, I believe you would have to run this locally on node or something similar. If Jest is needed, you could have a job/script that could use playcanvas-sync to get all the script files and run Jest against your test folder? Not perfect but would allow you to continue writing code and tests in the Editor

1 Like