node-java icon indicating copy to clipboard operation
node-java copied to clipboard

Testing issue with Jest

Open rgwch opened this issue 2 years ago • 3 comments

This bridge works great for me, thanks! Only when it comes to unit testing, I run into the following issue:

This example test

import java from "java";
describe("Javascript-java bridge",()=>{
  it("loads the bridge",()=>{
    expect(java).toBeTruthy()
  })
})

produces the following result:

npx jest src/util/java-init.spec.ts 
 PASS  src/util/java-init.spec.ts
  Javascript-java bridge
    ✓ loads the bridge (1 ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        1.859 s, estimated 2 s
Ran all test suites matching /src\/util\/java-init.spec.ts/i.
Jest did not exit one second after the test run has completed.

This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with `--detectOpenHandles` to troubleshoot this issue.

When running with --detectOpenHandles, it eventually throws a "segmentation fault"

Environment: Ubuntu 20.04, Java 1.8.0_312, node 16.14.0, Jest 27.4.7

Any ideas how to solve this?

rgwch avatar May 03 '22 05:05 rgwch

@rgwch Any help about how you manage to run node-java with jest i am facing this error. Do you know how to solve this?

 FAIL  src/testing/sum.test.js
  ● Test suite failed to run
                                                                                                                                                                                                                                        
    The specified module could not be found.
    \\mypath\app\node_modules\java\build\Release\nodejavabridge_bindings.node

      1 | //import sum from "./sum.js"
      2 | import myfunc from "../sum.js"
    > 3 | import java from "java";
        | ^
      4 |
      5 | test('adds 1 + 2 to equal 3', () => {
      6 |     expect(myfunc.myfunc(1, 2)).toBe(3);

      at Runtime._loadModule (node_modules/jest-runtime/build/index.js:1009:29)
      at Object.<anonymous> (node_modules/java/lib/nodeJavaBridge.js:21:16)
      at Object.<anonymous> (node_modules/java/index.js:2:18)

PanagiotisDrakatos avatar Mar 24 '23 16:03 PanagiotisDrakatos

  • JAVA_HOME set correctly? Supported Java version? (I only tried 1.8)

  • Tried to reinstall the module completely?

    npm r java npm i java

this helped sometimes...

rgwch avatar Mar 25 '23 09:03 rgwch

@rgwch I already tried reinstall and install nothings works. Java path is set correctly actually the code works correctly but the problem is that i get this error only when i tried to run it with jest. For some reason cannot find module of node-java.

npm test

PanagiotisDrakatos avatar Mar 25 '23 09:03 PanagiotisDrakatos