Skip to content

Writing and running tests

Ioan Moldovan edited this page Nov 11, 2022 · 8 revisions

Tests require secrets, and are unfortunately not useful without the secrets. Without secrets, you can still run some smoke tests with npm run-script test, which will test for code style and other basic tests. Additionally, you can see test results when they run in CI. Once you join us and sign an NDA, you will be given the test secrets to be able to run tests conveniently.

Excluding linters and other small tests, there are 4 categories of tests:

  • unit tests which run in NodeJS
  • unit tests which run in browser
  • complex UI tests using mocked Google apis
  • complex UI tests using live Google apis, login, and mail.google.com (You need a test-secrets.json file that you may get after signing an NDA which goes in the flowcrypt-browser/test directory)

When a change to a class was made that is very specific, a unit test may be appropriate. When behavior of the extension was changed that involves the UI, then use mock UI test.

Don't try to run all tests at once locally - it would take too long and time out. Instead choose a particular test: find the test that you're interested in, and switch ava.default( to ava.default.only( on that test. Then only that test will run with the command below. Just don't commit the "only" into git.

To run test, you press F2 in vscode and choose "npm: test_local_chrome_consumer_mock". (or run npm run-script test_local_chrome_consumer_mock)

Resources: https://github.com/avajs/ava https://github.com/puppeteer/puppeteer

In the html code you'll see data-test="action-something" type of tags. These are identifiers for testing, so that tests keep working even if we change markup or ids/classes. Basically all test ids should start with "action", "container" or "input" (convention).

Tags identified this way are then used from test code with a @. Like:

await composePage.click('@input-body');

Don't forget your awaits in the tests (everything involving the browser is async). If you find yourself using puppeteer API directly, you are almost certainly doing something that we already have a convenience method for somewhere, probably in ControllableBase, ControllablePage, or BrowserHandle.

How to write a test for modified behavior by an OrgRule

See steps for one specific example here: https://github.com/FlowCrypt/flowcrypt-browser/issues/3781#issuecomment-883223754