This is an experiment in writing a JXA script that can be run unmodified by OSA, Node and Jest. As of ES2019 it appears to not be possible to use the Proxy object to mock JXA calls as method calls and properties are indistinguishable. Dependency injection can be used to make method wrappers for the JXA and testing environment.
A very old AppleScript that updates missing track counts in iTunes has been rewritten in JXA and tested with Jest at about 1/3 of the size of the old script.
The code can be run without modification in the following environments:
osascript -l JavaScript set-track-counts.js
node set-track-counts.js
jest set-track-counts.test.js
cp set-track-counts.js set-track-counts.applescript && open -a "Script Editor" set-track-counts.applescript
cp set-track-counts.js ~/Library/Music/Scripts/set-track-counts.js
The script menu can be used to invoke the script in Music.