CoolFace
Apppublic

strong-tie/inbound-calls

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
await-use.test.js295 linesDownload Raw Back to test
1'use strict'2 3const { test } = require('tap')4const { promisify } = require('node:util')5const sleep = promisify(setTimeout)6const boot = require('..')7 8test('await use - nested plugins with same tick callbacks', async (t) => {9  const app = {}10  boot(app)11 12  t.plan(4)13 14  await app.use((f, opts, cb) => {15    t.pass('plugin init')16    app.use((f, opts, cb) => {17      t.pass('plugin2 init')18      cb()19    })20    cb()21  })22  t.pass('reachable')23 24  await app.ready()25  t.pass('reachable')26})27 28test('await use - nested plugins with future tick callbacks', async (t) => {29  const app = {}30  boot(app)31 32  t.plan(4)33 34  await app.use((f, opts, cb) => {35    t.pass('plugin init')36    app.use((f, opts, cb) => {37      t.pass('plugin2 init')38      setImmediate(cb)39    })40    setImmediate(cb)41  })42  t.pass('reachable')43 44  await app.ready()45  t.pass('reachable')46})47 48test('await use - nested async function plugins', async (t) => {49  const app = {}50  boot(app)51 52  t.plan(5)53 54  await app.use(async (f, opts) => {55    t.pass('plugin init')56    await app.use(async (f, opts) => {57      t.pass('plugin2 init')58    })59    t.pass('reachable')60  })61  t.pass('reachable')62 63  await app.ready()64  t.pass('reachable')65})66 67test('await use - promise returning function plugins + promise chaining', async (t) => {68  const app = {}69  boot(app)70 71  t.plan(6)72 73  await app.use((f, opts) => {74    t.pass('plugin init')75    return app.use((f, opts) => {76      t.pass('plugin2 init')77      return Promise.resolve()78    }).then(() => {79      t.pass('reachable')80      return 'test'81    }).then((val) => {82      t.equal(val, 'test')83    })84  })85  t.pass('reachable')86 87  await app.ready()88  t.pass('reachable')89})90 91test('await use - await and use chaining', async (t) => {92  const app = {}93  boot(app)94 95  t.plan(3)96 97  app.use(async (f, opts, cb) => {98    await app.use(async (f, opts) => {99      t.pass('plugin init')100    }).use(async (f, opts) => {101      t.pass('plugin2 init')102    })103  })104 105  await app.ready()106  t.pass('reachable')107})108 109function thenableRejects (t, thenable, err, msg) {110  return t.rejects(async () => { await thenable }, err, msg)111}112 113test('await use - error handling, async throw', async (t) => {114  const app = {}115  boot(app)116 117  t.plan(2)118 119  await thenableRejects(t, app.use(async (f, opts) => {120    throw Error('kaboom')121  }), Error('kaboom'))122 123  await t.rejects(app.ready(), Error('kaboom'))124})125 126test('await use - error handling, async throw, nested', async (t) => {127  const app = {}128  boot(app)129 130  t.plan(2)131 132  await thenableRejects(t, app.use(async function a (f, opts) {133    await app.use(async function b (f, opts) {134      throw Error('kaboom')135    })136  }, Error('kaboom')), 'b')137 138  t.rejects(() => app.ready(), Error('kaboom'))139})140 141test('await use - error handling, same tick cb err', async (t) => {142  const app = {}143  boot(app)144 145  t.plan(2)146 147  await thenableRejects(t, app.use((f, opts, cb) => {148    cb(Error('kaboom'))149  }), Error('kaboom'))150 151  t.rejects(() => app.ready(), Error('kaboom'))152})153 154test('await use - error handling, same tick cb err, nested', async (t) => {155  const app = {}156  boot(app)157 158  t.plan(2)159 160  await thenableRejects(t, app.use((f, opts, cb) => {161    app.use((f, opts, cb) => {162      cb(Error('kaboom'))163    })164    cb()165  }), Error('kaboom'))166 167  t.rejects(() => app.ready(), Error('kaboom'))168})169 170test('await use - error handling, future tick cb err', async (t) => {171  const app = {}172  boot(app)173 174  t.plan(2)175 176  await thenableRejects(t, app.use((f, opts, cb) => {177    setImmediate(() => { cb(Error('kaboom')) })178  }), Error('kaboom'))179 180  t.rejects(() => app.ready(), Error('kaboom'))181})182 183test('await use - error handling, future tick cb err, nested', async (t) => {184  const app = {}185  boot(app)186 187  t.plan(2)188 189  await thenableRejects(t, app.use((f, opts, cb) => {190    app.use((f, opts, cb) => {191      setImmediate(() => { cb(Error('kaboom')) })192    })193    cb()194  }), Error('kaboom'))195 196  t.rejects(() => app.ready(), Error('kaboom'))197})198 199test('mixed await use and non-awaited use ', async (t) => {200  const app = {}201  boot(app)202  t.plan(16)203 204  let firstLoaded = false205  let secondLoaded = false206  let thirdLoaded = false207  let fourthLoaded = false208 209  await app.use(first)210  t.ok(firstLoaded, 'first is loaded')211  t.notOk(secondLoaded, 'second is not loaded')212  t.notOk(thirdLoaded, 'third is not loaded')213  t.notOk(fourthLoaded, 'fourth is not loaded')214  app.use(second)215  t.ok(firstLoaded, 'first is loaded')216  t.notOk(secondLoaded, 'second is not loaded')217  t.notOk(thirdLoaded, 'third is not loaded')218  t.notOk(fourthLoaded, 'fourth is not loaded')219  await app.use(third)220  t.ok(firstLoaded, 'first is loaded')221  t.ok(secondLoaded, 'second is loaded')222  t.ok(thirdLoaded, 'third is loaded')223  t.ok(fourthLoaded, 'fourth is loaded')224  await app.ready()225  t.ok(firstLoaded, 'first is loaded')226  t.ok(secondLoaded, 'second is loaded')227  t.ok(thirdLoaded, 'third is loaded')228  t.ok(fourthLoaded, 'fourth is loaded')229 230  async function first () {231    firstLoaded = true232  }233 234  async function second () {235    secondLoaded = true236  }237 238  async function third (app) {239    thirdLoaded = true240    app.use(fourth)241  }242 243  async function fourth () {244    fourthLoaded = true245  }246})247 248test('await use - mix of same and future tick callbacks', async (t) => {249  const app = {}250  boot(app, { autostart: false })251  let record = ''252 253  t.plan(4)254 255  await app.use(async function plugin0 () {256    t.pass('plugin0 init')257    record += 'plugin0|'258  })259  await app.use(async function plugin1 () {260    t.pass('plugin1 init')261    await sleep(500)262    record += 'plugin1|'263  })264  await sleep(1)265  await app.use(async function plugin2 () {266    t.pass('plugin2 init')267    await sleep(500)268    record += 'plugin2|'269  })270  record += 'ready'271  t.equal(record, 'plugin0|plugin1|plugin2|ready')272})273 274test('await use - fork the promise chain', (t) => {275  t.plan(3)276  const app = {}277  boot(app, { autostart: false })278 279  async function setup () {280    let set = false281    await app.use(async function plugin0 () {282      t.pass('plugin0 init')283      await sleep(500)284      set = true285    })286    t.equal(set, true)287  }288  setup()289 290  app.ready((err, done) => {291    t.error(err)292    done()293  })294})295