Hi,
Is there a way to start and stop serverless-offline programatically from a node script?
I would like to start the server before running my tests against it, then shut it down after the tests have finished running.
With jest, there are two separate files which can be used for this, globalSetup (https://jestjs.io/docs/en/configuration#globalsetup-string) and globalTeardown (https://jestjs.io/docs/en/configuration#globalteardown-string).
Thank you
Hi @balintpeak ,
This can be achieved using child_process.exec or child_process.spawn like so:
const { exec } = require('child_process')
exec('sls offline', (error, stdout, stderr) => {
})
Hi @dherault,
Thanks for the quick answer.
Can you recommend a way to be able to tell whether is has actually started or not?
Using exec I could not find a way to reliably wrap it in a promise and resolve when serverless-offline has successfully started.
The callback gets called when an error occured but does not get called when the server actually started.
const server = new Promise((resolve, reject) => {
const ps = exec('./node_modules/.bin/sls offline start --env test', (error, stdout, stderr) => {
console.log('******** run sls offline ********')
if (error) {
console.error(`exec error: ${error}`)
reject()
return
}
if (stderr) {
console.error(`stderr: ${stderr}`)
reject()
return
}
console.log(`stdout: ${stdout}`)
console.log('**** resolve promise *****')
resolve(ps)
})
})
@balintpeak what about listening serverless-offline stdout (i.e https://stackoverflow.com/a/10232330) and if it contains string [HTTP] server ready: you know that it should be up.
thank you @arnas, using spawn instead of exec did the trick!