SSH2-Promise exec() method resolving before STDOUT is returned

113 views Asked by At

I am attempting to run a bash script utilizing the exec() method within the ssh2-promise library wrapper. This function is then placed inside of a beforeAll block to execute before my test cases run, and my HTTP response body should contain "connected:true." However, the promise resolves before the stdout is returned, and the tests execute before the script is actually finished executing, resulting in a "connected:false" status. I think this is in part due to the fact that the stdout takes around 2 seconds before it is returned in the console. Below is the start script function and the beforeAll block it resides in.

export async function startScript() {
  const data = await ssh.exec('/path/to/file && ./startScript')
  console.log(data)
}



describe('Connected endpoint tests with Script On', () => {
  beforeAll(() => {
    actions.startScript()
  })

I have tried different ssh and ssh2-promise library functions, but I can't seem to get the timing right. Some methods cause the log to occur after the test has finished executing, leading to failed tests and JEST errors with attempting to log after test completion. I would like to avoid a setTimeout() addition as this isn't best practice.

1

There are 1 answers

1
Barmar On BEST ANSWER

You're not returning the promise to beforeAll(), so it's not awaiting it.

describe('Connected endpoint tests with Script On', () => {
  beforeAll(() => {
    return actions.startScript()
  })