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

Make tests run in browsers #2

Closed
cinnamon-bun opened this issue Jun 3, 2020 · 2 comments
Closed

Make tests run in browsers #2

cinnamon-bun opened this issue Jun 3, 2020 · 2 comments

Comments

@cinnamon-bun
Copy link
Member

Get tooling in place to run all the tests in a browser environment.

@sgwilym
Copy link
Contributor

sgwilym commented Aug 24, 2020

I did a little bit of looking into this as earthstar-graphql also needs tests that run in a node-like environment and a browser-like one. In that repo I'm using jest, which lets you configure the environment used for testing: you can use jsdom to test how code would run in a browser. I'm going to try configuring it so that the same test suites run in both environments.

But that's not very useful here, is it? 😅 It seems like tap is made specifically for testing in node environments? I couldn't find docs about configuring it otherwise.

I will see how this shakes out in earthstar-graphql, and if there is ever any appetite to migrate tests to jest in this repo, I'm more than happy to help.

@cinnamon-bun
Copy link
Member Author

Yeah, turns out tap only works in node. But tape is almost identical and it can be browserified! So I kinda got this working but it's awkward.

(jsdom doesn't help here since it just emulates browser APIs and we need to test the actual browser javascript engine.)

Commit: 8a2bd98

Docs are in karma.conf.js, also pasted below.

Unfortunately this didn't detect #48 "detChoice problems in browser", it just worked. :/ In headless chrome, anyway.


How this works

Run npm run test-in-browser to do all these steps at once:

  1. npm run clean
  2. npm run build // typescript build
  3. npm run browserify-for-karma // make a bundle from one test file
  4. npm run karma // run karma headless browser test on the bundle

The browserify step currently can only process one of the *.test.js files.
It's hardcoded in package.json in the "browserify-for-karma" step.
Change it there to run different tests.
To fix this, I think browserify would need to make a separate bundle
for each of our *.test.js files.

ALSO

The tap module is not browserifiable. We tell browserify to
swap it for tape (see the browser section of package.json).
tape's API is almost identical except it doesn't have t.done(), it has t.end().
So write all the tests using t.end(), which works in both.

FUTURE IMPROVEMENTS

The npm packages karma-browserify and karma-typescript look promising
but I was not able to get them working.

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

No branches or pull requests

2 participants