Skip to content

Commit b11e61e

Browse files
authored
test(instrumentation-mongoose): add Mongoose guard to skip tests (#3036)
1 parent 143a61e commit b11e61e

File tree

6 files changed

+131
-22
lines changed

6 files changed

+131
-22
lines changed

.github/workflows/test-all-versions.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ jobs:
152152
POSTGRES_USER: postgres
153153
POSTGRES_PASSWORD: postgres
154154
RUN_MONGODB_TESTS: 1
155+
RUN_MONGOOSE_TESTS: 1
155156
RUN_MSSQL_TESTS: 1
156157
RUN_MYSQL_TESTS: 1
157158
RUN_ORACLEDB_TESTS: 1

.github/workflows/test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ jobs:
151151
RUN_CASSANDRA_TESTS: 1
152152
RUN_MEMCACHED_TESTS: 1
153153
RUN_MONGODB_TESTS: 1
154+
RUN_MONGOOSE_TESTS: 1
154155
RUN_MYSQL_TESTS: 1
155156
RUN_MSSQL_TESTS: 1
156157
RUN_ORACLEDB_TESTS: 1

packages/instrumentation-mongoose/test/mongoose-common.test.ts

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,20 @@ import { DB_NAME, MONGO_URI } from './config';
3737

3838
// Please run `npm run test-services:start` before
3939
describe('mongoose instrumentation [common]', () => {
40-
before(async () => {
40+
// For these tests, MongoDB must be running. Add RUN_MONGOOSE_TESTS to run
41+
// these tests.
42+
const RUN_MONGOOSE_TESTS = process.env.RUN_MONGOOSE_TESTS;
43+
let shouldTest = true;
44+
45+
before(async function () {
46+
// Check if tests should run
47+
if (!RUN_MONGOOSE_TESTS) {
48+
console.log('Skipping mongoose tests. Set RUN_MONGOOSE_TESTS env to run');
49+
shouldTest = false;
50+
return;
51+
}
52+
53+
// Try to connect to MongoDB
4154
try {
4255
await mongoose.connect(MONGO_URI, {
4356
useNewUrlParser: true,
@@ -51,18 +64,37 @@ describe('mongoose instrumentation [common]', () => {
5164
// the following check tries both signatures, so test-all-versions
5265
// can run against both versions.
5366
if (err?.name === 'MongoParseError') {
54-
await mongoose.connect(MONGO_URI, {
55-
dbName: DB_NAME,
56-
}); // TODO: amir - document older mongoose support
67+
try {
68+
await mongoose.connect(MONGO_URI, {
69+
dbName: DB_NAME,
70+
}); // TODO: amir - document older mongoose support
71+
} catch (innerErr: any) {
72+
console.log(
73+
'Skipping mongoose tests. Connection failed:',
74+
innerErr.message
75+
);
76+
shouldTest = false;
77+
}
78+
} else {
79+
console.log('Skipping mongoose tests. Connection failed:', err.message);
80+
shouldTest = false;
5781
}
5882
}
5983
});
6084

6185
after(async () => {
62-
await mongoose.connection.close();
86+
if (shouldTest) {
87+
await mongoose.connection.close();
88+
}
6389
});
6490

65-
beforeEach(async () => {
91+
beforeEach(async function () {
92+
// Skipping all tests in beforeEach() is a workaround. Mocha does not work
93+
// properly when skipping tests in before() on nested describe() calls.
94+
// https://github.com/mochajs/mocha/issues/2819
95+
if (!shouldTest) {
96+
this.skip();
97+
}
6698
instrumentation.disable();
6799
instrumentation.setConfig({
68100
dbStatementSerializer: (_operation: string, payload) => {
@@ -77,7 +109,9 @@ describe('mongoose instrumentation [common]', () => {
77109

78110
afterEach(async () => {
79111
instrumentation.disable();
80-
await User.collection.drop().catch();
112+
if (shouldTest) {
113+
await User.collection.drop().catch();
114+
}
81115
});
82116

83117
describe('instrumenting save operation', async () => {

packages/instrumentation-mongoose/test/mongoose-v5-v6.test.ts

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,20 @@ import { DB_NAME, MONGO_URI } from './config';
3737

3838
// Please run `npm run test-services:start` before
3939
describe('mongoose instrumentation [v5/v6]', () => {
40-
before(async () => {
40+
// For these tests, MongoDB must be running. Add RUN_MONGOOSE_TESTS to run
41+
// these tests.
42+
const RUN_MONGOOSE_TESTS = process.env.RUN_MONGOOSE_TESTS;
43+
let shouldTest = true;
44+
45+
before(async function () {
46+
// Check if tests should run
47+
if (!RUN_MONGOOSE_TESTS) {
48+
console.log('Skipping mongoose tests. Set RUN_MONGOOSE_TESTS env to run');
49+
shouldTest = false;
50+
return;
51+
}
52+
53+
// Try to connect to MongoDB
4154
try {
4255
await mongoose.connect(MONGO_URI, {
4356
useNewUrlParser: true,
@@ -51,18 +64,37 @@ describe('mongoose instrumentation [v5/v6]', () => {
5164
// the following check tries both signatures, so test-all-versions
5265
// can run against both versions.
5366
if (err?.name === 'MongoParseError') {
54-
await mongoose.connect(MONGO_URI, {
55-
dbName: DB_NAME,
56-
}); // TODO: amir - document older mongoose support
67+
try {
68+
await mongoose.connect(MONGO_URI, {
69+
dbName: DB_NAME,
70+
}); // TODO: amir - document older mongoose support
71+
} catch (innerErr: any) {
72+
console.log(
73+
'Skipping mongoose tests. Connection failed:',
74+
innerErr.message
75+
);
76+
shouldTest = false;
77+
}
78+
} else {
79+
console.log('Skipping mongoose tests. Connection failed:', err.message);
80+
shouldTest = false;
5781
}
5882
}
5983
});
6084

6185
after(async () => {
62-
await mongoose.connection.close();
86+
if (shouldTest) {
87+
await mongoose.connection.close();
88+
}
6389
});
6490

65-
beforeEach(async () => {
91+
beforeEach(async function () {
92+
// Skipping all tests in beforeEach() is a workaround. Mocha does not work
93+
// properly when skipping tests in before() on nested describe() calls.
94+
// https://github.com/mochajs/mocha/issues/2819
95+
if (!shouldTest) {
96+
this.skip();
97+
}
6698
instrumentation.disable();
6799
instrumentation.setConfig({
68100
dbStatementSerializer: (_operation: string, payload) => {
@@ -77,7 +109,9 @@ describe('mongoose instrumentation [v5/v6]', () => {
77109

78110
afterEach(async () => {
79111
instrumentation.disable();
80-
await User.collection.drop().catch();
112+
if (shouldTest) {
113+
await User.collection.drop().catch();
114+
}
81115
});
82116

83117
describe('when save call has callback', async () => {
@@ -324,7 +358,10 @@ describe('mongoose instrumentation [v5/v6]', () => {
324358

325359
describe('responseHook', () => {
326360
const RESPONSE = 'db.response';
327-
beforeEach(() => {
361+
beforeEach(function () {
362+
if (!shouldTest) {
363+
this.skip();
364+
}
328365
instrumentation.disable();
329366
instrumentation.setConfig({
330367
responseHook: (span, responseInfo) =>

packages/instrumentation-mongoose/test/mongoose-v7-v8.test.ts

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,20 @@ import { DB_NAME, MONGO_URI } from './config';
3434

3535
// Please run `npm run test-services:start` before
3636
describe('mongoose instrumentation [v7/v8]', () => {
37-
before(async () => {
37+
// For these tests, MongoDB must be running. Add RUN_MONGOOSE_TESTS to run
38+
// these tests.
39+
const RUN_MONGOOSE_TESTS = process.env.RUN_MONGOOSE_TESTS;
40+
let shouldTest = true;
41+
42+
before(async function () {
43+
// Check if tests should run
44+
if (!RUN_MONGOOSE_TESTS) {
45+
console.log('Skipping mongoose tests. Set RUN_MONGOOSE_TESTS env to run');
46+
shouldTest = false;
47+
return;
48+
}
49+
50+
// Try to connect to MongoDB
3851
try {
3952
await mongoose.connect(MONGO_URI, {
4053
useNewUrlParser: true,
@@ -48,18 +61,37 @@ describe('mongoose instrumentation [v7/v8]', () => {
4861
// the following check tries both signatures, so test-all-versions
4962
// can run against both versions.
5063
if (err?.name === 'MongoParseError') {
51-
await mongoose.connect(MONGO_URI, {
52-
dbName: DB_NAME,
53-
}); // TODO: amir - document older mongoose support
64+
try {
65+
await mongoose.connect(MONGO_URI, {
66+
dbName: DB_NAME,
67+
}); // TODO: amir - document older mongoose support
68+
} catch (innerErr: any) {
69+
console.log(
70+
'Skipping mongoose tests. Connection failed:',
71+
innerErr.message
72+
);
73+
shouldTest = false;
74+
}
75+
} else {
76+
console.log('Skipping mongoose tests. Connection failed:', err.message);
77+
shouldTest = false;
5478
}
5579
}
5680
});
5781

5882
after(async () => {
59-
await mongoose.connection.close();
83+
if (shouldTest) {
84+
await mongoose.connection.close();
85+
}
6086
});
6187

62-
beforeEach(async () => {
88+
beforeEach(async function () {
89+
// Skipping all tests in beforeEach() is a workaround. Mocha does not work
90+
// properly when skipping tests in before() on nested describe() calls.
91+
// https://github.com/mochajs/mocha/issues/2819
92+
if (!shouldTest) {
93+
this.skip();
94+
}
6395
instrumentation.disable();
6496
instrumentation.setConfig({
6597
dbStatementSerializer: (_operation: string, payload) => {
@@ -74,7 +106,9 @@ describe('mongoose instrumentation [v7/v8]', () => {
74106

75107
afterEach(async () => {
76108
instrumentation.disable();
77-
await User.collection.drop().catch();
109+
if (shouldTest) {
110+
await User.collection.drop().catch();
111+
}
78112
});
79113

80114
it('instrumenting findOneAndUpdate operation', async () => {

test/test-services.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ MONGODB_DB=opentelemetry-tests
1010
MONGODB_HOST=127.0.0.1
1111
MONGODB_PORT=27017
1212

13+
RUN_MONGOOSE_TESTS=1
14+
1315
RUN_MSSQL_TESTS=1
1416
MSSQL_PASSWORD=mssql_passw0rd
1517

0 commit comments

Comments
 (0)