Skip to content

Commit

Permalink
test(NODE-3236): separate unit from functional (#2991)
Browse files Browse the repository at this point in the history
  • Loading branch information
dariakp authored Oct 1, 2021
1 parent 7998956 commit 3aa953c
Show file tree
Hide file tree
Showing 26 changed files with 78 additions and 68 deletions.
2 changes: 2 additions & 0 deletions .evergreen/run-checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ echo "Typescript $(npx tsc -v)"
npx tsc --noEmit mongodb.ts34.d.ts
# check that resolution uses the downleveled types
echo "import * as mdb from '.'" > file.ts && npx tsc --noEmit --traceResolution file.ts | grep 'mongodb.ts34.d.ts' && rm file.ts

npm run check:unit
4 changes: 2 additions & 2 deletions .mocharc.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
],
"require": [
"ts-node/register",
"source-map-support/register"
"source-map-support/register",
"test/tools/runner/chai-addons.js"
],
"file": "test/tools/runner",
"ui": "test/tools/runner/metadata_ui.js",
"recursive": true,
"timeout": 60000,
Expand Down
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,23 +100,25 @@
"build:dts": "npm run build:ts && api-extractor run && rimraf 'lib/**/*.d.ts*' && downlevel-dts mongodb.d.ts mongodb.ts34.d.ts",
"build:docs": "typedoc",
"check:bench": "node test/benchmarks/driverBench",
"check:coverage": "nyc npm run check:test",
"check:coverage": "nyc npm run test:all",
"check:lint": "npm run build:dts && npm run check:dts && npm run check:eslint && npm run check:tsd",
"check:eslint": "eslint -v && eslint --max-warnings=0 --ext '.js,.ts' src test",
"check:tsd": "tsd --version && tsd",
"check:dts": "tsc --noEmit mongodb.d.ts && tsd",
"check:test": "mocha --recursive test/functional test/unit",
"check:test": "mocha --file test/tools/runner --recursive test/functional",
"check:unit": "mocha --recursive test/unit/",
"check:ts": "tsc -v && tsc --noEmit",
"check:atlas": "mocha --config \"test/manual/mocharc.json\" test/manual/atlas_connectivity.test.js",
"check:adl": "mocha test/manual/data_lake.test.js",
"check:adl": "mocha --file test/tools/runner test/manual/data_lake.test.js",
"check:ocsp": "mocha --config \"test/manual/mocharc.json\" test/manual/ocsp_support.test.js",
"check:kerberos": "mocha --config \"test/manual/mocharc.json\" test/manual/kerberos.test.js",
"check:tls": "mocha --config \"test/manual/mocharc.json\" test/manual/tls_support.test.js",
"check:ldap": "mocha --config \"test/manual/mocharc.json\" test/manual/ldap.test.js",
"check:csfle": "mocha test/functional/client_side_encryption",
"prepare": "node etc/prepare.js",
"release": "standard-version -i HISTORY.md",
"test": "npm run check:lint && npm run check:test"
"test": "npm run check:lint && npm run test:all",
"test:all": "npm run check:unit && npm run check:test"
},
"tsd": {
"directory": "test/types",
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const expect = require('chai').expect;
const mock = require('../../tools/mock');
const { ReplSetFixture } = require('../core/common');
const { ReplSetFixture } = require('../../unit/core/common');

const test = {};
describe('Sessions - client/unit', function () {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
58 changes: 57 additions & 1 deletion test/functional/write_concern.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ const generateTopologyTests = require('./spec-runner').generateTopologyTests;
const loadSpecTests = require('../spec').loadSpecTests;
const { withMonitoredClient } = require('./shared');

// WriteConcernError test requires
const { once } = require('events');

const mock = require('../tools/mock');
const { MongoClient } = require('../../src');
const { MongoClient, MongoServerError } = require('../../src');

describe('Write Concern', function () {
describe('spec tests', function () {
Expand Down Expand Up @@ -117,4 +120,57 @@ describe('Write Concern', function () {
return client.close();
});
});

// This test was moved from the WriteConcernError unit test file, there is probably a better place for it
describe('WriteConcernError', () => {
let client;

beforeEach(async function () {
client = this.configuration.newClient({ monitorCommands: true });
await client.connect();
});

afterEach(async () => {
if (client) {
await client.close();
client.removeAllListeners();
}
});

it('should always have the errInfo property accessible', {
metadata: { requires: { mongodb: '>=5.0.0' } },
async test() {
try {
await client.db().collection('wc_details').drop();
} catch {
// don't care
}

const collection = await client
.db()
.createCollection('wc_details', { validator: { x: { $type: 'string' } } });

const evCapture = once(client, 'commandSucceeded');

let errInfoFromError;
try {
await collection.insertOne({ x: /not a string/ });
expect.fail('The insert should fail the validation that x must be a string');
} catch (error) {
expect(error).to.be.instanceOf(MongoServerError);
expect(error).to.have.property('code', 121);
expect(error).to.have.property('errInfo').that.is.an('object');
errInfoFromError = error.errInfo;
}

const commandSucceededEvents = await evCapture;
expect(commandSucceededEvents).to.have.lengthOf(1);
const ev = commandSucceededEvents[0];
expect(ev).to.have.nested.property('reply.writeErrors[0].errInfo').that.is.an('object');

const errInfoFromEvent = ev.reply.writeErrors[0].errInfo;
expect(errInfoFromError).to.deep.equal(errInfoFromEvent);
}
});
});
});
8 changes: 8 additions & 0 deletions test/tools/runner/chai-addons.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

// configure chai
const chai = require('chai');
chai.use(require('sinon-chai'));
chai.use(require('chai-subset'));
chai.use(require('../../functional/spec-runner/matcher').default);
chai.config.truncateThreshold = 0;
7 changes: 1 addition & 6 deletions test/tools/runner/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,5 @@ require('./plugins/deferred');
require('./plugins/session_leak_checker');
require('./plugins/client_leak_checker');

// configure mocha and chai
// configure mocha
require('mocha-sinon');
const chai = require('chai');
chai.use(require('sinon-chai'));
chai.use(require('chai-subset'));
chai.use(require('../../functional/spec-runner/matcher').default);
chai.config.truncateThreshold = 0;
55 changes: 1 addition & 54 deletions test/unit/core/write_concern_error.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
const { Topology } = require('../../../src/sdam/topology');
const mock = require('../../tools/mock');
const { ReplSetFixture } = require('./common');
const { MongoServerError, MongoWriteConcernError } = require('../../../src/error');
const { MongoWriteConcernError } = require('../../../src/error');
const { expect } = require('chai');
const { ns } = require('../../../src/utils');
const { once } = require('events');

describe('WriteConcernError', function () {
let test;
Expand Down Expand Up @@ -160,56 +159,4 @@ describe('WriteConcernError', function () {
});
});
});

describe('errInfo property', () => {
let client;

beforeEach(async function () {
client = this.configuration.newClient({ monitorCommands: true });
await client.connect();
});

afterEach(async () => {
if (client) {
await client.close();
client.removeAllListeners();
}
});

it('should always be accessible', {
metadata: { requires: { mongodb: '>=5.0.0' } },
async test() {
try {
await client.db().collection('wc_details').drop();
} catch {
// don't care
}

const collection = await client
.db()
.createCollection('wc_details', { validator: { x: { $type: 'string' } } });

const evCapture = once(client, 'commandSucceeded');

let errInfoFromError;
try {
await collection.insertOne({ x: /not a string/ });
expect.fail('The insert should fail the validation that x must be a string');
} catch (error) {
expect(error).to.be.instanceOf(MongoServerError);
expect(error).to.have.property('code', 121);
expect(error).to.have.property('errInfo').that.is.an('object');
errInfoFromError = error.errInfo;
}

const commandSucceededEvents = await evCapture;
expect(commandSucceededEvents).to.have.lengthOf(1);
const ev = commandSucceededEvents[0];
expect(ev).to.have.nested.property('reply.writeErrors[0].errInfo').that.is.an('object');

const errInfoFromEvent = ev.reply.writeErrors[0].errInfo;
expect(errInfoFromError).to.deep.equal(errInfoFromEvent);
}
});
});
});

0 comments on commit 3aa953c

Please sign in to comment.