let serverless

export async function setup(options) {
  const { servicePath } = options

  if (RUN_TEST_AGAINST_AWS) {
    return
  }

  // require lazy, AWS tests will execute faster
  const { default: Serverless } = await import('serverless')

  const { argv } = process

  // just areally hacky way to pass options
  process.argv = [
    '', // '/bin/node',
    '', // '/serverless-offline/node_modules/.bin/serverless',
    'offline',
    'start',
    '--webpack-no-watch',
  ]

  serverless = new Serverless({ servicePath })

  await serverless.init()
  await serverless.run()

  // set to original
  process.argv = argv
}

export async function teardown() {
  if (RUN_TEST_AGAINST_AWS) {
    return
  }

  const { plugins } = serverless.pluginManager

  const serverlessOffline = plugins.find(
    (item) => item.constructor.name === 'ServerlessOffline',
  )

  const serverlessWebpack = plugins.find(
    (item) => item.constructor.name === 'ServerlessWebpack',
  )

  if (serverlessWebpack) {
    await serverlessWebpack.cleanup()
  }

  await serverlessOffline.end(true)
}

const { node } = require('execa')
const { resolve } = require('path')

let serverlessProcess

const serverlessPath = resolve(
  __dirname,
  '../../../node_modules/serverless/bin/serverless',
)

export async function setup(options) {
  const { servicePath } = options

  if (RUN_TEST_AGAINST_AWS) {
    return
  }

  serverlessProcess = node(serverlessPath, ['offline', 'start'], {
    cwd: servicePath,
  })

  await new Promise((res) => {
    serverlessProcess.stdout.on('data', (data) => {
      if (String(data).includes('[HTTP] server ready')) {
        res()
      }
    })
  })
}

export async function teardown() {
  if (RUN_TEST_AGAINST_AWS) {
    return
  }

  serverlessProcess.cancel()

  await serverlessProcess
}

Rewrite plugin instantiation for integration tests