napi-rs/test_module/__test__/napi4/threadsafe_function.spec.ts
2020-08-24 18:22:48 +08:00

29 lines
592 B
TypeScript

import test from 'ava'
import { napiVersion } from '../napi-version'
const bindings = require('../../index.node')
test('should get js function called from a thread', async (t) => {
let called = 0
if (napiVersion < 4) {
t.is(bindings.testThreadsafeFunction, undefined)
return
}
await new Promise((resolve, reject) => {
bindings.testThreadsafeFunction((...args: any[]) => {
called += 1
try {
t.deepEqual(args, [null, 42, 1, 2, 3])
} catch (err) {
reject(err)
}
if (called === 2) {
resolve()
}
})
})
})