I have large CSV files that I want to parse and check values eg UUIDs, not nulls, lengths etc. I know there is some validation in some of the CSV parsers like fast-csv but not everything, so I am creating a test suite using Mocha/Chai combined with Validator.js.
However as I want to read the data line by line and do multiple tests on each value I am struggling to define my tests. This first code snippet works as the readline is within the 'it' function.
describe('Each row should validate', function() {
it('validate', function(done) {
const rl = readline.createInterface({
input: fs.createReadStream('file path'),
crlfDelay: Infinity
});
let lineCount = 0
rl.on('line', (line) => {
lineCount++
if(lineCount > 1) {
expect(validator.isUUID(line.split(',')[0]), `Line Number:${lineCount} is incorrect`).to.equal(true)
}
});
rl.on('close', function (line) {
done();
});
})
});
However what I would like is to have multiple 'it' tests against each line when I add those in then no tests are run at all.
describe('Each row should validate', function() {
const rl = readline.createInterface({
input: fs.createReadStream(product.sourcefile),
crlfDelay: Infinity
});
let lineCount = 0
rl.on('line', (line) => {
lineCount++
if(lineCount > 1) {
it('Should be a UUID', function() {
expect(validator.isUUID(line.split(',')[0]), `Line Number:${lineCount} is incorrect`).to.equal(true)
})
it('Should not be null', function() {
expect(validator.isEmpty(line.split(',')[0]), `Line Number:${lineCount} ID is missing`).to.equal(false)
})
}
});
});
Can anyone recommend a better way or fix the above?
Using NodeJS on Windows 10.