Closed sbarber2 closed 7 months ago
As was mentioned in #118, here's a nice roundup of JavaScript unit testing tools: https://raygun.com/blog/javascript-unit-testing-frameworks/. Also, https://2022.stateofjs.com/en-US/libraries/testing/ gives a good idea of the popularity of the available JavaScript testing frameworks.
Selenium is implemented in PlantTracer using its Python API (pip install selenium). Selenium is, however, a browser application automation tool, primarily, and not so much a unit testing tool. Also, the way most people code Selenium tests, a new browser process is started and stopped for every test method, so throughput is going to be limited. So, Selenium will be good for E2E (end-to-end) tests, but not for high-coverage unit tests. Selenium can be used as means to initiate JavaScript unit tests if desired by importing JavaScript test frameworks to use alongside Selenium, but that seems to me to stretching Selenium into places it really wasn't designed to go.
Looking at the available frameworks, I'm planning to start with Jest since its market penetration is very high, its retention is still pretty good, its documentation thorough, and there's no shortage of examples out there, as well as people to answer questions. It has auto-mocking, which may help (but also may slow down test runs). It does automatic screen snapshotting during test runs, which will be helpful, I think. The learning curve doesn't look steep. What Jest is missing is great JavaScript-in-a-browser and easy cross-browser support.
So, I think a cross-browser-oriented test framework would also be nice. The candidates there appear to be Puppeteer, Playwright, and Cypress. I'm most attracted to Playwright on paper. It's fairly new, but supported by Microsoft and gaining popularity, and the API and setup seem simpler than Puppeteer's. Cypress's API just looks obscure! So I'll look at Playwright next, after Jest.
I think it's going to make sense to put tests written in a JavaScript in their own top-level folder under webapp, not under tests. Perhaps jstests? This is because the tests folder contains only pytests now, and there's no easy way to exclude sub-folders from pytest when there are pytests in the top-level tests folder. But very open to suggestions.
One item to consider is whether each JavaScript testing framework will want its own tests subfolder. On initial reading, I tend to think not as there are so many examples of using multiple frameworks together. So, I'll probably start with jstests.
Narrow the scope of this issue to setting up functional unit tests with Jest.
Actually writing unit tests will be separate issues.
Setting up Playwright for in-browser and e2e testing will be a separate issue.
It would be good that functional unit tests for JavaScript modules such as planttracer.js
We need to select one or more JavaScript testing frameworks and we need to consider both in-browser and standalone JavaScript testing.