test: setup test framework in test_module project

This commit is contained in:
LongYinan 2020-06-17 20:33:28 +08:00
parent 874851ea2d
commit 422682c8b8
No known key found for this signature in database
GPG key ID: A3FFE134A3E20881
13 changed files with 1422 additions and 93 deletions

View file

@ -35,11 +35,8 @@ jobs:
run: |
docker run --rm -v $(pwd)/.cargo:/root/.cargo -v $(pwd):/napi-rs -w /napi-rs builder cargo test -p napi-rs --lib -- --nocapture
- name: Build native module
- name: Unit test
run: |
docker run --rm -v $(pwd)/.cargo:/root/.cargo -v $(pwd):/napi-rs -w /napi-rs builder sh -c "yarn && cd test_module && yarn build"
docker run --rm -v $(pwd)/.cargo:/root/.cargo -v $(pwd):/napi-rs -w /napi-rs builder sh -c "yarn && yarn --cwd ./test_module build && yarn test"
env:
RUST_BACKTRACE: 1
- name: Fuzzy
run: docker run --rm -v $(pwd):/napi-rs -w /napi-rs/test_module node:${{ matrix.node }}-alpine node fuzzy.js

View file

@ -60,12 +60,11 @@ jobs:
command: test
args: -p napi-rs --lib -- --nocapture
- name: Fuzzy tests
- name: Unit tests
run: |
yarn
cd test_module
yarn build
node fuzzy.js
yarn --cwd ./test_module build
yarn test
env:
RUST_BACKTRACE: 1

View file

@ -60,12 +60,11 @@ jobs:
command: test
args: -p napi-rs --lib -- --nocapture
- name: Fuzzy tests
- name: Unit tests
run: |
yarn
cd test_module
yarn build
node fuzzy.js
yarn --cwd ./test_module build
yarn test
env:
RUST_BACKTRACE: 1

View file

@ -66,12 +66,11 @@ jobs:
command: test
args: -p napi-rs --lib -- --nocapture
- name: Fuzzy tests
- name: Unit tests
run: |
yarn
cd test_module
yarn build
node fuzzy.js
yarn --cwd ./test_module build
yarn test
env:
RUST_BACKTRACE: 1

View file

@ -1044,9 +1044,7 @@ impl Value<Object> {
pub fn is_date(&self) -> Result<bool> {
let mut is_date = true;
let status = unsafe {
sys::napi_is_date(self.env, self.raw_value(), &mut is_date)
};
let status = unsafe { sys::napi_is_date(self.env, self.raw_value(), &mut is_date) };
check_status(status)?;
Ok(is_date)
}

View file

@ -23,7 +23,8 @@
"format:json": "prettier --parser json --write './**/*.json'",
"format:rs": "cargo fmt",
"format:source": "prettier --config ./package.json --write './**/*.js'",
"format:yaml": "prettier --parser yaml --write './**/*.{yml,yaml}'"
"format:yaml": "prettier --parser yaml --write './**/*.{yml,yaml}'",
"test": "ava"
},
"bugs": {
"url": "https://github.com/Brooooooklyn/napi-rs/issues"
@ -47,6 +48,9 @@
"*.json": ["prettier --parser json --write"],
"*.md": ["prettier --parser markdown --write"]
},
"ava": {
"files": ["test_module/__test__/**/*.spec.js"]
},
"husky": {
"hooks": {
"pre-commit": "lint-staged && cargo fmt --all"
@ -54,6 +58,7 @@
},
"devDependencies": {
"@types/node": "^14.0.13",
"ava": "^3.9.0",
"husky": "^4.2.5",
"lint-staged": "^10.2.10",
"npm-run-all": "^4.1.5",

View file

@ -0,0 +1,14 @@
const test = require('ava')
const bindings = require('../index.node')
test('should return false if value is not date', (t) => {
t.false(bindings.testObjectIsDate({}))
t.false(bindings.testObjectIsDate(null))
t.false(bindings.testObjectIsDate())
t.false(bindings.testObjectIsDate(10249892))
})
test('should return true if value is date', (t) => {
t.true(bindings.testObjectIsDate(new Date()))
})

View file

@ -0,0 +1,8 @@
const test = require('ava')
const bindings = require('../index.node')
test('should be able to spawn thread and return promise', async (t) => {
const result = await bindings.testSpawnThread(20)
t.is(result, 6765)
})

View file

@ -0,0 +1,16 @@
const test = require('ava')
const bindings = require('../index.node')
test('should be able to throw error from native', (t) => {
t.throws(bindings.testThrow)
})
test('should be able to throw error from native with reason', (t) => {
const reason = 'Fatal'
t.throws(() => bindings.testThrowWithReason(reason), null, reason)
})
test('should throw if argument type is not match', (t) => {
t.throws(() => bindings.testThrowWithReason(2))
})

View file

@ -1,34 +0,0 @@
const { exec } = require('child_process')
Array.from({ length: 500 })
.reduce(async (acc) => {
await acc
await run()
}, null)
.then(() => {
console.info(`Fuzzy test success, passed ${500} tests.`)
})
.catch((e) => {
console.error(e)
process.exit(1)
})
const run = () => {
return new Promise((resolve, reject) => {
const testProcess = exec('node ./spawn.js', {
env: process.env,
})
testProcess.stdout.pipe(process.stdout)
testProcess.stderr.pipe(process.stderr)
testProcess.on('error', (err) => {
reject(err)
})
testProcess.on('exit', (code) => {
if (code) {
reject(new TypeError(`Child process exit code ${code}`))
} else {
resolve()
}
})
})
}

View file

@ -1,20 +0,0 @@
const assert = require('assert')
const testModule = require('./index.node')
function testSpawnThread(n) {
console.info('=== Test spawn task to threadpool')
return testModule.testSpawnThread(n)
}
testSpawnThread(20)
.then((value) => {
console.assert(value === 6765)
console.info('=== fibonacci result', value)
})
.catch((e) => {
console.error(e)
process.exit(1)
})
assert(testModule.testObjectIsDate({}) === false)
assert(testModule.testObjectIsDate(new Date()) === true)

View file

@ -3,20 +3,26 @@ extern crate napi_rs as napi;
#[macro_use]
extern crate napi_rs_derive;
use napi::{Any, CallContext, Env, Error, Number, Object, Result, Status, Task, Value, Boolean};
use napi::{
Any, Boolean, CallContext, Env, Error, JsString, Number, Object, Result, Status, Task, Value,
};
use std::convert::TryInto;
register_module!(test_module, init);
fn init(env: &Env, exports: &mut Value<Object>) -> Result<()> {
exports.set_named_property("testThrow", env.create_function("testThrow", test_throw)?)?;
exports.set_named_property(
"testThrowWithReason",
env.create_function("testThrowWithReason", test_throw_with_reason)?,
)?;
exports.set_named_property(
"testSpawnThread",
env.create_function("testSpawnThread", test_spawn_thread)?,
)?;
exports.set_named_property(
"testObjectIsDate",
env.create_function("testObjectIsDate", test_object_is_date)?
env.create_function("testObjectIsDate", test_object_is_date)?,
)?;
Ok(())
}
@ -64,6 +70,15 @@ fn test_throw(_ctx: CallContext) -> Result<Value<Any>> {
Err(Error::from_status(Status::GenericFailure))
}
#[js_function(1)]
fn test_throw_with_reason(ctx: CallContext) -> Result<Value<Any>> {
let reason = ctx.get::<JsString>(0)?;
Err(Error {
status: Status::GenericFailure,
reason: Some(reason.as_str()?.to_owned()),
})
}
#[js_function(1)]
fn test_object_is_date(ctx: CallContext) -> Result<Value<Boolean>> {
let obj: Value<Object> = ctx.get::<Object>(0)?;

1365
yarn.lock

File diff suppressed because it is too large Load diff