Unit Testing

Whatever you do, listen to @jorbascrumps . Do not attempt 100% coverage.

If I were in your position, I’d just isolate whatever code I can unit test, and well, test it (using jest, etc).

The thing to look for is long functions (with more than, say, 30 lines of code or nested if statements). Try to extract that code into smaller functions that you can unit test.

That way, your create/update methods are relatively clean
e.g:

// file1: probable a scene class
create(){
 this.thingies = longProcessToinitializeMySprites(this.add)
 anotherLongProcess(this.add, this.thingies)
}

update(){
 if (hasOnlyCoolSprites(this.thingies)){
   coolProcess(this.thingies, this.input)
 }
}

and then you can unit test hasOnlyCoolSprites, coolProcess, etc because if you really think about it, they may not really depend too much on an actual game running.

Mind you, this approach does introduce some unnecessary abstraction and sacrifices readability of your code, but we are just talking about possible approaches, so just take this as generic ideas and not things you have to do.

1 Like