plugin-reload.test.ts 1.5 KB

123456789101112131415161718192021222324252627282930
  1. import { writeFile } from "node:fs/promises"
  2. import path from "node:path"
  3. import { pathToFileURL } from "node:url"
  4. import { expect, test } from "bun:test"
  5. import { freshSpecifier, localSource } from "../src/plugin/discovery"
  6. import { tmpdir } from "./fixture/fixture"
  7. test("localSource resolves file URLs and local paths but not package specs", () => {
  8. const base = process.cwd()
  9. const absolute = path.resolve(base, "abs", "plugin.ts")
  10. expect(localSource("file:///tmp/plugin.ts", base)?.href).toBe("file:///tmp/plugin.ts")
  11. expect(localSource("./plugin.ts", base)?.href).toBe(pathToFileURL(path.join(base, "plugin.ts")).href)
  12. expect(localSource("../plugin.ts", path.join(base, "nested"))?.href).toBe(
  13. pathToFileURL(path.join(base, "plugin.ts")).href,
  14. )
  15. expect(localSource(absolute, base)?.href).toBe(pathToFileURL(absolute).href)
  16. expect(localSource("some-package", base)).toBeUndefined()
  17. expect(localSource("@scope/some-package", base)).toBeUndefined()
  18. })
  19. test("freshSpecifier re-imports a plugin source after it changes", async () => {
  20. await using tmp = await tmpdir()
  21. const file = path.join(tmp.path, "plugin.ts")
  22. await writeFile(file, "export default 1")
  23. const first: { readonly default?: unknown } = await import(freshSpecifier(pathToFileURL(file).href, 1))
  24. await writeFile(file, "export default 2")
  25. const second: { readonly default?: unknown } = await import(freshSpecifier(pathToFileURL(file).href, 2))
  26. expect(first.default).toBe(1)
  27. expect(second.default).toBe(2)
  28. })