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

Automated test for browser environment #219

Open
lenkan opened this issue Feb 15, 2024 · 2 comments
Open

Automated test for browser environment #219

lenkan opened this issue Feb 15, 2024 · 2 comments

Comments

@lenkan
Copy link
Collaborator

lenkan commented Feb 15, 2024

We need some sort of tests that verifies that the library is working properly in a browser environment. See PR #218 #214 for related issues.

There must be thousands of other examples out there with libraries that are supposed to work for both node.js and browser environments. But just to spark the discussion I have written down some thoughts below.

Let a bundler catch any node specific imports

One simple check that can be done is to try and bundle the entire library for a browser, then just discard the output. For example: (Note: this does not seem to catch if we are using the Buffer module)

npx esbuild src/index.ts --bundle --target=chrome58

Will fail because we have a dependency on the node-specific crypto module:

$ npx esbuild src/index.ts --bundle --target=chrome58
✘ [ERROR] Could not resolve "crypto"

    node_modules/ecdsa-secp256r1/index.js:1:23:
      1 │ const crypto = require('crypto')
        ╵                        ~~~~~~~~

  The package "crypto" wasn't found on the file system but is built into node. Are you trying to
  bundle for node? You can use "--platform=node" to do that, which will remove this error.

1 error

Configure eslint environment

It is possible to configure the eslint environment to catch usage of globals that are not available in both node and browser environments. https://eslint.org/docs/latest/use/configure/language-options. The setting is supposed to be shared-node-browser, but not sure if it works as I expect.

Running integration test suite in browser environment

It would be good to try and run the jest test suite in a browser environment. Not quite sure if this works, it could be that jest itself actually requires the node.js runtime environment.

Using an e2e test tool

Using an e2e test tool. For example

One thing to keep in mind here is that we really only want to execute scripts and make sure they run. Most of these tools I think focus testing the DOM behavior. So we might be able to get away with something much simpler. I am thinking some way to just include a script in a dummy HTML document and wrap in it a try catch, then load the html in a headless browser using puppeteer might be the easiest way.

@edeykholt
Copy link

I've used https://playwright.dev/dotnet/ for testing a Blazor WASM extension and interaction with javascript pages, including in an automated CI/CD pipeline (Azure DevOps). Should work on this stack, as well?

@lenkan
Copy link
Collaborator Author

lenkan commented Mar 4, 2024

This looks like a promising tool, especially the headless option https://vitest.dev/guide/browser.html

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

No branches or pull requests

2 participants