flatironinstitute / mcmc-monitor

Monitor MCMC runs in the browser
Other
35 stars 0 forks source link

Adds support for vitest tests, code coverage. #45

Closed jsoules closed 1 year ago

jsoules commented 1 year ago

This PR installs packages related to support for vitest, as well as setting up code coverage (with .gitignore support) using lcov for coverage gutters.

I've included example tests for (much of) the MCMCMonitorData file and for the networking/postApiRequest.ts file (which demonstrates at least one way of effectively using mocking with ESM modules and clearing out mocks between tests). I've implemented vitest instead of jest precisely because jest ESM module support remains poorly supported--there are issues over a year old related to incomplete functionality, which does not inspire a great deal of confidence in support going forward. By contrast, vitest has native support for ESM modules throughout. Community support around vitest appears sufficient for it to be a low-risk dependency, and there is definitely something to be said for using the testing framework that's supported by the same org that handles the overall deployment framework.

The model tests are subject to further revision--it's possible they can be written in a more elegant way. In particular, tests for the private functions in MCMCMonitorData.ts require actually exporting what should be private code. I did it this way because it was the only way I could get to work in jest, but there's probably a better practice. (Testing only the public-facing interface would, in this instance, both miss granularity for core units of work and would require needlessly redundant tests through a longer code path in order to hit those core units.)

Draft tests did expose two bugs, which have been corrected as part of this PR. There will also be some refactoring as part of completing tests for MCMCMonitorData.

Further experimentation is necessary for integrating vitest with React-Testing-Library for front-end tests, but there appears to be pretty abundant tutorial material on this subject.

Tangentially, this PR updates the vite.dev-config.ts dev-deployment configuration so that it dynamically modifies (and thus doesn't need to cut-and-paste from) the production vite configuration, so those will be easier to keep in sync. I've confirmed that it still works to deploy directly to the dev/staging area.

magland commented 1 year ago

This looks good. Could you add something to the dev docs to explain how to run the tests?

https://github.com/flatironinstitute/mcmc-monitor/blob/main/doc/developing.md

jsoules commented 1 year ago

Good suggestion; added.