Mocha report to contain only failed cases - javascript

I want to cofigure mocha report to only contain failed cases.
I am using below code to read js test files and send mail on completion .
I want to send only failure cases. How to configure it in mocha ?
const Mocha = require('mocha');
const fs = require('fs');
const path = require('path');
const mocha = new Mocha({});
const sendMail = require('./sendMail');
const error = require('./errMsg');
async function executeMocha() {
const testDirPath = path.resolve('./') + '/test';
fs.readdirSync(testDirPath).filter(function (file) {
// Only keep the .js files
return file.substr(-3) === '.js';
}).forEach(function (file) {
mocha.addFile(
path.join(testDirPath, file)
);
});
mocha.reporter('mocha-simple-html-reporter', { output: '/tmp/testspec.html' }).run(
async (err,res) => {
if(err){
console.log("\n\n\n\nTest Case FAAAAAAAAAAILLLLLL \n\n\n\n");
}
console.log("\n\n\n\nTest Case Execution Successfull\n\n\n\n");
await getResult();
}
);
}
async function getResult() {
fs.readFile(('/tmp/testspec.html'), 'utf8', async (err, res) => {
if (err) {
await sendMail((process.env.env_type +error.fail.subject + process.env.CB_TEST_URL ), error.fail.body + err);
}
await sendMail((process.env.env_type +error.success.subject + process.env.CB_TEST_URL ), res);
})
}
module.exports = executeMocha

Um, assuming in your mocha.reporter function that that if(err) the test case is a fail, u can just do this(however I'm not even sure what would be a fail for u, not seeing you wanting any specific results or anything)
const Mocha = require('mocha');
const fs = require('fs');
const path = require('path');
const mocha = new Mocha({});
const sendMail = require('./sendMail');
const error = require('./errMsg');
async function executeMocha() {
const testDirPath = path.resolve('./') + '/test';
fs.readdirSync(testDirPath).filter(function (file) {
//in case someone has a FOLDER named with '.js' at the end
if(!fs.lstatSync(file).isFile()){return null}
// Only keep the .js files
return file.substr(-3) === '.js';
}).forEach(function (file) {
mocha.addFile(
path.join(testDirPath, file)
);
});
mocha.reporter('mocha-simple-html-reporter', { output: '/tmp/testspec.html' }).run(
async (err,res) => {
if(err){
console.log("\n\n\n\nTest Case FAAAAAAAAAAILLLLLL \n\n\n\n");
return await sendMail((process.env.env_type +error.fail.subject + process.env.CB_TEST_URL ), error.fail.body + err);
}
console.log("\n\n\n\nTest Case Execution Successfull\n\n\n\n");
}
);
}
async function getResult() { //error would only happen here if there was a problem READING the html file, unsure where the js files in question would be responsible
fs.readFile(('/tmp/testspec.html'), 'utf8', async (err, res) => {
if (err) {
await sendMail((process.env.env_type +error.fail.subject + process.env.CB_TEST_URL ), error.fail.body + err);
}
await sendMail((process.env.env_type +error.success.subject + process.env.CB_TEST_URL ), res);
})
}
module.exports = executeMocha

Related

Run code after executing promise in Javascript

I am trying to save to json the values returned from indeed api. I use indeed-scraper code from github https://github.com/rynobax/indeed-scraper
My code:
... required files ...
const parsedResults = []
indeed.query(queryOptions).then(response => {
response.forEach((res,i) => {
setTimeout(function(){
let url = res.url
let resultCount = 0
console.log(`\n Scraping of ${url} initiated...\n`)
const getWebsiteContent = async (url) => {
try {
const response = await axios.get(url)
const $ = cheerio.load(response.data)
...get scraped data...
parsedResults.push(metadata)
} catch (error) {
exportResults(parsedResults)
console.error(error)
}
}
getWebsiteContent(url)
}
, i*3000);
});
});
const outputFile = 'data.json'
const fs = require('fs');
const exportResults = (parsedResults) => {
fs.writeFile(outputFile, JSON.stringify(parsedResults, null, 4), (err) => {
if (err) {
console.log(err)
}
console.log(`\n ${parsedResults.length} Results exported successfully to ${outputFile}\n`)
})
}
parsedResults is not accessible in last portion of script, so to save as json file.
Any help appreciated!

Nodejs exits after awaiting async code without error

I'm making a build script for my angular app in node. Please have a look at the snippet:
const fs = require('fs-extra');
const dev = process.argv[2] === 'dev';
const folder = process.argv[3];
if (folder && fs.existsSync(`./projects/${folder}`)) {
const execSync = require('child_process').execSync;
// ng build --prod --output-hashing=none OR ng build --source-map --output-hashing=none
let command;
if (dev) {
command = 'ng build --source-map --output-hashing=none ' + folder;
} else {
command = 'ng build --prod --output-hashing=none ' + folder;
}
// execSync(command, {stdio:[0, 1, 2]});
(async function build()
{
const files = [
];
const { promisify } = require('util')
const getFiles = async () => {
try {
const readdir = promisify(fs.readdir);
await readdir(`./dist/${folder}`, {withFileTypes:true}, (err, elements) => {
//handling error
if (err) {
return console.error('Unable to scan directory: ' + err);
} else {
elements.forEach(async element => {
if( !element.isDirectory() && /.*-es2015.js$/.test(element.name) ) {
files.push(`./dist/${folder}/${element.name}`);
console.log(`Pushing file: ./dist/${folder}/${element.name}`);
}
});
}
});
} catch (err) {
console.error(err);
}
}
await getFiles();
// We need a random number for voiding the cache with every new build
const random = [...Array(10)].map(()=>(c = (r = Math.random()).toString(36)[2]) && r>.5 ? c.toUpperCase():c ).join('');
// create directory if doesnt exists (not needed anymore): await fs.ensureDir(`../js/${folder}/dist`)
if (!dev && files.length) {
const concat = require('concat');
await concat(files, `./dist/${folder}/concatenated.${random}.js`);
}
console.log('Build complete');
}
)();
} else if (folder && !fs.existsSync(`projects/${folder}`)) {
console.log('Specified destination folder does not exists as a project');
}
else {
console.log('Please specify a destination folder such as app-name');
}
Well, the mysterious is that just after await getFiles() call, the execution halts, no error neither message anywhere is shown. I'm getting crazy investigating this.
Can anybody spot the issue?
Thanks
The main issue in your code is that you are not promisfying the readdir correctly.
Try this:
(async () => {
try {
const readdir = require('util').promisify(require('fs').readdir);
const elements = await readdir(`./dist/${folder}`, { withFileTypes: true });
await Promise.all(
elements.map(async (element) => {
if (!element.isDirectory() && /.*-es2015.js$/.test(element.name)) {
files.push(`./dist/${folder}/${element.name}`);
console.log(`Pushing file: ./dist/${folder}/${element.name}`);
}
})
);
} catch (error) {
console.error('Unable to scan directory: ' + err);
}
})();
You can of course keep your forEach while omitting the async instead of the map + async + Promise.all. The difference is is that the one I suggest is faster since it utilizes concurrency while forEach would work sequentially! But either one would work!

Passing arguments to callback function with in a separte module

I would create a new separate module with a function that contains a callback that I call in main file.
My program should take 3 args: first a file directory, then file extention and finally a callback function that should filter a directory by fileextension.
Here's my module.js file
var fs = require('fs')
module.exports = function (directory, fileExtension, callbackfun) {
fs.readdir(directory, callbackfun);
}
then in main file I import module then use function.
modulejs(process.argv[2], process.argv[3], callbackfun(callbackfn));
var callbackfn = function (err, data, fileExtension) {
console.log(fileExtension);
let filtred = data.filter(file => { if (file.indexOf("." + fileExtension) > -1) { console.log(file); } });
};
function callbackfun(callbackfunc1) {
callbackfunc1(err, data, fileExtension);
}
The error that I get actually is
ReferenceError: err is not defined
UPDATE: I have edited my code, I get different error now
UPDATE2: I get the solution on internet but does not understand how it works, if anyboby explain it to me step by step it would be nice
module.jsconst fs = require('fs')
const path = require('path')
module.exports = function (dir, filterStr, callback) {
fs.readdir(dir, function (err, list) {
if (err) {
return callback(err)
}
list = list.filter(function (file) {
return path.extname(file) === '.' + filterStr
})
callback(null, list)
})
}
main.js
const filterFn = require('./module.js')
const dir = process.argv[2]
const filterStr = process.argv[3]
filterFn(dir, filterStr, function (err, list) {
console.log(list);
if (err) {
return console.error('There was an error:', err)
}
list.forEach(function (file) {
console.log(file)
})
})

Error: Cloud Functions for Firebase spawn EACCES (ghostscript)

I tried using Firebase Cloud Functions to create a thumbnail of a PDF file.
After the call of gs I get the following error:
2018-06-12T11:29:08.685Z E makeThumbnail: Error: spawn EACCES
at exports._errnoException (util.js:1020:11)
at ChildProcess.spawn (internal/child_process.js:328:11)
at exports.spawn (child_process.js:370:9)
at Object.exec (/user_code/node_modules/gs/index.js:86:28)
at Promise (/user_code/index.js:95:12)
at mkdirp.then.then (/user_code/index.js:86:12)
2018-06-12T11:29:08.698166767Z D makeThumbnail: Function execution took 780 ms, finished with status: 'error'
Is it necessary to use a component like ghostscript to use a plan other than Spark?
In addition, my code. Maybe I just do not see my problem in the code
const functions = require('firebase-functions');
const mkdirp = require('mkdirp-promise');
const gcs = require('#google-cloud/storage')();
const admin = require('firebase-admin');
const spawn = require('child-process-promise').spawn;
const path = require('path');
const os = require('os');
const fs = require('fs');
const gs = require('gs');
const THUMB_MAX_HEIGHT = 200;
const THUMB_MAX_WIDTH = 200;
const THUMB_PREFIX = 'thumb_';
const gs_exec_path = path.join(__dirname, './lambda-ghostscript/bin/gs');
try{admin.initializeApp(functions.config().firebase); } catch(e) {}
exports.makeThumbnail = functions.storage.object().onFinalize((object) => {
const filePath = object.name;
const contentType = object.contentType;
const fileDir = path.dirname(filePath);
const fileName = path.basename(filePath);
const thumbFilePath = path.normalize(path.join(fileDir, `${THUMB_PREFIX} ${fileName}`));
const tempLocalFile = path.join(os.tmpdir(), filePath);
const tempLocalDir = path.dirname(tempLocalFile);
const tempLocalThumbFile = path.join(os.tmpdir(), thumbFilePath);
const tmp_dir = os.tmpdir();
if (fileName.startsWith(THUMB_PREFIX)) {
console.log('Is thumbnail');
return null;
}
const bucket = gcs.bucket(object.bucket);
const file = bucket.file(filePath);
const thumbFile = bucket.file(thumbFilePath);
const metadata = {
contentType: contentType,
};
return mkdirp(tmp_dir).then(() => {
console.log("Dir Created");
console.log(tempLocalFile);
return file.download({destination: tempLocalFile});
}).then(() => {
console.log("File downloaded");
if(!contentType.startsWith("image/")){
return new Promise((resolve, reject) => {
const pg= 1;
gs().batch().nopause()
.option(`-dFirstPage=${pg}`)
.option(`-dLastPage=${pg}`)
.executablePath(gs_exec_path)
.device('png16m')
.output(tempLocalThumbFile+".png")
.input(tempLocalFile)
.exec(err => err ? reject(err) : resolve());
});
}
else
{
var args = [ tempLocalFile, '-thumbnail', `${THUMB_MAX_WIDTH}x${THUMB_MAX_HEIGHT}>`, tempLocalThumbFile ];
return spawn('convert', args, {capture: ['stdout', 'stderr']});
}
}).then(() => {
return bucket.upload(tempLocalThumbFile, { destination: thumbFilePath });
}).then(() => {
fs.unlinkSync(tempLocalFile);
fs.unlinkSync(tempLocalThumbFile);
return result[0];
});
});
After hours of scratching my head and running same code over and over again pointlessly, I've finally found the problem!
The executable path that you've defined is not correct. It should be 'gs'.
Here's a complete gs() call sample:
gs()
.batch()
.option('-dFirstPage=2')
.option('-dLastPage=2')
.nopause()
.res(90)
.executablePath('gs')
.device('jpeg')
.output(tempNewPath2)
.input(tempFilePath)
.exec((err, stdout, stderr) => {
if (!err) {
console.log('gs executed w/o error');
console.log('stdout', stdout);
console.log('stderr', stderr);
resolve();
} else {
console.log('gs error:', err);
reject(err);
}
});
For more help, you can go through a sample repo that I created for this issue
https://github.com/krharsh17/ghostscript-firebase-sample

Append currently logged in user to JSON file

I am trying to find a way to get the currently logged in user and than append them to a JSON file. Below is my code to first read the dir, then get the most recent file, return it and then append the current user that is logged in.
I can append a string to the file but when trying to perform req.user it states
Cannot read property 'user' of undefined
What would I need to include in this file so that it knows what user is?
let fs = require("fs"),
express = require("express"),
_ = require("underscore"),
User = require("./models/user"),
path = require("path");
let getFileAddUser = () => {
let filePath = '../automation_projects/wss-automation-u/results/temp/';
fs.readdir(filePath, (err, files) => {
if (err) { throw err; }
let file = getMostRecentFile(files, filePath);
console.log(file);
fs.readFile(filePath + file, 'utf8', (err, data) => {
let json = JSON.parse(data);
if(err){
console.error(err);
return;
} else {
//Un-comment to write to most recent file.
//==================================================
//This should find the currently logged in user and append them to the most recent file found.
json.currentuser = req.user;
fs.writeFile(filePath + file, JSON.stringify(json), (error) => {
if(error){
console.error(error);
return;
} else {
console.log(json);
}
});
//==================================================
console.log(data);
}
});
});
};
//Get the most recent file from the results folder.
function getMostRecentFile(files, path) {
let out = [];
files.forEach(function(file) {
let stats = fs.statSync(path + "/" +file);
if(stats.isFile()) {
out.push({"file":file, "mtime": stats.mtime.getTime()});
}
});
out.sort(function(a,b) {
return b.mtime - a.mtime;
})
return (out.length>0) ? out[0].file : "";
}
module.exports = getFileAddUser;
Thanks to a knowledgeable co-worker and some further research we were able to get this working. I'd like to share the code we came up with to append the currently logged in user to our results file. You will also notice we got some help using the Ramada.js library.
let fs = require("fs"),
express = require("express"),
_ = require("underscore"),
User = require("./models/user"),
r = require("ramda"),
path = require("path");
//This will be our function to get the most recent file from our dir and
//return it to us. We than user this function below.
function getMostRecentFile(files, path) {
let out = [];
let f = r.tail(files);
console.log(files);
f.forEach(function(file) {
let stats = fs.statSync(path + "/" +file);
if(stats.isFile()) {
out.push({"file":file, "mtime": stats.mtime.getTime()});
}
});
out.sort(function(a,b) {
return b.mtime - a.mtime;
})
return (out.length>0) ? out[0].file : "";
}
//Passing in 'u' as a argument which can than be used in a route and pass in
//anything that we want it to be. In our case it was the currently logged
//in user.
let getUser = (u) => {
let user = u;
let filePath = '../automation_projects/wss-automation-u/results/temp/';
//Comment above and uncomment below for testing locally.
// let filePath = "./temp/";
let file = "";
//Below we read our dir then get the most recent file using the
//getMostRecentfile function above.
read_directory(filePath).then( files => {
file = getMostRecentFile(files, filePath)
console.log(file);
return(read_file(filePath + file))
}).then( x => {
// Here we parse through our data with x representing the data that we
//returned above.
let json = JSON.parse(x);
return new Promise(function(resolve, reject) {
json.currentuser = u;
//And finally we write to the end of the latest file.
fs.writeFile(filePath + file, JSON.stringify(json), (error) => {
if(error) reject(error);
else resolve(json);
// console.log(json);
});
});
});
}
let read_directory = (path) => {
return new Promise((resolve, reject) => {
fs.readdir(path, (err, items) => {
if (err){
return reject(err)
}
return resolve([path, ...items])
})
})
}
let read_file = (path) => {
return new Promise((resolve, reject) => {
fs.readFile(path, "utf8", (err, items) => {
if (err){
return reject(err)
}
return resolve(items)
})
})
}
module.exports = getUser;
Than below is an example route with how to use the getUser module. You will want to require it like you do everything else with node.js and dependencies. Hope this helps someone in the future.
let getUser = require("getuser");
//Make a route to use the getUser module and pass in our argument value.
app.get("/", (req, res) => {
//With in the get user function pass in whatever you want to equal 'u' from the getuser module.
getUser(req.user.username);
res.render("index", { username: req.user });
});

Categories