Stopping an active Testcafe run - javascript

I try to add a stop function to the Testcafe run. I start Testcafe with:
let testcafe = null;
let testcafeprom = null;
testcafeprom = createTestCafe('localhost', 1337, 1338)
.then(tc => {
testcafe = tc;
const runner = testcafe.createRunner();
return runner
.src([__basedir + '/tests/temp.js'])
.browsers(myBrowser)
//.browsers('browserstack:Chrome')
.screenshots(__basedir +'/allure/screenshots/', true)
.reporter(['uistatusreporter', {name: 'allure',output: 'test/report.json'}])
.run();
})
.then(failedCount => {
testcafe.close();
startReportGenerator();
capcon.stopCapture(process.stdout);
console.log("Testcafe Ende");
if(failedCount>0){
res.sendStatus(400);
console.log('Tests failed: ' + failedCount);
//res.statusCode = 400; //BadRequest 400
/*
res.json({
success: 'failed',
fails: failedCount
});
*/
}else{
//res.statusCode = 200; //BadRequest 400
res.sendStatus(200);
console.log('All success');
/*
res.json({
success: 'ok',
fails: failedCount
});
*/
}
})
.catch(error => {
testcafe.close();
console.log('Tests failed: Testcafe Error');
console.log(error);
res.sendStatus(401);
});
Then I added a function to stop the run:
router.get('/stopit', async (req, res) => {
testcafeprom.cancel();
res.sendStatus(200);
});
As I understand is that createTestCafe will return a promise and in all to stop the promise I call testcafeprom.cancel(); or testcafeprom.stop();
But the browser is running and running. A simple testcafe.close(); will stop Testcafe complete. But I want to stop it and not shoot it down.
Any suggestion to stop it a better way?
Update:
I have also tested the way to make the runner as promise:
createTestCafe('localhost', 1337, 1338)
.then(tc => {
testcafe = tc;
const runner = testcafe.createRunner();
testcafeprom = runner
.src([__basedir + '/tests/temp.js'])
.browsers(myBrowser)
//.browsers('browserstack:Chrome')
.screenshots(__basedir +'/allure/screenshots/', true)
.reporter(['uistatusreporter', {name: 'allure',output: 'test/report.json'}])
.run();
return testcafeprom;
})
Adding also
await testcafeprom.cancel();
This will have exact the same result as testCafe.close(), means everything is shoot down without any response. Iam confused.
And finally I tried:
let runner = null;
createTestCafe('localhost', 1337, 1338, void 0, true)
.then(testcafe => {
runner = testcafe.createRunner();
})
.then(() => {
return runner
.src([__basedir + '/tests/temp.js'])
.browsers(myBrowser)
//.browsers('browserstack:Chrome')
.screenshots(__basedir +'/allure/screenshots/', true)
.reporter(['uistatusreporter', {name: 'allure',output: 'test/report.json'}])
.run()
.then(failedCount => {
//testcafe.close();
startReportGenerator();
capcon.stopCapture(process.stdout);
console.log(`Finished. Count failed tests:${failedCount}`);
//process.exit(failedCount);
res.sendStatus(200);
});
})
.catch(error => {
startReportGenerator();
capcon.stopCapture(process.stdout);
console.log(error);
//process.exit(1);
res.sendStatus(401);
});
But here is the same. If I call await runner.stop() it looks like that the command will kill the whole process and nothing comes back to the promise.
Is this such a secret how to stop a running TestCafe instance or is the secret that the whole process is shoot down?

It's difficult to say precisely why you face an issue since I cannot debug your project. However, you are correct when you use cancel to stop test execution. The cancel method stops tests execution and closes the browser, but it does not stop TestCafe. This means that you can use the run method again, and it will start test execution and open browsers again.
I created an example to demonstrate that this approach works.
Test code:
fixture `fixture`
.page `http://example.com`;
for (let i = 0; i < 50; i++) {
test(`test A ${i}`, async t => {
await t.click('h1');
});
}
Testcafe start code:
const createTestCafe = require('testcafe');
(async () => {
const testCafe = await createTestCafe();
let timeout;
const runner = testCafe.createRunner();
const runPromise = runner
.src('test.js')
.browsers('chrome')
.run();
const cancelPromise = new Promise(resolve => {
timeout = setTimeout(() => {
runPromise.cancel();
resolve('canceled');
}, 20000);
});
let value = await Promise.race([runPromise, cancelPromise]);
if (value === 'canceled')
console.log('test execution was canceled')
value = await runner.run();
console.log(`${value} failed tests`);
await testCafe.close();
})();

Related

Emitting an error in my jest test doesn't trigger the error handler as expected

My download code relies on listening on listening for events to determine when to fire callbacks, and whether the promise it's in should be resolved or rejected:
async function downloadMtgJsonZip() {
const path = Path.resolve(__dirname, 'resources', fileName);
const writer = Fs.createWriteStream(path);
console.info('...connecting...');
const { data, headers } = await axios({
url,
method: 'GET',
responseType: 'stream',
});
return new Promise((resolve, reject) => {
const timeout = 20000;
const timer = setTimeout(() => {
console.log('timed out'); // debug log
writer.close();
reject(new Error(`Promise timed out after ${timeout} ms`));
}, timeout);
let error = null;
const totalLength = headers['content-length'];
const progressBar = getProgressBar(totalLength);
console.info('...starting download...');
// set up data and writer listeners
data.on('data', (chunk) => progressBar.tick(chunk.length));
data.on('error', (err) => { // added this to see if it would be triggered - it is not
console.log(`did a data error: ${error}`);
error = err;
clearTimeout(timer);
writer.close();
reject(err);
});
writer.on('error', (err) => {
console.log(`did a writer error: ${error}`);
error = err;
clearTimeout(timer);
writer.close();
reject(err);
});
writer.on('close', () => {
const now = new Date();
console.log(`close called: ${now}`);
console.log(`error is: ${error}`);
console.info(
`Completed in ${(now.getTime() - progressBar.start) / 1000} seconds`,
);
clearTimeout(timer);
console.log(`time cleared: ${timer}`);
if (!error) resolve(true);
// no need to call the reject here, as it will have been called in the
// 'error' stream;
});
// finally call data.pipe with our writer
data.pipe(writer);
});
}
I had some issues writing my tests, but I managed to get something that worked, despite feeling slightly messy, based on this advice:
Here is my test, with the relevant bits of my set up:
describe('fetchData', () => {
let dataChunkFn;
let dataErrorFn;
let dataOnFn;
let writerCloseFn;
let writerErrorFn;
let writerOnFn;
let pipeHandler;
beforeEach(() => {
// I've left all the mocking in place,
// to give an idea of what I've set up
const mockWriterEventHandlers = {};
const mockDataEventHandlers = {};
dataChunkFn = jest.fn((chunk) => mockDataEventHandlers.data(chunk));
dataErrorFn = jest.fn((chunk) => mockDataEventHandlers.data(chunk));
dataOnFn = jest.fn((e, cb) => {
mockDataEventHandlers[e] = cb;
});
writerCloseFn = jest.fn(() => mockWriterEventHandlers.close());
writerErrorFn = jest.fn(() => mockWriterEventHandlers.error());
writerOnFn = jest.fn((e, cb) => {
mockWriterEventHandlers[e] = cb;
});
const getMockData = (pipe) => ({
status: 200,
data: {
pipe,
on: dataOnFn,
},
headers: { 'content-length': 100 },
});
axios.mockImplementationOnce(() => getMockData(pipeHandler));
fs.createWriteStream.mockImplementationOnce(() => ({
on: writerOnFn,
close: writerCloseFn,
}));
jest.spyOn(console, 'info').mockImplementation(() => {});
jest.spyOn(console, 'log').mockImplementation(() => {});
});
it.only('handles errors from the writer', async (done) => {
console.log('writer error');
expect.assertions(1);
pipeHandler = (writer) => writer.emit('error', new Error('bang'));
try {
await downloadMtgJsonZip();
done.fail('ran without error');
} catch (exception) {
// expect(dataErrorFn).toHaveBeenCalled(); // neither of these are called
expect(writerErrorFn).toHaveBeenCalled();
}
});
I would have expected, that when data(pipe) ran, and the writer emitted a new error, it would have triggered at least one of the error listeners.
The code runs as expected, and it even handles the timeout (which I initially set too low), but this last test doesn't run.
As I commented above, neither of the functions above are called, so the expect.assertions(1); code fails the test.
It's possible I need to fundamentally change how I've written the tests, but I'm not sure how I would do that.
Why doesn't that last test pass?
When the code invokes data.pipe(writer), it's running your pipeHandler function defined in the test. This function takes a given writer object and calls writer.emit(...). I believe the issue is that the writer object being passed in is the one mocked out for fs.createWriteStream(), which doesn't have an emit method defined, so nothing is happening in response to that call. It is likely throwing an error, which you may be able to see in your catch block.
I believe what you want is to invoke the handlers saved by the writerOnFn. One way to do so would be to add a property to the object returned by your mock of fs.createWriteStream named emit and define it as a function that invokes the appropriate handler from inside mockWriterEventHandlers. I haven't tested this code but it would look something like the following
const writerEmitFn = (event, arg) => {
mockWriterEventHandlers[event](arg);
}
fs.createWriteStream.mockImplementationOnce(() => ({
on: writerOnFn,
close: writerCloseFn,
emit: writerEmitFn,
}));
My guess is that jest is gobbling up the error.
In order to continue running in the case of exceptions, jest could be guarding against ever having to run try and throw.
You could try expecting an error to have been thrown using jest's API.

How to prevent a infinite loop in nodejs child process spwan

I'm building an online ide by running the command to execute the code by spawn... but what should I do if there is an infinite loop in the code...The process is not being stopped
const { childElements } = require("dom-helpers");
const path = require("path");
const executeCpp = (filepath) => {
const file = path.basename(filepath[1].split(".")[0]);
const execute = new Promise((resolve, reject) => {
const run = spawn(
`cd ${filepath[0]} && g++ ${file}.cpp -o ${file}.out && ./${file}.out < ${file}.txt`,
{
shell: true,
}
// (error, stdout, stderr) => {
// error && reject({ error, stderr });
// stderr && reject(stderr);
// resolve(stdout);
// }
);
setTimeout(() => {
run.kill();
}, 2000);
run.on("exit", (code) => {
// clearTimeout(to);
resolve(`Process Exited with code ${code}`);
});
run.stderr.on("data", (data) => {
resolve(String(data));
console.log(data)
});
run.stdout.on("data", (data) => {
resolve(data);
console.log(data)
});
});
return execute;
};
module.exports = {
executeCpp,
};
This is my workaround, but it is not working.
Also, when I use exec, I can reject the promise if there is an error, but it's not working; instead, I need to resolve the promise even if there is an error.

nodejs api route testing(mocha) failed due to timeout

i am new to nodejs testing with mocha and chai.right now I am having issue while testing a API route handler with mocha. my route handler code is
exports.imageUpload = (req, res, next) => {
Upload(req,res, async () => {
//get the image files and its original urls (form-data)
let files = req.files['files[]'];
const originalUrls = req.body.orgUrl;
// check the input parameters
if(files == undefined || originalUrls == undefined){
res.status(400).send({status:'failed', message:"input field cannot be undefined"})
}
if(files.length > 0 && originalUrls.length > 0){
//array of promises
let promises = files.map(async(file,index)=>{
//create a image document for each file
let imageDoc = new ImageModel({
croppedImageUrl : file.path,
originalImageUrl: (typeof originalUrls === 'string') ? originalUrls : originalUrls[index],
status: 'New'
});
// return promises to the promises array
return await imageDoc.save();
});
// resolve the promises
try{
const response = await Promise.all(promises);
res.status(200).send({status: 'success', res: response});
}catch(error){
res.status(500).send({status:"failed", error: error})
}
}else{
res.status(400).send({status:'failed', message:"input error"})
}
})
}
the Upload function is just a multer utility to store the imagefile
and my test code is
describe('POST /content/image_upload', () => {
it('should return status 200', async() => {
const response = await chai.request(app).post('/content/image_upload')
.set('Content-Type', 'application/form-data')
.attach('files[]',
fs.readFileSync(path.join(__dirname,'./asset/listEvent.png')), 'asset')
.field('orgUrl', 'https://images.unsplash.com/photo-1556830805-7cec0906aee6?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=1534&q=80')
expect(response.error).to.be.false;
expect(response.status).to.be.equal(200);
expect(response.body.status).to.be.equal('success');
response.body.res.forEach(item => {
expect(item).to.have.property('croppedImageUrl');
expect(item).to.have.property('originalImageUrl');
expect(item).to.have.property('status');
expect(item).to.have.property('_id');
})
})
})
and the output shown after running this code is
1) POST /content/image_upload
should return status 200:
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (/home/techv/content_capture/server/ContentServiceLib/api/test/image_upload.test.js)
I believe this updated code should work
describe('POST /content/image_upload', async() => {
await it('should return status 200', async() => {
const response = await chai.request(app).post('/content/image_upload')
.set('Content-Type', 'application/form-data')
.attach('files[]',
fs.readFileSync(path.join(__dirname,'./asset/listEvent.png')), 'asset')
.field('orgUrl', 'https://images.unsplash.com/photo-1556830805-7cec0906aee6?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=1534&q=80')
expect(response.error).to.be.false;
expect(response.status).to.be.equal(200);
expect(response.body.status).to.be.equal('success');
response.body.res.forEach(item => {
expect(item).to.have.property('croppedImageUrl');
expect(item).to.have.property('originalImageUrl');
expect(item).to.have.property('status');
expect(item).to.have.property('_id');
})
})
})
async needs to be added at the top most level as well for it to actually be asynchronous
I think your code is breaking as the upload is taking more then 2 seconds which is the default timeout of mocha as specified in the mocha documentation at https://mochajs.org/#-timeout-ms-t-ms
There are two ways to overcome this problem:
Add a global level timeout while running your command to run the tests --timeout 5s
Add a test specific timeout in the test case itself e.g.
describe('POST /content/image_upload', () => {
it('should return status 200', async() => {
this.timeout(5000) // Timeout
const response = await chai.request(app).post('/content/image_upload')
.set('Content-Type', 'application/form-data')
.attach('files[]',
fs.readFileSync(path.join(__dirname,'./asset/listEvent.png')), 'asset')
.field('orgUrl', 'https://images.unsplash.com/photo-1556830805-7cec0906aee6?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=1534&q=80')
expect(response.error).to.be.false;
expect(response.status).to.be.equal(200);
expect(response.body.status).to.be.equal('success');
response.body.res.forEach(item => {
expect(item).to.have.property('croppedImageUrl');
expect(item).to.have.property('originalImageUrl');
expect(item).to.have.property('status');
expect(item).to.have.property('_id');
})
})
})
Just check the line #3 in above code snippet. more details are here : https://mochajs.org/#test-level

neo4j Node js Unit test with Jest

I use the Jest framework to create unit tests. When I run them. there is the message at the end:
"Jest did not exit one second after the test run has completed.
This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with --detectOpenHandles to troubleshoot this issue."
To exit, I use the command "--forceExit".
Also, I've tried to find an issue with --detectOpenHandles, but it didn't show anything.
I can't find what it hasn't closed, session or driver, or something else.
How could it be fixed?
const neo4j = require("neo4j-driver");
const driver = neo4j.v1.driver(
`bolt://${host}`,
neo4j.v1.auth.basic(username, password)
);
beforeAll(async () => {
await cleanDB();
});
afterAll(async () => {
await cleanDB();
driver.close();
});
async function cleanDB() {
await runQuery(`...query`);
}
async function runQuery(query) {
const session = driver.session();
return session
.writeTransaction(tx => tx.run(query))
.then(result => {
session.close();
return result;
})
.catch(error => {
session.close();
return { error };
});
}
describe(`bla-bla-bla`, function() {
beforeAll(async () => {
await dataBaseLoader(data);
});
test(`bla-bla-bla`, async function() {
const result = await runQuery(
'...query' );
//Body of Test
expect(result).toStrictEqual(expected);
});
There is no need to use async before function if you don't use await in body, also if function is not async access it without await
function cleanDB() {
runQuery(`...query`);
}
function runQuery(query) {
const session = driver.session();
return session
.writeTransaction(tx => tx.run(query))
.then(result => {
session.close();
return result;
})
.catch(error => {
session.close();
return { error };
});
}
and so on, check all your functions, maybe it will help

how to assert catch promise with sinon and chai

We have a method in our CLI which uses method returning a promise to print message to user.
exports.handler = (argv) => {
let customUtils = new Utils(argv);
Utils.deploy()
.then(res => console.log(`Ressource was deployed`))
.catch(e => {
console.error(`Ressource was not deployed`);
console.error(e);
process.exit(1);
});
}
We are looking for a way to test console errors and process exit in case of deploy() promise rejection.
We tried using sandbox stub then assert in an async test:
describe('when promise is errored', () => {
beforeEach(() => {
sandbox = sinon.createSandbox();
utilsStub = sandbox.stub(Utils.prototype, 'deploy').rejects('rejected');
processStub = sandbox.stub(process, 'exit');
consoleStub = sandbox.stub(console, 'error');
});
afterEach(() => {
sandbox.restore();
});
it('should call deploy and log the error before exiting', async () => {
await handler({});
expect(utilsStub).to.have.been.called;
expect(console.error).to.have.been.called;
});
});
This test doesn't work: AssertionError: expected error to have been called at least once, but it was never called.
The same happens when we expect(process.exit).to.have.been.called;. It's never called.
We successfuly tested the then part in a similary way:
describe('when promise is resolved', () => {
beforeEach(() => {
sandbox = sinon.createSandbox();
utilsStub = sandbox.stub(Utils.prototype, 'deploy').callsFake(() => Promise.resolve('some text'));
consoleStub = sandbox.stub(console, 'log');
});
afterEach(() => {
sandbox.restore();
});
it('should call deploy and print success message', async () => {
await handler({});
expect(utilsStub).to.have.been.called;
expect(console.log).to.have.been.calledWith('Ressource was deployed');
});
});
There are some things to fix the source and test file.
For source file, we must use customUtils to call deploy() function. Since, you can use async/await, convert it from Promise can produce better code.
exports.handler = async argv => { // put async
let customUtils = new Utils(argv);
try {
await customUtils.deploy(); // change to await and use customUtils
console.log(`Ressource was deployed`);
} catch (e) {
console.error(`Ressource was not deployed`);
console.error(e);
process.exit(1);
}
};
For test file, nothing changes
describe('when promise is errored', () => {
beforeEach(() => {
sandbox = sinon.createSandbox();
utilsStub = sandbox.stub(Utils.prototype, 'deploy').rejects('rejected');
processStub = sandbox.stub(process, 'exit');
consoleStub = sandbox.stub(console, 'error');
});
afterEach(() => {
sandbox.restore();
});
it('should call deploy and log the error before exiting', async () => {
await handler({});
expect(utilsStub).to.have.been.called;
expect(console.error).to.have.been.called;
expect(process.exit).to.have.been.called; // add it
});
});
UPDATED:
In case want to still use promise, we have to make sure we return the promise.
exports.handler = (argv) => {
let customUtils = new Utils(argv);
return customUtils.deploy() // <== specify return here
.then(res => console.log(`Ressource was deployed`))
.catch(e => {
console.error(`Ressource was not deployed`);
console.error(e);
process.exit(1);
});
};
Hope it helps
You need to be able await the result of exports.handler before you test your assertions. You are awaiting it, but exports.handler is not returning the promise, so there's nothing to await in the test — exports.handler returns undefined immediately so the test runs the assertions in the same event loop before console.error can be called.
I'm not sure why you aren't seeing similar problems in the test where the promise resolves. (Maybe worth checking that that test fails properly)
This should help:
exports.handler = (argv) => {
let customUtils = new Utils(argv);
//Utils.deploy() // <- is that a typo?
return customUtils.deploy()
.then(res => console.log(`Ressource was deployed`))
.catch(e => {
console.error(`Ressource was not deployed`);
console.error(e);
process.exit(1);
});
}
Also in your tests you are creating a spy with:
consoleStub = sandbox.stub(console, 'error');
But writing the assertion directly on console.error. I don't think this should work:
expect(console.error).to.have.been.called;
// maybe expect(consoleStub)...
With those changes the test passes for me and (more importantly) fails when I don't call console.error in the catch.

Categories