Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Unable to run on Karma since v1.0.0 #7

Open
ueokande opened this issue Feb 9, 2020 · 2 comments
Open

Unable to run on Karma since v1.0.0 #7

ueokande opened this issue Feb 9, 2020 · 2 comments

Comments

@ueokande
Copy link

ueokande commented Feb 9, 2020

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
Copy link
Owner

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
Copy link
Author

@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)

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants