|
| 1 | +# E2E Tests |
| 2 | + |
| 3 | +E2E tests enable us to verify the behavior of the packages in this repository as if they were to be published in their current state. |
| 4 | + |
| 5 | +## How to run |
| 6 | + |
| 7 | +Prerequisites: Docker |
| 8 | + |
| 9 | +```bash |
| 10 | +yarn test:e2e |
| 11 | +``` |
| 12 | + |
| 13 | +## How they work |
| 14 | + |
| 15 | +Before running any tests we launch a fake test registry (in our case [Verdaccio](https://verdaccio.org/docs/e2e/)), we build our packages, pack them, and publish them to the fake registry. |
| 16 | +The fake registry is hosted in a Docker container, and the script to publish the packages is also run from within a container to ensure that the fake publishing happens with the same Node.js and npm versions as we're using in CI. |
| 17 | + |
| 18 | +After publishing our freshly built packages to the fake registry, the E2E test script will look for `test-recipe.json` files in test applications located in the `test-applications` folder. |
| 19 | +In this folder, we keep standalone test applications, that use our SDKs and can be used to verify their behavior. |
| 20 | +The `test-recipe.json` recipe files contain information on how to build the test applications and how to run tests on these applications. |
| 21 | + |
| 22 | +## How to set up a new test |
| 23 | + |
| 24 | +Test applications are completely standalone applications that can be used to verify our SDKs. |
| 25 | +To set one up, follow these commands: |
| 26 | + |
| 27 | +```sh |
| 28 | +cd packages/e2e-tests |
| 29 | + |
| 30 | +# Create a new test application folder |
| 31 | +mkdir test-applications/my-new-test-application # Name of the new folder doesn't technically matter but choose something meaningful |
| 32 | + |
| 33 | +# Create an npm configuration file that uses the fake test registry |
| 34 | +cat > test-applications/my-new-test-application/.npmrc << EOF |
| 35 | +@sentry:registry=http://localhost:4873 |
| 36 | +@sentry-internal:registry=http://localhost:4873 |
| 37 | +EOF |
| 38 | + |
| 39 | +# Add a gitignore that ignores lockfiles |
| 40 | +cat > test-applications/my-new-test-application/.gitignore << EOF |
| 41 | +yarn.lock |
| 42 | +package-lock.json |
| 43 | +EOF |
| 44 | + |
| 45 | +# Add a test recipe file to the test application |
| 46 | +touch test-applications/my-new-test-application/test-recipe.json |
| 47 | +``` |
| 48 | + |
| 49 | +To get you started with the recipe, you can copy the following into `test-recipe.json`: |
| 50 | + |
| 51 | +```json |
| 52 | +{ |
| 53 | + "$schema": "../../test-recipe-schema.json", |
| 54 | + "testApplicationName": "My New Test Application", |
| 55 | + "buildCommand": "yarn install --no-lockfile", |
| 56 | + "tests": [ |
| 57 | + { |
| 58 | + "testName": "My new test", |
| 59 | + "testCommand": "yarn test", |
| 60 | + "timeoutSeconds": 60 |
| 61 | + } |
| 62 | + ] |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +The `test-recipe.json` files follow a schema (`e2e-tests/test-recipe-schema.json`). Here is a basic explanation of the fields: |
| 67 | + |
| 68 | +- The `buildCommand` command runs only once before any of the tests and is supposed to build the test application. If this command returns a non-zero exit code, it counts as a failed test and the test application's tests are not run. |
| 69 | +- The `testCommand` command is supposed to run tests on the test application. If the configured command returns a non-zero exit code, it counts as a failed test. |
| 70 | +- A test timeout can be configured via `timeoutSeconds`, it defaults to `60`. |
| 71 | + |
| 72 | +**An important thing to note:** In the context of the `buildCommand` the fake test registry is available at `http://localhost:4873`. It hosts all of our packages as if they were to be published with the state of the current branch. |
| 73 | +This means we can install the packages from this registry via the `.npmrc` configuration as seen above. |
| 74 | +If you add Sentry dependencies to your test application, you should set the dependency versions set to `*`: |
| 75 | + |
| 76 | +```jsonc |
| 77 | +// package.json |
| 78 | +{ |
| 79 | + "name": "my-new-test-application", |
| 80 | + "version": "1.0.0", |
| 81 | + "private": true, |
| 82 | + "scripts": { |
| 83 | + "test": "echo \"Hello world!\"" |
| 84 | + }, |
| 85 | + "dependencies": { |
| 86 | + "@sentry/node": "*" |
| 87 | + } |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +All that is left for you to do now is to create a test app and run `yarn test:e2e`. |
0 commit comments