Skip to content

fix(test): Fix MongoDB integration tests on CI. #4810

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 7 commits into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion packages/node-integration-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"express": "^4.17.3",
"mysql": "^2.18.1",
"mongodb": "^3.7.3",
"mongodb-memory-server": "^8.4.1",
"mongodb-memory-server": "^7.6.3",
"nock": "^13.1.0",
"pg": "^8.7.3",
"portfinder": "^1.0.28"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
import '@sentry/tracing';

import * as Sentry from '@sentry/node';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,81 +1,91 @@
import { MongoMemoryServer } from 'mongodb-memory-server';

import { assertSentryTransaction, getEnvelopeRequest, runServer } from '../../../../utils';
import { assertSentryTransaction, conditionalTest, getEnvelopeRequest, runServer } from '../../../../utils';

test('should auto-instrument `mongodb` package.', async () => {
const mongoServer = await MongoMemoryServer.create();
process.env.MONGO_URL = mongoServer.getUri();
conditionalTest({ min: 12 })('MongoDB Test', () => {
let mongoServer: MongoMemoryServer;

const url = await runServer(__dirname);
beforeAll(async () => {
mongoServer = await MongoMemoryServer.create();
process.env.MONGO_URL = mongoServer.getUri();
}, 30000);

const envelope = await getEnvelopeRequest(url);
afterAll(async () => {
await mongoServer.stop();
});

test('should auto-instrument `mongodb` package.', async () => {
const url = await runServer(__dirname);

const envelope = await getEnvelopeRequest(url);

expect(envelope).toHaveLength(3);
expect(envelope).toHaveLength(3);

assertSentryTransaction(envelope[2], {
transaction: 'Test Transaction',
spans: [
{
data: {
collectionName: 'movies',
dbName: 'admin',
namespace: 'admin.movies',
doc: '{"title":"Rick and Morty"}',
assertSentryTransaction(envelope[2], {
transaction: 'Test Transaction',
spans: [
{
data: {
collectionName: 'movies',
dbName: 'admin',
namespace: 'admin.movies',
doc: '{"title":"Rick and Morty"}',
},
description: 'insertOne',
op: 'db',
},
description: 'insertOne',
op: 'db',
},
{
data: {
collectionName: 'movies',
dbName: 'admin',
namespace: 'admin.movies',
query: '{"title":"Back to the Future"}',
{
data: {
collectionName: 'movies',
dbName: 'admin',
namespace: 'admin.movies',
query: '{"title":"Back to the Future"}',
},
description: 'findOne',
op: 'db',
},
description: 'findOne',
op: 'db',
},
{
data: {
collectionName: 'movies',
dbName: 'admin',
namespace: 'admin.movies',
query: '{"title":"Back to the Future"}',
{
data: {
collectionName: 'movies',
dbName: 'admin',
namespace: 'admin.movies',
query: '{"title":"Back to the Future"}',
},
description: 'find',
op: 'db',
},
description: 'find',
op: 'db',
},
{
data: {
collectionName: 'movies',
dbName: 'admin',
namespace: 'admin.movies',
filter: '{"title":"Back to the Future"}',
update: '{"$set":{"title":"South Park"}}',
{
data: {
collectionName: 'movies',
dbName: 'admin',
namespace: 'admin.movies',
filter: '{"title":"Back to the Future"}',
update: '{"$set":{"title":"South Park"}}',
},
description: 'updateOne',
op: 'db',
},
description: 'updateOne',
op: 'db',
},
{
data: {
collectionName: 'movies',
dbName: 'admin',
namespace: 'admin.movies',
query: '{"title":"South Park"}',
{
data: {
collectionName: 'movies',
dbName: 'admin',
namespace: 'admin.movies',
query: '{"title":"South Park"}',
},
description: 'findOne',
op: 'db',
},
description: 'findOne',
op: 'db',
},
{
data: {
collectionName: 'movies',
dbName: 'admin',
namespace: 'admin.movies',
query: '{"title":"South Park"}',
{
data: {
collectionName: 'movies',
dbName: 'admin',
namespace: 'admin.movies',
query: '{"title":"South Park"}',
},
description: 'find',
op: 'db',
},
description: 'find',
op: 'db',
},
],
],
});
});
});
18 changes: 18 additions & 0 deletions packages/node-integration-tests/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
import { parseSemver } from '@sentry/utils';
import { Express } from 'express';
import * as http from 'http';
import nock from 'nock';
import * as path from 'path';
import { getPortPromise } from 'portfinder';

/**
* Returns`describe` or `describe.skip` depending on allowed major versions of Node.
*
* @param {{ min?: number; max?: number }} allowedVersion
* @return {*} {jest.Describe}
*/
export const conditionalTest = (allowedVersion: { min?: number; max?: number }): jest.Describe => {
const NODE_VERSION = parseSemver(process.versions.node).major;
if (!NODE_VERSION) {
return describe.skip;
}

return NODE_VERSION < (allowedVersion.min || -Infinity) || NODE_VERSION > (allowedVersion.max || Infinity)
? describe.skip
: describe;
};

/**
* Asserts against a Sentry Event ignoring non-deterministic properties
*
Expand Down
Loading