* revert all commits to before Typescript migration * update compat workflow to match latest deps (#2335) * update compat workflow to match latest deps * attempt to debug * attempt to debug * remove debugging code * typo * update deps to match desktop (#2340) * fix: don't run lint with `--fix` on push tests (#2273) * fix: don't run lint with `--fix` on push tests * npx Co-authored-by: maniarathi <mani.arathi@gmail.com> Co-authored-by: Madison Dunitz <madison.dunitz@chanzuckerberg.com> * rename X_approx_distribution to X_approximate_distribution (#2337) * Correctly handle non-finite numbers in heuristic determination of X distribution (#2342) * handle non-finites explicitly * improve and test edge case handling for distribution estimation * revert debugging changes * code readability * clean up type inferencing (#2332) * unit tests for 64 bit conversion * clean up type handling * type inference tests * more type inference fixes * use schema to determine user intent for data typing * stop using deprecated API * fbs type encoding test * add missing test * add more tests * correctly infer X type for CXG adaptor * lint * fix typo * ts migration * cleanup from PR review * lint * PR review changes * remove unused packages from client (#2359) * remove unused packages from client * add missing peer dep * fix: disable FE auth testing on compatibility tests (#2377) * update: release process (#2277) Co-authored-by: maniarathi <mani.arathi@gmail.com> * fix: remove spaces in param setup (#2380) * delete deploy workflow (#2396) * undo reformatting which now does not pass lint * fix snapshots which changed due to npm dep changes * add missing quoting to snapshot * another snapshot typo fix * TS Revert (2) - replay PR #2347 and #2354 (#2403) * replay edits from PR 2347 * TS Revert (3) - replay edits in PR #2327 (#2404) * replay edits in PR 2327 * TS Revert (4) - replay PR #2355 (#2405) * replay edits in PR 2355 * add additional babel config * reformat with new prettier config Co-authored-by: Severiano Badajoz <sbadajoz@chanzuckerberg.com> Co-authored-by: maniarathi <mani.arathi@gmail.com> Co-authored-by: Madison Dunitz <madison.dunitz@chanzuckerberg.com>
6.5 KiB
End to end testing
Thesis
End to end (e2e) tests, when done correctly, can surface errors and regressions in the application before it is released to production. The tests are run against a version of the web app that is running the full stack. By checking the fully functional app, you are able to surface issues with logic and data at every level of the stack (you still have to figure out which level that error is occurring at). You are also able to test how features that are added affect the whole application.
The major problem most developers have with e2e tests are that the tests can be flaky and thus not trusted. Failing tests wind up being disabled instead of fixed. To guard against that we need to establish principals about what features to test and have guidelines for writing tests to ensure that they are as robust as possible. By ensuring each test has a high value and is unlikely to fail when it shouldn't it increases the motivation to fix the test as opposed to deleting it.
Additionally, we try to combat flaky tests by re-running failed tests two more times before declaring a test is truly failing. Even though this mediation doesn't solve the root cause of the flakiness, it does help decrease the false positive cases caused by the flakiness.
Choosing what to test
- Common actions - Features and actions that are highly used will cause more impact if they break
- High value - Features that may be rarely used, but cause a major business impact if they break
- Depend on the whole stack and not just the FE - We want to test the backend and data as well as the FE. Features that only depend on the javascript code should be tested in unit tests instead.
- Feature is in a stable state - If the feature is in active development, it's likely that the test will have to be constantly updated. Wait until it is in a steady state before adding an e2e test.
- Feature is a likely target of regressions - If a feature has caused regressions in the past, it's good to add some monitoring to it.
Guidelines for writing tests
-
Use data attributes as selectors instead of positional, classname, or id selectors. Add
data-testidordata-testclassattributes to components that are inputs and outputs. The advantages are 1) You can move the component around and even change its html element type and it will not break the test and 2) It is self documenting what the attribute is used by as opposed to a class name or id that could be used by CSS or something else. -
Use the helper functions in
puppeteerUtils.jsandcellxgeneActions.jsfor performing common actions. Puppeteer operates at a low level and these abstract it to a level of user actions. -
We use
expect-puppeteer's API to avoid directly using Puppeteer's API where possible. So please only resort to Puppeteer's API if you cannot achieve an interaction you need viaexpect-puppeteer.
For actions that don't meet these guidelines, you can create a test in feature.test.js that does not run on PRs so if the feature breaks it doesn't block anyone.
Where/How
Where: client/__tests__/e2e/e2e.test.js
How: npm run e2e or make e2e
Tests are written using the Jest testing framework. Browser automation is handled by Puppeteer. Puppeteer provides an API to control Chrome over the DevTools Protocol. This allows us to write code that mimics user actions and check page state.
Tests are running on CI with every PR, they can also be run locally (see developer guidelines).
The cellxgene instance is running with the pbmc3k dataset. Tests for additional datasets can be added, the data.js file contains the values for inputs and outputs expected for different datasets. Choose values that make sense for that dataset.
Setting up e2e tests locally
How to write a new test
-
Ensure that your new test meets the guidelines above for high-value robust user actions to test.
-
Clearly define the series of user actions and outputs you want to test
-
Add
testid(and/ortestclassif element is not unique) data attributes to inputs and outputs you want to interact with. E.g.,<div data-testid="test-id" />or<div data-testclass="test-class" /> -
Create a new test block possible nested in a describe block if you want to add more than one related test client/__tests__/e2e/e2e.test.js
-
Write the test code.
-
Run tests locally to ensure they pass.
Debugging Tips
-
Mystery bugs are often caused by a missing
asyncorawait. Double check every function and every line (twice!). -
Use
ndbfor debugging: Put a javascript breakpoint in by adding this linedebuggerto the test at the point which you want to pause execution. And then runndb npm run e2eorndb make e2e.NOTE: You will need to global install
ndbvianpm install -g ndb, which provides an improved debugging experience for Node.js, enabled by Chrome DevTools -
Limit the tests that are run by adding
.onlyafterdescribeortestto limit execution to just that block of tests. (Remember to take the.onlyout before you commit!) -
Avoid using
beforeEach() => {}to set up test environment for each test, since Jest/Jasmine doesn't fail a test when a set up step fails, and we will end up getting obscure errors and don't know why a test fails. Another reason is for readability as explained here -
Use
takeScreenshot()to timestamp and store screenshots in the already git ignored folderclient/__tests__/screenshots/, so you don't accidentally check in your screenshot in your commit
Config Files
-
jest-puppeteer.config.js is for configuring Puppeteer's launch options as specified here. Note: The link is to the doc for v3.1.0
-
e2eJestConfig.json is for configuring
jestwhen running e2e tests. For example, inpackage.jsonwe have:{ "e2e": "jest --config __tests__/e2e/e2eJestConfig.json e2e/e2e.test.js", }which tells
jestto usee2eJestConfig.jsonas the config file to run e2e test filee2e.test.js -
puppeteer.setup.js is for configuring
jest,browser, andpageobjects at runtime