napi-rs/examples/napi/__tests__/generator.spec.ts
Caleb Everett 95dd6ef485
refactor(cli): refactor js-binding to support easier bundling. (#1957)
* Refactor js-binding to support bundling single-package artifacts

The existsSync check breaks the bundlers I've tested (esbuild, webpack,
ncc), so you cannot bundle napi-rs packages that have all binarys in a
single package.

I've tested this change with both single package and multi package
libraries.

* Update snapshots

* Update CI

* Fix electron test

---------

Co-authored-by: Caleb ツ Everett <calebev@amazon.com>
Co-authored-by: LongYinan <lynweklm@gmail.com>
2024-02-26 15:46:07 +08:00

57 lines
1.2 KiB
TypeScript

import test from 'ava'
import { Fib, Fib2, Fib3 } from '../index.cjs'
for (const [index, factory] of [
() => new Fib(),
() => Fib2.create(0),
() => new Fib3(0, 1),
].entries()) {
test(`should be able to stop a generator #${index}`, (t) => {
const fib = factory()
const gen = fib[Symbol.iterator]
t.is(typeof gen, 'function')
const iterator = gen()
t.deepEqual(iterator.next(), {
done: false,
value: 1,
})
iterator.next()
iterator.next()
iterator.next()
iterator.next()
t.deepEqual(iterator.next(), {
done: false,
value: 8,
})
t.deepEqual(iterator.return?.(), {
done: true,
})
t.deepEqual(iterator.next(), {
done: true,
})
})
test(`should be able to throw to generator #${index}`, (t) => {
const fib = factory()
const gen = fib[Symbol.iterator]
t.is(typeof gen, 'function')
const iterator = gen()
t.deepEqual(iterator.next(), {
done: false,
value: 1,
})
iterator.next()
iterator.next()
iterator.next()
iterator.next()
t.deepEqual(iterator.next(), {
done: false,
value: 8,
})
t.throws(() => iterator.throw!(new Error()))
t.deepEqual(iterator.next(), {
done: true,
})
})
}