From 4108104689626795b31bd5ea09b8753dba9bb6da Mon Sep 17 00:00:00 2001 From: Onur Temizkan Date: Fri, 25 Mar 2022 17:43:27 +0000 Subject: [PATCH 1/2] test(node): Document Node integration tests. --- packages/node-integration-tests/README.md | 50 +++++++++++++++++++ packages/node-integration-tests/package.json | 3 +- .../node-integration-tests/utils/index.ts | 45 +++++++++++++++++ 3 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 packages/node-integration-tests/README.md diff --git a/packages/node-integration-tests/README.md b/packages/node-integration-tests/README.md new file mode 100644 index 000000000000..1b74f04c249e --- /dev/null +++ b/packages/node-integration-tests/README.md @@ -0,0 +1,50 @@ +# Integration Tests for Sentry Node.JS SDK + +## Structure + +``` +suites/ +|---- public-api/ + |---- captureMessage/ + |---- test.ts [assertions] + |---- scenario.ts [Sentry initialization and test subject] + |---- customTest/ + |---- test.ts [assertions] + |---- scenario_1.ts [optional extra test scenario] + |---- scenario_2.ts [optional extra test scenario] + |---- server_with_mongo.ts [optional custom server] + |---- server_with_postgres.ts [optional custom server] +utils/ +|---- defaults/ + |---- server.ts [default Express server configuration] +``` + +The tests are grouped by their scopes, such as `public-api` or `tracing`. In every group of tests, there are multiple folders containing test scenarios and assertions. + +Tests run on Express servers (a server instance per test). By default, a simple server template inside `utils/defaults/server.ts` is used. Every server instance runs on a different port. + +A custom server configuration can be used, supplying a script that exports a valid express server instance as default. `runServer` utility function accepts an optional `serverPath` argument for this purpose. + +`scenario.ts` contains the initialization logic and the test subject. By default, `{TEST_DIR}/scenario.ts` is used, but `runServer` also accepts an optional `scenarioPath` argument for non-standard usage. + +`test.ts` is required for each test case, and contains the server runner logic, request interceptors for Sentry requests, and assertions. Test server, interceptors and assertions are all run on the same Jest thread. + +### Utilities + +`utils/` contains helpers and Sentry-specific assertions that can be used in (`test.ts`). + +## Running Tests Locally + +Tests can be run locally using the latest version of Chromium with: + +`yarn test` + +To run tests with Jest's watch mode: + +`yarn test:jest` + +To filter tests by their title: + +`yarn test -t "set different properties of a scope"` + +You can refer to [Jest documentation](https://jestjs.io/docs/cli) for other CLI options. diff --git a/packages/node-integration-tests/package.json b/packages/node-integration-tests/package.json index 86b963064166..e92ac0af8b51 100644 --- a/packages/node-integration-tests/package.json +++ b/packages/node-integration-tests/package.json @@ -11,7 +11,8 @@ "lint:eslint": "eslint . --cache --cache-location '../../eslintcache/' --format stylish", "lint:prettier": "prettier --check \"{suites,utils}/**/*.ts\"", "type-check": "tsc", - "test": "jest --detectOpenHandles --runInBand --forceExit" + "test": "jest --detectOpenHandles --runInBand --forceExit", + "test:watch": "yarn test --watch" }, "dependencies": { "express": "^4.17.3", diff --git a/packages/node-integration-tests/utils/index.ts b/packages/node-integration-tests/utils/index.ts index 8e31c2ee3514..373b8c8d2605 100644 --- a/packages/node-integration-tests/utils/index.ts +++ b/packages/node-integration-tests/utils/index.ts @@ -5,6 +5,12 @@ import nock from 'nock'; import * as path from 'path'; import { getPortPromise } from 'portfinder'; +/** + * Asserts against a Sentry Event ignoring non-deterministic properties + * + * @param {Record} actual + * @param {Record} expected + */ export const assertSentryEvent = (actual: Record, expected: Record): void => { expect(actual).toMatchObject({ event_id: expect.any(String), @@ -13,6 +19,12 @@ export const assertSentryEvent = (actual: Record, expected: Rec }); }; +/** + * Asserts against a Sentry Transaction ignoring non-deterministic properties + * + * @param {Record} actual + * @param {Record} expected + */ export const assertSentryTransaction = (actual: Record, expected: Record): void => { expect(actual).toMatchObject({ event_id: expect.any(String), @@ -24,10 +36,23 @@ export const assertSentryTransaction = (actual: Record, expecte }); }; +/** + * Parses response body containing an Envelope + * + * @param {string} body + * @return {*} {Array>} + */ export const parseEnvelope = (body: string): Array> => { return body.split('\n').map(e => JSON.parse(e)); }; +/** + * Intercepts and extracts multiple requests containing a Sentry Event + * + * @param {string} url + * @param {number} count + * @return {*} {Promise>>} + */ export const getMultipleEventRequests = async (url: string, count: number): Promise>> => { const events: Record[] = []; @@ -47,10 +72,22 @@ export const getMultipleEventRequests = async (url: string, count: number): Prom }); }; +/** + * Intercepts and extracts a single request containing a Sentry Event + * + * @param {string} url + * @return {*} {Promise>} + */ export const getEventRequest = async (url: string): Promise> => { return (await getMultipleEventRequests(url, 1))[0]; }; +/** + * Intercepts and extracts a request containing a Sentry Envelope + * + * @param {string} url + * @return {*} {Promise>>} + */ export const getEnvelopeRequest = async (url: string): Promise>> => { return new Promise(resolve => { nock('https://dsn.ingest.sentry.io') @@ -65,6 +102,14 @@ export const getEnvelopeRequest = async (url: string): Promise} + */ export async function runServer(testDir: string, serverPath?: string, scenarioPath?: string): Promise { const port = await getPortPromise(); const url = `http://localhost:${port}/test`; From ee364bbd3e7612dd7f997a01beea915e35c9edfb Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Fri, 25 Mar 2022 14:16:49 -0400 Subject: [PATCH 2/2] Update packages/node-integration-tests/README.md --- packages/node-integration-tests/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/node-integration-tests/README.md b/packages/node-integration-tests/README.md index 1b74f04c249e..a68b4a86c60e 100644 --- a/packages/node-integration-tests/README.md +++ b/packages/node-integration-tests/README.md @@ -35,7 +35,7 @@ A custom server configuration can be used, supplying a script that exports a val ## Running Tests Locally -Tests can be run locally using the latest version of Chromium with: +Tests can be run locally with: `yarn test`