Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

test: add test for browser compatibility #721

Closed
wants to merge 1 commit into from

Conversation

Dhaiwat10
Copy link
Member

@Dhaiwat10 Dhaiwat10 commented Jan 9, 2023

  • Related to (but does not close) Create test to browser compatibility #284
  • This PR adds one unit test to the fuels package that scans the browser bundles of all the child packages exported by the umbrella fuels package for any references to Node.js built-in modules that may cause the package to break in a browser environment.
  • We have discussed that we should try running all of our tests in a browser environment on top of our current setup which is a Node.js environment, but I don't think we have yet decided on how exactly to go about that. I think this test might be an alternative approach to that same problem, or an added layer of safety in terms of browser compatibility - depends on how we look at it.

Keeping this a draft for now. Just an idea.

@github-actions
Copy link
Contributor

github-actions bot commented Jan 9, 2023

Coverage report

St.
Category Percentage Covered / Total
🟢 Statements 93.82% 4207/4484
🟡 Branches 79.63% 727/913
🟢 Functions 90.78% 827/911
🟢 Lines 93.67% 4043/4316

Test suite run success

674 tests passing in 95 suites.

Report generated by 🧪jest coverage report action from 854db46

Comment on lines +14 to +33
const requireRegex = /(?:^|[^.])\brequire\s*\(\s*(["'])(.+?)\1\s*\)/g;
let match;
while ((match = requireRegex.exec(formattedBundleContent))) {
const moduleName = match[2];
listOfModules.add(moduleName);
}

const importRegex = /import\s+(?:.+\s+from\s+)?["'](.+)["'];?/g;
let importMatch;
while ((importMatch = importRegex.exec(formattedBundleContent))) {
const moduleName = importMatch[1];
listOfModules.add(moduleName);
}

const exportRegex = /export\s+(?:.+\s+from\s+)?["'](.+)["'];?/g;
let exportMatch;
while ((exportMatch = exportRegex.exec(formattedBundleContent))) {
const moduleName = exportMatch[1];
listOfModules.add(moduleName);
}
Copy link
Member Author

@Dhaiwat10 Dhaiwat10 Jan 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The approach that I have gone with is scanning the bundled files for require and import statements to check for any references to Node.js built-in modules that are only available in a Node environment and not in a browser. I am not sure if this is the absolute best way to go about this, but I have checked that it does get the job done.

The moment I tried importing something from a module like fs, used it in a function and exported that function from one of the child packages, the test failed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rationale is valid, but it'd be hard to have certainty about the complete required stack, since we don't control all the libraries in use. For example, we can import an NPM package that uses some native module, which might be hard or impossible to detect, especially if we consider multiple nested dependencies.

In the end, I believe a declarative approach will suit us better.

One inspiration is how jest-runner-groups does it using manual tags.

@Dhaiwat10
Copy link
Member Author

Closing this in favor of #728

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

Successfully merging this pull request may close these issues.

2 participants