Author Topic: Jest - JavaScript Testing  (Read 2397 times)

Offline Hooman

  • Administrator
  • Hero Member
  • *****
  • Posts: 4955
Jest - JavaScript Testing
« on: October 02, 2017, 01:02:15 AM »
I came across the Jest library for JavaScript testing. Looked like it might be worth remembering. I particularly liked the way it supports snapshot testing, and how it eases the burden of writing and updating these sorts of tests. The first video on the page gives a good overview of it. I'd like to see that feature in other testing libraries.

Jest also has built in code coverage reports, so you know how much of your code base has been tested.

@lordpalandus: This could potentially be used for testing Cataclysm of Chaos. I believe if the source was extracted from the zip file, and a test harness was written that pointed to the extracted source, it could work. If you're interested, let me know and maybe we can figure something out.

I'd also be curious about using this myself in the future. I wanted to play around a bit with some JavaScript code, so this would be good to know.

Offline lordpalandus

  • Banned
  • Hero Member
  • *****
  • Posts: 825
Re: Jest - JavaScript Testing
« Reply #1 on: October 02, 2017, 01:54:26 AM »
Testing for what? Syntax errors? Its the logic errors I have the most problem with hunting down and fixing. The runtime errors are sometimes my fault, sometimes the engine. I often find most of my errors by just playing and replaying the game... though finding some of the more pesky errors would be ... swell.
Currently working on Cataclysm of Chaos, Remade.
Link to OPU page = http://forum.outpost2.net/index.php/topic,6073.0.html

Offline Hooman

  • Administrator
  • Hero Member
  • *****
  • Posts: 4955
Re: Jest - JavaScript Testing
« Reply #2 on: October 03, 2017, 03:08:20 AM »
Oh, I think you would love automated testing.

Automated testing is largely about logic errors. Since the code is executed, it must of course be parsed, and so you essentially get syntax error checking for free too.

Quote
I often find most of my errors by just playing and replaying the game...

This is exactly what automated testing helps to reduce. At a broad level, you could write tests that essentially script a part of play, and verify expected outputs. At a more detailed level, you can exercise specific functions, without having to go through the trouble of playing the game to get to a point where that function is called. It cuts down drastically on the time and repetition involved in testing. When refactoring code, an automated test can tell you if you've broken something in a fraction of a second.


Better yet, if you do local JavaScript development (which I realize you're not doing here), you can setup a file system watcher that knows to rerun your tests each time you save changes to your JavaScript file. You make a change, save it, and see the tests run in a window off to the side, with color coded red/green output depending on the test results.


As for how to test your code, I may need to think further about it. I was just taking a scroll through your updated code. It seems to be stored in an XML file, with the JavaScript in sub tags. That's a slight complication for testing, since it's not just a raw JavaScript file, so it can't just be included by the test code. Might need some kind of pre-processing step to extract the code into plain JavaScript files, and then run it through tests. This is probably not too difficult to do using an XML library, and it would be able to sort each block of code into its own function or file for testing.