Merge pull request #73 from napi-rs/unit-test
test: setup test framework in test_module project
This commit is contained in:
commit
26bd7ddb62
14 changed files with 1430 additions and 101 deletions
9
.github/workflows/linux-musl.yaml
vendored
9
.github/workflows/linux-musl.yaml
vendored
|
@ -8,7 +8,7 @@ jobs:
|
||||||
strategy:
|
strategy:
|
||||||
fail-fast: false
|
fail-fast: false
|
||||||
matrix:
|
matrix:
|
||||||
node: ['10', '12', '13', '14']
|
node: ['10', '12', '14']
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
|
@ -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
|
|
||||||
|
|
9
.github/workflows/linux.yaml
vendored
9
.github/workflows/linux.yaml
vendored
|
@ -7,7 +7,7 @@ jobs:
|
||||||
strategy:
|
strategy:
|
||||||
fail-fast: false
|
fail-fast: false
|
||||||
matrix:
|
matrix:
|
||||||
node: ['10', '12', '13', '14']
|
node: ['10', '12', '14']
|
||||||
|
|
||||||
name: stable - x86_64-unknown-linux-gnu - node@${{ matrix.node }}
|
name: stable - x86_64-unknown-linux-gnu - node@${{ matrix.node }}
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
@ -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
|
||||||
|
|
||||||
|
|
9
.github/workflows/macos.yaml
vendored
9
.github/workflows/macos.yaml
vendored
|
@ -7,7 +7,7 @@ jobs:
|
||||||
strategy:
|
strategy:
|
||||||
fail-fast: false
|
fail-fast: false
|
||||||
matrix:
|
matrix:
|
||||||
node: ['10', '12', '13', '14']
|
node: ['10', '12', '14']
|
||||||
|
|
||||||
name: stable - x86_64-apple-darwin - node@${{ matrix.node }}
|
name: stable - x86_64-apple-darwin - node@${{ matrix.node }}
|
||||||
runs-on: macos-latest
|
runs-on: macos-latest
|
||||||
|
@ -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
|
||||||
|
|
||||||
|
|
9
.github/workflows/windows.yaml
vendored
9
.github/workflows/windows.yaml
vendored
|
@ -7,7 +7,7 @@ jobs:
|
||||||
strategy:
|
strategy:
|
||||||
fail-fast: false
|
fail-fast: false
|
||||||
matrix:
|
matrix:
|
||||||
node: ['10', '12', '13', '14']
|
node: ['10', '12', '14']
|
||||||
target:
|
target:
|
||||||
- x86_64-pc-windows-msvc
|
- x86_64-pc-windows-msvc
|
||||||
|
|
||||||
|
@ -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
|
||||||
|
|
||||||
|
|
|
@ -16,9 +16,9 @@
|
||||||
|
|
||||||
## NodeJS
|
## NodeJS
|
||||||
|
|
||||||
| Node10 | Node 12 | Node13 | Node14 |
|
| Node10 | Node 12 | Node14 |
|
||||||
| ------ | ------- | ------ | ------ |
|
| ------ | ------- | ------ |
|
||||||
| ✓ | ✓ | ✓ | ✓ |
|
| ✓ | ✓ | ✓ |
|
||||||
|
|
||||||
A minimal library for building compiled Node add-ons in Rust.
|
A minimal library for building compiled Node add-ons in Rust.
|
||||||
|
|
||||||
|
@ -195,7 +195,7 @@ npm test
|
||||||
| [napi_is_array](https://nodejs.org/api/n-api.html#n_api_napi_is_array) | 1 | v8.0.0 | ✅ |
|
| [napi_is_array](https://nodejs.org/api/n-api.html#n_api_napi_is_array) | 1 | v8.0.0 | ✅ |
|
||||||
| [napi_is_arraybuffer](https://nodejs.org/api/n-api.html#n_api_napi_is_arraybuffer) | 1 | v8.0.0 | ✅ |
|
| [napi_is_arraybuffer](https://nodejs.org/api/n-api.html#n_api_napi_is_arraybuffer) | 1 | v8.0.0 | ✅ |
|
||||||
| [napi_is_buffer](https://nodejs.org/api/n-api.html#n_api_napi_is_buffer) | 1 | v8.0.0 | ✅ |
|
| [napi_is_buffer](https://nodejs.org/api/n-api.html#n_api_napi_is_buffer) | 1 | v8.0.0 | ✅ |
|
||||||
| [napi_is_date](https://nodejs.org/api/n-api.html#n_api_napi_is_date) | 1 | v8.0.0 | ⛔️ |
|
| [napi_is_date](https://nodejs.org/api/n-api.html#n_api_napi_is_date) | 1 | v8.0.0 | ✅ |
|
||||||
| [napi_is_error](https://nodejs.org/api/n-api.html#n_api_napi_is_error_1) | 1 | v8.0.0 | ⛔️ |
|
| [napi_is_error](https://nodejs.org/api/n-api.html#n_api_napi_is_error_1) | 1 | v8.0.0 | ⛔️ |
|
||||||
| [napi_is_typedarray](https://nodejs.org/api/n-api.html#n_api_napi_is_typedarray) | 1 | v8.0.0 | ⛔️ |
|
| [napi_is_typedarray](https://nodejs.org/api/n-api.html#n_api_napi_is_typedarray) | 1 | v8.0.0 | ⛔️ |
|
||||||
| [napi_is_dataview](https://nodejs.org/api/n-api.html#n_api_napi_is_dataview) | 1 | v8.3.0 | ⛔️ |
|
| [napi_is_dataview](https://nodejs.org/api/n-api.html#n_api_napi_is_dataview) | 1 | v8.3.0 | ⛔️ |
|
||||||
|
|
|
@ -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