napi-rs/examples/napi-compat-mode/__test__/napi4/tokio_readfile.spec.ts
forehalo 2467b7139b
Introduce #[napi] procedural macro to automation development boilerplate (#696)
* napi procedural macro for basic rust/JavaScript types
* introduce the `compat-mode` for `napi` and `napi-derive` crates for backward compatible
* remove #[inline] and let compiler to decide the inline behavior
* cli now can produce the `.d.ts` file for native binding
* many tests and example for the new procedural macro

Co-authored-by: LongYinan <lynweklm@gmail.com>
2021-09-23 01:29:09 +08:00

29 lines
727 B
TypeScript

import fs from 'fs'
import path from 'path'
import test from 'ava'
import { napiVersion } from '../napi-version'
const bindings = require('../../index.node')
const filepath = path.resolve(__dirname, './example.txt')
test('should read a file and return its a buffer', async (t) => {
if (napiVersion < 4) {
t.is(bindings.testTokioReadfile, undefined)
return
}
await new Promise((resolve, reject) => {
bindings.testTokioReadfile(filepath, (err: Error | null, value: Buffer) => {
try {
t.is(err, null)
t.is(Buffer.isBuffer(value), true)
t.is(value.toString(), fs.readFileSync(filepath, 'utf8'))
resolve()
} catch (err) {
reject(err)
}
})
})
})