stoically / webextensions-api-mock

WebExtensions API as sinon stubs
Mozilla Public License 2.0
5 stars 2 forks source link

Unable to run on Karma since v1.0.0 #7

Open ueokande opened 4 years ago

ueokande commented 4 years ago

I cannot run tests with webextensions-api-mock on Karma since the update to v1.0.0. The karma outputs the following message:

Error: Cannot find module 'fs'

Karma is a test runner to run code on real browsers and unable to load Node.js modules. The fs and path depend on Node.js and running code on Karma cannot load them. webextensions-api-mock has read and updated the local schema since v1.0.0.

I tried to fix index.ts as the following:

diff --git a/src/index.ts b/src/index.ts
index 24f1e21..abef9a5 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -1,9 +1,8 @@
-import fs from 'fs';
-import path from 'path';
 import { SchemaNamespaces } from 'webextensions-schema';
 import { Update } from './update';
 import { BrowserGenerator } from './stub';
 import { BrowserMock } from './generated/types';
+import schema from './generated/schema.json';

 export class WebExtensionsApiMock {
   public namespaces!: SchemaNamespaces;
@@ -17,11 +16,7 @@ export class WebExtensionsApiMock {
   }

   readSchema(): void {
-    this.namespaces = JSON.parse(
-      fs
-        .readFileSync(path.join(__dirname, 'generated', 'schema.json'))
-        .toString()
-    ) as SchemaNamespaces;
+    this.namespaces = schema as any;
   }

   update = async (): Promise<void> => {

but still not works since fs and path modules are also used in update.ts and webextensions-schema.

Running tests on real browsers is necessary for me due to detecting the different behavior between browsers on the unit test.

stoically commented 4 years ago

Should be possible to directly bundle the json with typescript's resolveJsonModule for just using the mock, and dynamically importing fs/path when updating - I'll look into that.

I'm curious, wouldn't it be possible to use the actual `browser' if the tests are already running in a real browser?

ueokande commented 4 years ago

@stoically Karma runs test scripts inside a tab. The scripts in the test cannot access the actual browser.

To access the browser object in the test, a custom test runner that allows tests to run on extensions namespace might be required (I'm not sure that is possible)