Skip to content

Null instead of undefined in TestResult #796

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
agostbiro opened this issue Feb 7, 2025 · 6 comments
Open

Null instead of undefined in TestResult #796

agostbiro opened this issue Feb 7, 2025 · 6 comments

Comments

@agostbiro
Copy link
Member

agostbiro commented Feb 7, 2025

Steps to reproduce

  1. Check out the fix/null-instead-of-undefined branch
  2. Navigate to js/integration-tests/solidity-tests in the repo
  3. Run pnpm i
  4. Run pnpm test

Expected result

All tests pass.

Actual result

Many tests fail with the following message:

Error: Expected reason to be undefined for test that didn't fail, instead it is: 'null'

This is caused by returning null instead of undefined as the optional failure reason for succeeding tests.

@github-project-automation github-project-automation bot moved this to Inbox in EDR Feb 7, 2025
@agostbiro agostbiro changed the title Null in TestResult Null instead of undefined in TestResult Feb 13, 2025
@agostbiro
Copy link
Member Author

I've tried to repro this in isolation, but no luck.

Rust code: https://github.com/agostbiro/napi-rs-null-vs-undefined/blob/a3b4ed07aa6ab062e003d96ddd2bd0bcd62f6823/src/lib.rs#L15
JS test: https://github.com/agostbiro/napi-rs-null-vs-undefined/blob/a3b4ed07aa6ab062e003d96ddd2bd0bcd62f6823/__test__/index.spec.ts

To execute the JS test, clone the linked repo and then: yarn && yarn test

@AmosAidoo
Copy link

I've tried to repro this in isolation, but no luck.

@agostbiro when you say "no luck", are you pointing to the fact that it passes in isolation but fails in the solidity tests?

@AmosAidoo
Copy link

Looks like it would always be null if #[napi] is used. #[napi(object)] behaves like we want just like in your isolated test, but unfortunately, the impls will not work with #[napi(object)].
Based on https://napi.rs/docs/concepts/values#undefined, I played around with this idea:

pub struct TestResult {
  // snip
  #[napi(readonly)]
  pub reason: Option<Either<String, ()>>
  // snip
}

Then for the impl From part, I updated reason to:

reason: test_result.reason.map_or(Some(Either::B(())), |reason| Some(Either::A(reason)))

And the tests pass.

@agostbiro
Copy link
Member Author

I've tried to repro this in isolation, but no luck.

@agostbiro when you say "no luck", are you pointing to the fact that it passes in isolation but fails in the solidity tests?

Hey, yeah I mean in the repor it passes, but in Solidity tests we sometimes get null instead of undefined.

@agostbiro
Copy link
Member Author

Looks like it would always be null if #[napi] is used. #[napi(object)] behaves like we want just like in your isolated test, but unfortunately, the impls will not work with #[napi(object)]. Based on https://napi.rs/docs/concepts/values#undefined, I played around with this idea:

pub struct TestResult {
// snip
#[napi(readonly)]
pub reason: Option<Either<String, ()>>
// snip
}
Then for the impl From part, I updated reason to:

reason: test_result.reason.map_or(Some(Either::B(())), |reason| Some(Either::A(reason)))
And the tests pass.

Oh nice, could you open a PR with this?

@AmosAidoo
Copy link

Oh nice, could you open a PR with this?

Yes sure

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Inbox
Development

No branches or pull requests

2 participants