Skip to content

Commit

Permalink
test: rely on chai type assertions instead of type casting
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurschreiber committed Oct 24, 2022
1 parent a1c6c1a commit 1103c79
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 41 deletions.
18 changes: 9 additions & 9 deletions test/integration/bulk-load-test.js
Expand Up @@ -129,7 +129,7 @@ describe('BulkLoad', function() {
it('fails if the column definition does not match the target table format', function(done) {
const bulkLoad = connection.newBulkLoad('#tmpTestTable2', (err, rowCount) => {
assert.instanceOf(err, RequestError, 'An error should have been thrown to indicate the incorrect table format.');
assert.strictEqual(/** @type {RequestError} */(err).message, 'An unknown error has occurred. This is likely because the schema of the BulkLoad does not match the schema of the table you are attempting to insert into.');
assert.strictEqual(err.message, 'An unknown error has occurred. This is likely because the schema of the BulkLoad does not match the schema of the table you are attempting to insert into.');

assert.isUndefined(rowCount);

Expand Down Expand Up @@ -376,7 +376,7 @@ describe('BulkLoad', function() {
].join(' ');

assert.instanceOf(err, RequestError);
assert.strictEqual(/** @type {RequestError} */(err).message, expectedMessage);
assert.strictEqual(err.message, expectedMessage);

assert.strictEqual(rowCount, 0);

Expand Down Expand Up @@ -486,7 +486,7 @@ describe('BulkLoad', function() {
it('does not insert any rows if `cancel` is called immediately after executing the bulk load', function(done) {
const bulkLoad = connection.newBulkLoad('#tmpTestTable5', { keepNulls: true }, (err, rowCount) => {
assert.instanceOf(err, RequestError);
assert.strictEqual(/** @type {RequestError} */(err).message, 'Canceled.');
assert.strictEqual(err.message, 'Canceled.');

assert.isUndefined(rowCount);

Expand Down Expand Up @@ -1154,7 +1154,7 @@ describe('BulkLoad', function() {
*/
function completeBulkLoad(err, rowCount) {
assert.instanceOf(err, RequestError);
assert.strictEqual(/** @type {RequestError} */(err).message, 'Canceled.');
assert.strictEqual(err.message, 'Canceled.');

assert.strictEqual(rowCount, 0);
startVerifyTableContent();
Expand Down Expand Up @@ -1199,7 +1199,7 @@ describe('BulkLoad', function() {

const bulkLoad = connection.newBulkLoad('#stream_test', (err, rowCount) => {
assert.instanceOf(err, RequestError);
assert.strictEqual(/** @type {RequestError} */(err).message, 'Canceled.');
assert.strictEqual(err.message, 'Canceled.');

assert.strictEqual(rowCount, 0);
});
Expand Down Expand Up @@ -1238,7 +1238,7 @@ describe('BulkLoad', function() {
it('cancels any bulk load that takes longer than the given timeout', function(done) {
const bulkLoad = connection.newBulkLoad('#tmpTestTable5', { keepNulls: true }, (err, rowCount) => {
assert.instanceOf(err, RequestError);
assert.strictEqual(/** @type {RequestError} */(err).message, 'Timeout: Request failed to complete in 10ms');
assert.strictEqual(err.message, 'Timeout: Request failed to complete in 10ms');

done();
});
Expand Down Expand Up @@ -1339,7 +1339,7 @@ describe('BulkLoad', function() {
*/
function completeBulkLoad(err, rowCount) {
assert.instanceOf(err, RequestError);
assert.strictEqual(/** @type {RequestError} */(err).message, 'Timeout: Request failed to complete in 200ms');
assert.strictEqual(err.message, 'Timeout: Request failed to complete in 200ms');

assert.strictEqual(rowCount, 0);

Expand Down Expand Up @@ -1526,7 +1526,7 @@ describe('BulkLoad', function() {
*/
function completeBulkLoad(err, rowCount) {
assert.instanceOf(err, TypeError);
assert.strictEqual(/** @type {TypeError} */(err).message, 'Invalid date.');
assert.strictEqual(err.message, 'Invalid date.');

done();
}
Expand All @@ -1546,7 +1546,7 @@ describe('BulkLoad', function() {
*/
function completeBulkLoad(err, rowCount) {
assert.instanceOf(err, TypeError);
assert.strictEqual(/** @type {TypeError} */(err).message, 'Invalid date.');
assert.strictEqual(err.message, 'Invalid date.');

assert.strictEqual(rowCount, 0);

Expand Down
15 changes: 8 additions & 7 deletions test/integration/connection-test.js
Expand Up @@ -290,7 +290,7 @@ describe('Initiate Connect Test', function() {
const connection = new Connection(config);
connection.connect(function(err) {
assert.instanceOf(err, ConnectionError);
assert.strictEqual(/** @type {ConnectionError} */(err).code, 'ESOCKET');
assert.strictEqual(err.code, 'ESOCKET');
});

connection.on('end', function() {
Expand Down Expand Up @@ -385,7 +385,7 @@ describe('Initiate Connect Test', function() {
conn.close();

assert.instanceOf(err, Error);
assert.strictEqual(/** @type {Error} */(err).message, 'Failed to connect to 192.0.2.1:1433 in 3000ms');
assert.strictEqual(err.message, 'Failed to connect to 192.0.2.1:1433 in 3000ms');

done();
});
Expand Down Expand Up @@ -867,7 +867,7 @@ describe('Insertion Tests', function() {

const request = new Request('select 8 as C1', function(err, rowCount) {
assert.instanceOf(err, RequestError);
assert.strictEqual(/** @type {RequestError} */(err).code, 'ECLOSE');
assert.strictEqual(err.code, 'ECLOSE');
});

const connection = new Connection(config);
Expand Down Expand Up @@ -1202,7 +1202,7 @@ describe('Insertion Tests', function() {

const request = new Request("select 1 as C1; waitfor delay '00:00:05'; select 2 as C2", (err, rowCount, rows) => {
assert.instanceOf(err, Error);
assert.strictEqual(/** @type {Error} */(err).message, 'Canceled.');
assert.strictEqual(err.message, 'Canceled.');

assert.isUndefined(rowCount);

Expand Down Expand Up @@ -1274,7 +1274,8 @@ describe('Insertion Tests', function() {
const request = new Request(
"select 1 as C1;waitfor delay '00:00:05';select 2 as C2",
function(err, rowCount, rows) {
assert.equal(/** @type {Error} */(err).message, 'Timeout: Request failed to complete in 1000ms');
assert.instanceOf(err, RequestError);
assert.equal(err.message, 'Timeout: Request failed to complete in 1000ms');

connection.close();
}
Expand Down Expand Up @@ -1369,9 +1370,9 @@ describe('Advanced Input Test', function() {
const config = getConfig();
config.options.enableAnsiNullDefault = false;

runSqlBatch(done, config, sql, function(/** @type {Error | null | undefined} */err) {
runSqlBatch(done, config, sql, function(err) {
assert.instanceOf(err, RequestError);
assert.strictEqual(/** @type {RequestError} */(err).number, 515);
assert.strictEqual(err.number, 515);
}); // Cannot insert the value NULL
});
});
Expand Down
32 changes: 14 additions & 18 deletions test/integration/errors-test.js
Expand Up @@ -70,8 +70,8 @@ describe('Errors Test', function() {
`;

execSql(done, sql, function(err) {
assert.ok(err instanceof RequestError);
assert.strictEqual(/** @type {RequestError} */(err).number, 2627);
assert.instanceOf(err, RequestError);
assert.strictEqual(err.number, 2627);
});
});

Expand All @@ -83,8 +83,8 @@ describe('Errors Test', function() {
`;

execSql(done, sql, function(err) {
assert.ok(err instanceof RequestError);
assert.strictEqual(/** @type {RequestError} */(err).number, 515);
assert.instanceOf(err, RequestError);
assert.strictEqual(err.number, 515);
});
});

Expand All @@ -94,8 +94,8 @@ describe('Errors Test', function() {
';

execSql(done, sql, function(err) {
assert.ok(err instanceof RequestError);
assert.strictEqual(/** @type {RequestError} */(err).number, 3701);
assert.instanceOf(err, RequestError);
assert.strictEqual(err.number, 3701);
});
});

Expand All @@ -108,24 +108,20 @@ describe('Errors Test', function() {
const connection = new Connection(config);

const execProc = new Request('#testExtendedErrorInfo', function(err) {
if (!err) {
assert.fail('Expected `err` to not be undefined');
}

const requestError = /** @type {RequestError} */(err);
assert.instanceOf(err, RequestError);

assert.strictEqual(requestError.number, 50000);
assert.strictEqual(requestError.state, 42);
assert.strictEqual(requestError.class, 14);
assert.strictEqual(err.number, 50000);
assert.strictEqual(err.state, 42);
assert.strictEqual(err.class, 14);

assert.exists(requestError.serverName);
assert.exists(requestError.procName);
assert.exists(err.serverName);
assert.exists(err.procName);

// The procedure name will actually be padded to 128 chars with underscores and
// some random hexadecimal digits.
assert.match(/** @type {string} */(requestError.procName), /^#testExtendedErrorInfo/);
assert.match(err.procName, /^#testExtendedErrorInfo/);

assert.strictEqual(requestError.lineNumber, 1);
assert.strictEqual(err.lineNumber, 1);

connection.close();
});
Expand Down
2 changes: 1 addition & 1 deletion test/integration/invalid-packet-stream-test.js
Expand Up @@ -60,7 +60,7 @@ describe('Connecting to a server that sends invalid packet data', function() {

connection.connect((err) => {
assert.instanceOf(err, ConnectionError);
assert.equal(/** @type {ConnectionError} */(err).message, 'Connection lost - Unable to process incoming packet');
assert.equal(err.message, 'Connection lost - Unable to process incoming packet');

done();
});
Expand Down
2 changes: 1 addition & 1 deletion test/integration/pause-resume-test.js
Expand Up @@ -119,7 +119,7 @@ describe('Pause-Resume Test', function() {

const request = new Request(sql, (error) => {
assert.instanceOf(error, RequestError);
assert.strictEqual(/** @type {RequestError} */(error).code, 'ECANCEL');
assert.strictEqual(error.code, 'ECANCEL');

next();
});
Expand Down
11 changes: 6 additions & 5 deletions test/unit/message-io-test.ts
Expand Up @@ -529,12 +529,13 @@ describe('MessageIO', function() {
// Use a cipher that causes an error immediately
ciphers: 'NULL'
}, 'localhost', true);
} catch (err: any) {
} catch (err: unknown) {
hadError = true;

assert.instanceOf(err, Error);
assert.strictEqual(err.code, 'ERR_SSL_NO_CIPHERS_AVAILABLE');
assert.strictEqual(err.reason, 'no ciphers available');

assert.strictEqual((err as any).code, 'ERR_SSL_NO_CIPHERS_AVAILABLE');
assert.strictEqual((err as any).reason, 'no ciphers available');
}

assert(hadError);
Expand Down Expand Up @@ -563,8 +564,8 @@ describe('MessageIO', function() {
hadError = true;

assert.instanceOf(err, Error);
assert.strictEqual(err.code, 'ERR_SSL_SSLV3_ALERT_HANDSHAKE_FAILURE');
assert.strictEqual(err.reason, 'sslv3 alert handshake failure');
assert.strictEqual((err as any).code, 'ERR_SSL_SSLV3_ALERT_HANDSHAKE_FAILURE');
assert.strictEqual((err as any).reason, 'sslv3 alert handshake failure');
}

assert(hadError);
Expand Down

0 comments on commit 1103c79

Please sign in to comment.