Unit tests with zotero-plugin-template
lifan0127 opened this issue · 16 comments
May I ask for your advice on how to incorporate unit tests in Zotero plugin development? I couldn't find any reference in the zotero-plugin-template documentation.
Separately I see how zotero-better-bibtext set up tests which is quite complicated: https://github.com/retorquere/zotero-better-bibtex/blob/master/test/behave
Do you have any guidance? Thank you!
BBT has many translators and its tests are mainly written for these UI-independent modules. He uses a debug-bridge to run code in Zotero, which is a standalone plugin and is a little bit complex.
I introduced a lightweight debug bridge in the plugin toolkit package. You can run zotero://debug-bridge/ uri to trigger JS command or run outside JS file in Zotero. You can build your test scripts based on that.
If you want to use existing unit test frameworks, it would not be that easy.
For UI-independent modules, it’s possible to use existing test frameworks, simply monitoring the Zotero API you use in your code with synthetic data returns.
However, most plugins’ features are UI-dependent, and these modules also depend on Zotero apis. You may try web-based UI test tools, but I don’t expect them to work with Zotero as the main window is not in a standard HTML environment.
See here for details of the debug bridge: https://github.com/windingwind/zotero-plugin-toolkit/blob/master/src/utils/debugBridge.ts
Hi @windingwind, I would like to test UI-independent modules via unit tests. How do I run a zotero://debug-bridge/ or chrome://ztoolkit-debug/ uri from a unit test framework such as jest?
Hi, the command line way is:
/path/to/zotero -url zotero://ztoolkit-debug/?xxx
You may need to save the test results to a log file, as there are no return from the custom protocol handler.
It is not matured yet. I'm going to spend some time on plugin hot reload and debug/test and welcome cooperation.
Thanks! I was able to run the "Hello World" example. How do I use this approach to test my code, for example, execute a function and check its output?
Passing the path of your test script (which may contain a call to your target function and do some check, then output the result to somewhere) to the command:
/*
* @example
* Run script from file. The `file` is URIencoded path to js file starts with `file:///`
*
* `chrome://ztoolkit-debug/?file=file%3A%2F%2F%2FC%3A%2FUsers%2Fw_xia%2FDesktop%2Frun.js`
*/
A rough example:
// Do the test
const result = test_xxx()
// Save test results
Zotero.File.putContentsAsync(path, result, "utf-8")
You may want to use libs like Chokidar to watch the output file in the main test script running on NodeJS. After the file is written or we meet a timeout, you know one test is done and continue with the next.
Sorry to keep bothering you. I got the following error:
NS_ERROR_DOM_BAD_URI: Component returned failure code: 0x805303f4 [nsICommandLine.resolveURI] (zotero-service.js:540)
I use Ubuntu. My file:/// URL is correct as tested with curl.
Also, how would I be able to import my functions defined under src/modules
from the test file?
Is it possible to import my functions defined under src/modules from the test file?
Oops, sorry I missed the message. I didn't try with Linux, but basically speaking, try file path w/ or w/o file://
. One of them would work.
Yes you can import functions in your project. Export them in the source file and import them in your test file. Maybe you'll need to write an extra build script to compile the test script.
If I have missed any point, please do not hesitate to let me know. Thanks for your patience!
It finally works for me now! In my case, the URL has to start with "zotero://ztoolkit-debug" instead of "chrome://ztoolkit-debug".
I will look into how to compile the test file.
Ops, my bad. It is zotero://ztoolkit-debug
.
I suspect the file content is somehow "cached" by Zotero. Every time I change the test file, I will need to restart Zotero for the change to take effect. Is it the expected behavior?
Yes. If you bundle the test functions inside the plugin, they would only be refreshed after restarting Zotero I guess.
Please don't put test functions in the production build, as the plugin package would contain unnecessary things.
Do you recommend bundling test functions with the plugin (conditionally based on NODE_ENV)?
My plan is to make a bundle of all the test functions (following the scripts/build.js example) and then execute the bundled test file through command line.
Thanks!
Oh sure. Running tests inside Zotero is OK.