Reputation: 1854
I have a Connection class used to make a connection to a AWS RDS Aurora database instance. The class works fine but I'm having trouble getting full unit test coverage. There is one piece that I'm not sure how to cover. It is mysql_clear_password: () => () => Buffer.from(this.options.password + '\0')
shown in the Connection class below. How can I cover that specific line? Is a refactor of the function necessary?
I have tried moving the Buffer
function to a separate function, but the coverage report still shows that original line as being uncovered
Connection class:
const mysql2 = require('mysql2/promise');
class Connection {
constructor(options = {}) {
this.options = options;
}
createPool () {
this.pool = mysql2.createPool({
host: this.options.host,
user: this.options.user,
database: 'my_db',
ssl: 'Amazon RDS',
password: this.options.password,
authPlugins: {
mysql_clear_password: () => () => Buffer.from(this.options.password + '\0')
}
});
}
}
module.exports = { Connection };
Here is what I have so far in my test:
const conns = require('../src/connection');
const sinon = require('sinon');
const mysql2 = require('mysql2/promise');
describe('connection', () => {
afterEach(() => {
sinon.restore();
});
test('Test creatPool function from connection class', async () => {
const options = {
host: 'testHost',
user: 'testUser',
password: 'testPassword'
};
const createPoolStub = sinon.stub(mysql2, 'createPool').returns(sinon.stub().returnsThis());
const conn = new conns.Connection(options);
await conn.createPool();
sinon.assert.calledOnce(createPoolStub);
});
});
Upvotes: 0
Views: 150
Reputation: 102527
Using stub.callsFake
method to make the stub(mysql2.createPool
) call the provided function when invoked. Then, you can get the mysql_clear_password
method from the provided function in your test case.
E.g.
connection.js
:
const mysql2 = require('mysql2/promise');
class Connection {
constructor(options = {}) {
this.options = options;
}
createPool() {
this.pool = mysql2.createPool({
host: this.options.host,
user: this.options.user,
database: 'my_db',
ssl: 'Amazon RDS',
password: this.options.password,
authPlugins: {
mysql_clear_password: () => () => Buffer.from(this.options.password + '\0'),
},
});
}
}
module.exports = { Connection };
connection.test.js
:
const mysql2 = require('mysql2/promise');
const conns = require('./connection');
const sinon = require('sinon');
const { expect } = require('chai');
describe('64300458', () => {
it('Test creatPool function from connection class', () => {
const options = {
host: 'testHost',
user: 'testUser',
password: 'testPassword',
};
let configRef;
const createPoolStub = sinon.stub(mysql2, 'createPool').callsFake((config) => {
configRef = config;
});
const conn = new conns.Connection(options);
conn.createPool();
sinon.assert.calledOnce(createPoolStub);
// test mysql_clear_password
const actual = configRef.authPlugins.mysql_clear_password()();
expect(actual).to.be.eql(Buffer.from('testPassword\0'));
createPoolStub.restore();
});
});
unit test result with coverage report:
64300458
✓ Test creatPool function from connection class
1 passing (11ms)
---------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
---------------|---------|----------|---------|---------|-------------------
All files | 100 | 0 | 100 | 100 |
connection.js | 100 | 0 | 100 | 100 | 4
---------------|---------|----------|---------|---------|-------------------
Upvotes: 1