Skip to content

Migrating to v1.0 ​

v1.0 rebuilds the runtime behind zotero-plugin test, and finds Zotero for you instead of requiring its path. Existing test suites keep working as they are, so treat this page as a checklist rather than a required rewrite.

Test runtime ​

Import the test API from zotero-plugin-scaffold/test ​

The preferred way to use Mocha and Chai in a test file is the explicit entry point, which gives full type support:

ts
import { afterEach, describe, expect, it } from "zotero-plugin-scaffold/test";

describe("Example Test", () => {
  afterEach(() => {
    // teardown
  });

  it("should pass", () => {
    expect(1 + 1).to.equal(2);
  });
});

The entry point exposes Mocha's bdd interface (describe, context, xdescribe, xcontext, it, specify, xit, xspecify, before, after, beforeEach, afterEach) together with Vitest's expect, Chai's assert, the chai namespace (also the default export) and the zo namespace for spies.

expect follows Vitest ​

expect is now the same assertion library Vitest uses: Chai plus @vitest/expect's plugins. The Jest matchers work out of the box:

ts
import { describe, expect, it } from "zotero-plugin-scaffold/test";

describe("Example Test", () => {
  it("should pass", () => {
    expect(1 + 1).toBe(2);
    expect({ a: 1 }).toEqual({ a: 1 });
    expect(() => {
      throw new TypeError("boom");
    }).toThrow(TypeError);
  });
});

Chai's own assertions still work on the same object (expect(1).to.equal(1)), so existing suites need no change. expect.extend, expect.addEqualityTesters, the asymmetric matchers (expect.any, expect.objectContaining, …) and the per-test checks expect.assertions(n) / expect.hasAssertions() are available as well.

Spies come from @vitest/spy ​

The spy matchers (toHaveBeenCalled, toHaveBeenCalledWith, toHaveBeenCalledTimes, …) were useless without a way to create a spy, so v1.0 also ships @vitest/spy, the library Vitest's vi.fn() / vi.spyOn() are built on. Its members are collected into zo, the stand-in for Vitest's vi:

ts
import { afterEach, describe, expect, it, zo } from "zotero-plugin-scaffold/test";

describe("Example Test", () => {
  afterEach(() => {
    zo.restoreAllMocks();
  });

  it("records calls", () => {
    const callback = zo.fn();
    callback(1);

    expect(callback).toHaveBeenCalledWith(1);
  });

  it("wraps an existing method", () => {
    const service = {
      greet: () => "hi"
    };
    expect(zo.spyOn(service, "greet")).not.toHaveBeenCalled();
    service.greet();
    expect(service.greet).toHaveBeenCalledOnce();
  });
});

zo holds fn, spyOn, isMockFunction, clearAllMocks, resetAllMocks and restoreAllMocks, plus the fake timers (useFakeTimers, advanceTimersByTime, …). It is a namespace rather than a dozen top-level exports so that another library (the timers behind vi.useFakeTimers()) extends it without adding generic names next to describe / expect.

Spies are not reset between tests: a zo.spyOn() keeps the object patched for the rest of the run, so suites that use it need their own afterEach(() => zo.restoreAllMocks()). Scaffold does not install that hook, because a throwing root beforeEach makes Mocha skip the remaining tests. Fake timers are the one exception: the runtime uninstalls the clock after every test, because a clock left installed freezes every timer of the Zotero window.

Not available are the parts which need Vitest's own runner: snapshots, vi itself (and with it vi.mock(), which needs an ESM module graph a Zotero page does not have, and vi.stubGlobal()), expect.soft and expect.poll. The latter two throw a descriptive error instead of silently passing.

Bare globals (describe, it, expect, assert, …) are still installed by the test runtime, so a suite that was written before the entry point existed needs no change. Adding the import is what gives you types and IDE completion.

These imports never inline Mocha or Chai into your test bundle: the bundler redirects them to the runtime, which loads the libraries once, before your tests.

Test libraries are provided by Scaffold ​

mocha, chai, @vitest/expect and @vitest/spy are bundled into the test runtime, so they are no longer read from your project: drop them from your devDependencies.

The old lookup chain (node_modules → .scaffold/cache → CDN) is gone, and nothing is downloaded at test time any more. mocha.js and chai.js left in .scaffold/cache can be deleted.

No longer supported: driving Mocha yourself ​

Importing the libraries to set them up is not part of the test API. In particular:

ts
// not supported
import Mocha from "mocha";

const mocha = new Mocha();
mocha.setup("bdd");
mocha.run();

Test bundles are redirected to a shim that only reads the globals installed by the test runtime; it exports no mocha instance. Use the entry point above instead, and let Scaffold own setup() / run().

The test UI is fixed to bdd: tdd, qunit and exports are not available through configuration.

The test command takes filters ​

zotero-plugin test accepts positional arguments now, which is how a subset of the tests runs without editing the configuration:

bash
zotero-plugin test prefs

An argument is a case-insensitive fragment of the test file path, so prefs alone usually selects what you want. To run exactly one file, pass its path in quotes, so the command receives the quotes and matches the path instead of the fragment:

bash
zotero-plugin test '"test/prefs.spec.ts"'

The selection itself still comes from the configuration: test.entries keeps naming the directories that hold the tests, and test.exclude is new, for the files that should not run:

ts
export default defineConfig({
  test: {
    entries: ["test"],
    exclude: ["test/legacy/**"]
  }
});

The arguments fill test.filter, which narrows one run down; test.entries and test.exclude decide which files exist at all. Neither of the two changed its meaning, so an existing configuration keeps running the same tests.

Generated files ​

The test page is assembled from two parts:

FileContent
content/test-runtime.jsMocha and Chai, plus the reporter, rebuilt on every run
content/units/*.jsYour test files only, as selected by test.entries

The runtime options (test.mocha.timeout, test.abortOnFail, …) are injected into content/index.xhtml instead of being baked into the runtime, so changing them needs no extra build step.

The Zotero binary path is optional ​

ZOTERO_PLUGIN_ZOTERO_BIN_PATH may now be left out, or empty: an installed Zotero is detected instead. A pipeline that relied on the empty value failing should point the variable at a path that does not exist — that is still an error.

@zotero-plugin/utils ​

saveResource was removed ​

It only existed to download the test libraries from a CDN. If you used it in your own scripts, replace it with a plain fetch and write:

ts
import { outputFile } from "fs-extra/esm";

const response = await fetch(url);
await outputFile(path, await response.text());