Skip to content

test(node): Add Node SDK integration test structure. #4694

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

Merged
merged 19 commits into from
Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -421,3 +421,36 @@ jobs:
run: |
cd packages/utils
yarn test:package

job_node_integration_tests:
name: Node SDK Integration Tests (${{ matrix.node }})
needs: job_build
runs-on: ubuntu-latest
timeout-minutes: 10
continue-on-error: true
strategy:
matrix:
node: [10, 12, 14, 16]
steps:
- name: Check out current commit (${{ github.sha }})
uses: actions/checkout@v2
- name: Set up Node
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node }}
- name: Check dependency cache
uses: actions/cache@v2
with:
path: ${{ env.CACHED_DEPENDENCY_PATHS }}
key: ${{ needs.job_build.outputs.dependency_cache_key }}
- name: Check build cache
uses: actions/cache@v2
with:
path: ${{ env.CACHED_BUILD_PATHS }}
key: ${{ env.BUILD_CACHE_KEY }}
- name: Run integration tests
env:
NODE_VERSION: ${{ matrix.node }}
run: |
cd packages/node-integration-tests
yarn test
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"lint:eslint": "lerna run --parallel lint:eslint",
"prepublishOnly": "lerna run --stream --concurrency 1 prepublishOnly",
"postpublish": "make publish-docs && lerna run --stream --concurrency 1 postpublish",
"test": "lerna run --ignore @sentry-internal/browser-integration-tests --stream --concurrency 1 --sort test",
"test": "lerna run --ignore @sentry-internal/browser-integration-tests @sentry-internal/node-integration-tests --stream --concurrency 1 --sort test",
"test-ci": "ts-node ./scripts/test.ts"
},
"volta": {
Expand All @@ -43,6 +43,7 @@
"packages/minimal",
"packages/nextjs",
"packages/node",
"packages/node-integration-tests",
"packages/react",
"packages/serverless",
"packages/tracing",
Expand Down
10 changes: 10 additions & 0 deletions packages/node-integration-tests/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
env: {
node: true,
jest: true,
},
extends: ['../../.eslintrc.js'],
parserOptions: {
sourceType: 'module',
},
};
10 changes: 10 additions & 0 deletions packages/node-integration-tests/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const config = {
transform: {
'^.+\\.ts$': 'ts-jest',
},
testEnvironment: 'node',
testMatch: ['**/test.ts'],
moduleFileExtensions: ['js', 'ts'],
};

module.exports = config;
21 changes: 21 additions & 0 deletions packages/node-integration-tests/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "@sentry-internal/node-integration-tests",
"version": "6.18.2",
"license": "MIT",
"engines": {
"node": ">=10"
},
"private": true,
"scripts": {
"lint": "run-s lint:prettier lint:eslint",
"lint:eslint": "eslint . --cache --cache-location '../../eslintcache/' --format stylish",
"lint:prettier": "prettier --check \"{suites,utils}/**/*.ts\"",
"type-check": "tsc",
"test": "jest --detectOpenHandles --runInBand --forceExit"
},
"dependencies": {
"express": "^4.17.3",
"nock": "^13.1.0",
"portfinder": "^1.0.28"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import * as Sentry from '@sentry/node';

Sentry.init({
dsn: 'https://[email protected]/1337',
release: '1.0',
});

Sentry.captureException(new Error('Captured Error'));
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { assertSentryEvent, getEventRequest, runServer } from '../../../utils';

test('should send captureException', async () => {
const url = await runServer(__dirname);
const requestBody = await getEventRequest(url);

assertSentryEvent(requestBody, {
exception: {
values: [
{
type: 'Error',
value: 'Captured Error',
},
],
},
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import * as Sentry from '@sentry/node';

Sentry.init({
dsn: 'https://[email protected]/1337',
release: '1.0',
});

Sentry.captureMessage('Message');
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { assertSentryEvent, getEventRequest, runServer } from '../../../utils';

test('should send captureMessage', async () => {
const url = await runServer(__dirname);
const requestBody = await getEventRequest(url);

assertSentryEvent(requestBody, {
message: 'Message',
level: 'info',
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as Sentry from '@sentry/node';
import * as _ from '@sentry/tracing';

Sentry.init({
dsn: 'https://[email protected]/1337',
release: '1.0',
tracesSampleRate: 1.0,
});

const transaction = Sentry.startTransaction({ name: 'test_transaction_1' });

transaction.finish();
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { assertSentryTransaction, getEnvelopeRequest, runServer } from '../../../utils';

test.skip('should send a manually started transaction when @sentry/tracing is imported using namespace import.', async () => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Skipping this test for now, we can re-enable it with the PR to fix #4731.

const url = await runServer(__dirname);
const envelope = await getEnvelopeRequest(url);

expect(envelope).toHaveLength(3);

assertSentryTransaction(envelope[2], {
transaction: 'test_transaction_1',
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import '@sentry/tracing';

import * as Sentry from '@sentry/node';

Sentry.init({
dsn: 'https://[email protected]/1337',
release: '1.0',
tracesSampleRate: 1.0,
});

const transaction = Sentry.startTransaction({ name: 'test_transaction_1' });

transaction.finish();
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { assertSentryTransaction, getEnvelopeRequest, runServer } from '../../../utils';

test('should send a manually started transaction when @sentry/tracing is imported using unnamed import.', async () => {
const url = await runServer(__dirname);
const envelope = await getEnvelopeRequest(url);

expect(envelope).toHaveLength(3);

assertSentryTransaction(envelope[2], {
transaction: 'test_transaction_1',
});
});
9 changes: 9 additions & 0 deletions packages/node-integration-tests/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"esModuleInterop": true,
"types": ["jest", "node"]
},
"include": ["**/*.ts", "jest.config.js"],
"exclude": ["node_modules"]
}
5 changes: 5 additions & 0 deletions packages/node-integration-tests/utils/defaults/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import express from 'express';

const app = express();

export default app;
81 changes: 81 additions & 0 deletions packages/node-integration-tests/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
import { Express } from 'express';
import * as http from 'http';
import nock from 'nock';
import * as path from 'path';
import { getPortPromise } from 'portfinder';

const assertSentryEvent = (actual: Record<string, unknown>, expected: Record<string, unknown>): void => {
expect(actual).toMatchObject({
event_id: expect.any(String),
timestamp: expect.any(Number),
...expected,
});
};

const assertSentryTransaction = (actual: Record<string, unknown>, expected: Record<string, unknown>): void => {
expect(actual).toMatchObject({
event_id: expect.any(String),
timestamp: expect.any(Number),
start_timestamp: expect.any(Number),
spans: expect.any(Array),
type: 'transaction',
...expected,
});
};

const parseEnvelope = (body: string): Array<Record<string, unknown>> => {
return body.split('\n').map(e => JSON.parse(e));
};

const getEventRequest = async (url: string): Promise<Record<string, unknown>> => {
return new Promise(resolve => {
nock('https://dsn.ingest.sentry.io')
.post('/api/1337/store/', body => {
resolve(body);
return true;
})
.reply(200);

http.get(url);
});
};

const getEnvelopeRequest = async (url: string): Promise<Array<Record<string, unknown>>> => {
return new Promise(resolve => {
nock('https://dsn.ingest.sentry.io')
.post('/api/1337/envelope/', body => {
const envelope = parseEnvelope(body);
resolve(envelope);
return true;
})
.reply(200);

http.get(url);
});
};

async function runServer(testDir: string, serverPath?: string, scenarioPath?: string): Promise<string> {
const port = await getPortPromise();
const url = `http://localhost:${port}/test`;
const defaultServerPath = path.resolve(process.cwd(), 'utils', 'defaults', 'server');

await new Promise(resolve => {
// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-unsafe-member-access
const app = require(serverPath || defaultServerPath).default as Express;

app.get('/test', async () => {
require(scenarioPath || `${testDir}/scenario`);

setTimeout(() => server.close(), 500);
});

const server = app.listen(port, () => {
resolve();
});
});

return url;
}

export { assertSentryEvent, assertSentryTransaction, parseEnvelope, getEventRequest, getEnvelopeRequest, runServer };
Loading