Created
August 8, 2019 07:29
-
-
Save jennifer-shehane/f498ce68b46425583fa72c8d945fe7bb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function() { | |
Cypress.Commands.add('sqlServer', (query) => { | |
if(!query) { | |
throw new Error('Query must be set'); | |
} | |
cy.task('sqlServer:execute', query).then(response => { | |
let result = []; | |
const flatten = r => Array.isArray(r) && r.length === 1 ? flatten(r[0]) : r; | |
if(response) { | |
for (let i in response) { | |
result[i] = []; | |
for (let c in response[i]) { | |
result[i][c] = response[i][c].value; | |
} | |
} | |
result = flatten(result); | |
} else { | |
result = response; | |
} | |
return result; | |
}); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Tedious = require('tedious'); | |
module.exports = (dbConfig) => { | |
return { | |
'sqlServer:execute': (sql) => { | |
const connection = new Tedious.Connection(dbConfig); | |
return new Promise((res, rej) => { | |
connection.on('connect', err => { | |
if (err) { | |
rej(err); | |
} | |
const request = new Tedious.Request(sql, function(err, rowCount, rows) { | |
return err ? rej(err) : res(rows); | |
}); | |
connection.execSql(request); | |
}); | |
}); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I use Cypress with Typescript. Will it be possible to call cy.sqlserver in a typescript class file. Could you provide the guidance?