test: setup test framework in test_module project
This commit is contained in:
parent
874851ea2d
commit
422682c8b8
13 changed files with 1422 additions and 93 deletions
7
.github/workflows/linux-musl.yaml
vendored
7
.github/workflows/linux-musl.yaml
vendored
|
@ -35,11 +35,8 @@ jobs:
|
||||||
run: |
|
run: |
|
||||||
docker run --rm -v $(pwd)/.cargo:/root/.cargo -v $(pwd):/napi-rs -w /napi-rs builder cargo test -p napi-rs --lib -- --nocapture
|
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: |
|
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:
|
env:
|
||||||
RUST_BACKTRACE: 1
|
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
|
|
||||||
|
|
7
.github/workflows/linux.yaml
vendored
7
.github/workflows/linux.yaml
vendored
|
@ -60,12 +60,11 @@ jobs:
|
||||||
command: test
|
command: test
|
||||||
args: -p napi-rs --lib -- --nocapture
|
args: -p napi-rs --lib -- --nocapture
|
||||||
|
|
||||||
- name: Fuzzy tests
|
- name: Unit tests
|
||||||
run: |
|
run: |
|
||||||
yarn
|
yarn
|
||||||
cd test_module
|
yarn --cwd ./test_module build
|
||||||
yarn build
|
yarn test
|
||||||
node fuzzy.js
|
|
||||||
env:
|
env:
|
||||||
RUST_BACKTRACE: 1
|
RUST_BACKTRACE: 1
|
||||||
|
|
||||||
|
|
7
.github/workflows/macos.yaml
vendored
7
.github/workflows/macos.yaml
vendored
|
@ -60,12 +60,11 @@ jobs:
|
||||||
command: test
|
command: test
|
||||||
args: -p napi-rs --lib -- --nocapture
|
args: -p napi-rs --lib -- --nocapture
|
||||||
|
|
||||||
- name: Fuzzy tests
|
- name: Unit tests
|
||||||
run: |
|
run: |
|
||||||
yarn
|
yarn
|
||||||
cd test_module
|
yarn --cwd ./test_module build
|
||||||
yarn build
|
yarn test
|
||||||
node fuzzy.js
|
|
||||||
env:
|
env:
|
||||||
RUST_BACKTRACE: 1
|
RUST_BACKTRACE: 1
|
||||||
|
|
||||||
|
|
7
.github/workflows/windows.yaml
vendored
7
.github/workflows/windows.yaml
vendored
|
@ -66,12 +66,11 @@ jobs:
|
||||||
command: test
|
command: test
|
||||||
args: -p napi-rs --lib -- --nocapture
|
args: -p napi-rs --lib -- --nocapture
|
||||||
|
|
||||||
- name: Fuzzy tests
|
- name: Unit tests
|
||||||
run: |
|
run: |
|
||||||
yarn
|
yarn
|
||||||
cd test_module
|
yarn --cwd ./test_module build
|
||||||
yarn build
|
yarn test
|
||||||
node fuzzy.js
|
|
||||||
env:
|
env:
|
||||||
RUST_BACKTRACE: 1
|
RUST_BACKTRACE: 1
|
||||||
|
|
||||||
|
|
|
@ -1044,9 +1044,7 @@ impl Value<Object> {
|
||||||
|
|
||||||
pub fn is_date(&self) -> Result<bool> {
|
pub fn is_date(&self) -> Result<bool> {
|
||||||
let mut is_date = true;
|
let mut is_date = true;
|
||||||
let status = unsafe {
|
let status = unsafe { sys::napi_is_date(self.env, self.raw_value(), &mut is_date) };
|
||||||
sys::napi_is_date(self.env, self.raw_value(), &mut is_date)
|
|
||||||
};
|
|
||||||
check_status(status)?;
|
check_status(status)?;
|
||||||
Ok(is_date)
|
Ok(is_date)
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,7 +23,8 @@
|
||||||
"format:json": "prettier --parser json --write './**/*.json'",
|
"format:json": "prettier --parser json --write './**/*.json'",
|
||||||
"format:rs": "cargo fmt",
|
"format:rs": "cargo fmt",
|
||||||
"format:source": "prettier --config ./package.json --write './**/*.js'",
|
"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": {
|
"bugs": {
|
||||||
"url": "https://github.com/Brooooooklyn/napi-rs/issues"
|
"url": "https://github.com/Brooooooklyn/napi-rs/issues"
|
||||||
|
@ -47,6 +48,9 @@
|
||||||
"*.json": ["prettier --parser json --write"],
|
"*.json": ["prettier --parser json --write"],
|
||||||
"*.md": ["prettier --parser markdown --write"]
|
"*.md": ["prettier --parser markdown --write"]
|
||||||
},
|
},
|
||||||
|
"ava": {
|
||||||
|
"files": ["test_module/__test__/**/*.spec.js"]
|
||||||
|
},
|
||||||
"husky": {
|
"husky": {
|
||||||
"hooks": {
|
"hooks": {
|
||||||
"pre-commit": "lint-staged && cargo fmt --all"
|
"pre-commit": "lint-staged && cargo fmt --all"
|
||||||
|
@ -54,6 +58,7 @@
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/node": "^14.0.13",
|
"@types/node": "^14.0.13",
|
||||||
|
"ava": "^3.9.0",
|
||||||
"husky": "^4.2.5",
|
"husky": "^4.2.5",
|
||||||
"lint-staged": "^10.2.10",
|
"lint-staged": "^10.2.10",
|
||||||
"npm-run-all": "^4.1.5",
|
"npm-run-all": "^4.1.5",
|
||||||
|
|
14
test_module/__test__/is-date.spec.js
Normal file
14
test_module/__test__/is-date.spec.js
Normal 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()))
|
||||||
|
})
|
8
test_module/__test__/spawn.spec.js
Normal file
8
test_module/__test__/spawn.spec.js
Normal 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)
|
||||||
|
})
|
16
test_module/__test__/throw.spec.js
Normal file
16
test_module/__test__/throw.spec.js
Normal 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))
|
||||||
|
})
|
|
@ -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()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
|
|
@ -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)
|
|
|
@ -3,20 +3,26 @@ extern crate napi_rs as napi;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate napi_rs_derive;
|
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;
|
use std::convert::TryInto;
|
||||||
|
|
||||||
register_module!(test_module, init);
|
register_module!(test_module, init);
|
||||||
|
|
||||||
fn init(env: &Env, exports: &mut Value<Object>) -> Result<()> {
|
fn init(env: &Env, exports: &mut Value<Object>) -> Result<()> {
|
||||||
exports.set_named_property("testThrow", env.create_function("testThrow", test_throw)?)?;
|
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(
|
exports.set_named_property(
|
||||||
"testSpawnThread",
|
"testSpawnThread",
|
||||||
env.create_function("testSpawnThread", test_spawn_thread)?,
|
env.create_function("testSpawnThread", test_spawn_thread)?,
|
||||||
)?;
|
)?;
|
||||||
exports.set_named_property(
|
exports.set_named_property(
|
||||||
"testObjectIsDate",
|
"testObjectIsDate",
|
||||||
env.create_function("testObjectIsDate", test_object_is_date)?
|
env.create_function("testObjectIsDate", test_object_is_date)?,
|
||||||
)?;
|
)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -64,6 +70,15 @@ fn test_throw(_ctx: CallContext) -> Result<Value<Any>> {
|
||||||
Err(Error::from_status(Status::GenericFailure))
|
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)]
|
#[js_function(1)]
|
||||||
fn test_object_is_date(ctx: CallContext) -> Result<Value<Boolean>> {
|
fn test_object_is_date(ctx: CallContext) -> Result<Value<Boolean>> {
|
||||||
let obj: Value<Object> = ctx.get::<Object>(0)?;
|
let obj: Value<Object> = ctx.get::<Object>(0)?;
|
||||||
|
|
Loading…
Reference in a new issue