aklinker1 / webext-core

Collection of essential libraries and tools for building web extensions
https://webext-core.aklinker1.io
MIT License
116 stars 13 forks source link

@webext-core/fake-browser don't mock the webextension-polyfill with vitest #43

Closed kayac-chang closed 11 months ago

kayac-chang commented 11 months ago

Description

The mocking functionality no longer works. The following is the error we encountered in the vitest.

Error: This script should only be loaded in a browser extension.
 ❯ node_modules/webextension-polyfill/dist/browser-polyfill.js:10:9
 ❯ node_modules/webextension-polyfill/dist/browser-polyfill.js:5:5
 ❯ Object.<anonymous> node_modules/webextension-polyfill/dist/browser-polyfill.js:13:3

Dependencies

Steps To Reproduce

I follow the documentation with a minimal setup.

Also, provide the repository https://github.com/kayac-chang/webext-core-fake-browser-test

aklinker1 commented 11 months ago
  • "@types/webextension-polyfill": "^0.10.5"
  • "@webext-core/fake-browser": "^1.2.2"
  • "typescript": "^5.2.2"
  • "vite": "^4.4.9"
  • "vitest": "^0.34.5"
  • "@webext-core/storage": "^1.1.3",
  • "webextension-polyfill": "^0.10.0"

In the future, make sure you add your lockfile to reproductions. Otherwise maintainers might use different versions of dependencies compared to your local setup.

I haven't updated the fake-browser package for a while, so it's likely something Vitest broke, but let me check it out.

aklinker1 commented 11 months ago

Related to https://github.com/vitest-dev/vitest/issues/3936#issuecomment-1739836844.

diff --git a/vitest.config.ts b/vitest.config.ts
index 7f3ee78..6c7b4f1 100644
--- a/vitest.config.ts
+++ b/vitest.config.ts
@@ -7,5 +7,10 @@ export default defineConfig({
   test: {
     globals: true,
     setupFiles: ["vitest.setup.ts"],
+    server: {
+      deps: {
+        inline: ["@webext-core/storage"],
+      },
+    },
   },
 });
aklinker1 commented 11 months ago

I've updated the docs. Read them here: https://webext-core.aklinker1.io/guide/fake-browser/testing-frameworks.html#vitest

https://github.com/aklinker1/webext-core/commit/3f7187d75ad0cbf557b6d5484e367dac83670854

kayac-chang commented 11 months ago

It works now! Thank you @aklinker1.