question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot load native module when running under jest

See original GitHub issue

🐛 Bug Report

I can load a native module in a regular script just fine, but I cannot do the same in a spec being executed by jest.

To Reproduce

Steps to reproduce the behavior:

Install native module like this:

$ npm config set @sap:registry https://npm.sap.com
$ npm install @sap/hana-client

Reproducible example:

$ cat package.json
{
  "name": "foo",
  "version": "1.0.0",
  "description": "",
  "main": "foobar.spec.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@sap/hana-client": "^2.3.123",
    "jest": "^23.6.0"
  }
}
$ cat foobar.spec.js
require("@sap/hana-client");
$ node -v
v8.12.0
$ DEBUG=*
$ node foobar.spec.js
  @sap/hana-client:index Starting index.js +0ms
  @sap/hana-client:index Attempting to load Hana node-hdbcapi driver +3ms
  @sap/hana-client:index ... Trying user-built copy... +0ms
  @sap/hana-client:index ... Looking for user-built copy in /Users/else/code/foo/node_modules/@sap/hana-client/build/Release/hana-client.node ...  +0ms
  @sap/hana-client:index Not found. +0ms
  @sap/hana-client:index ... Trying prebuilt copy... +0ms
  @sap/hana-client:index ... Looking for prebuilt copy in /Users/else/code/foo/node_modules/@sap/hana-client/prebuilt/darwinintel64-xcode7/hana-client_v8.node ...  +0ms
  @sap/hana-client:index Loaded. +50ms
  @sap/hana-client:index Success. +0ms
$ echo $?
0
$ node_modules/.bin/jest --runInBand
  @sap/hana-client:index Starting index.js +0ms
  @sap/hana-client:index Attempting to load Hana node-hdbcapi driver +1ms
  @sap/hana-client:index ... Trying user-built copy... +0ms
  @sap/hana-client:index ... Looking for user-built copy in /Users/else/code/foo/node_modules/@sap/hana-client/build/Release/hana-client.node ...  +0ms
  @sap/hana-client:index Not found. +0ms
  @sap/hana-client:index ... Trying prebuilt copy... +0ms
  @sap/hana-client:index ... Looking for prebuilt copy in /Users/else/code/foo/node_modules/@sap/hana-client/prebuilt/darwinintel64-xcode7/hana-client_v8.node ...  +0ms
  @sap/hana-client:index Failed to load DBCAPI. +2ms
  @sap/hana-client:index Could not load: Prebuilt copy did not satisfy requirements. +0ms
  @sap/hana-client:index Could not load modules for Platform: 'darwin', Process Arch: 'x64', and Version: 'v8.12.0' +0ms
 FAIL  ./foobar.spec.js
  ● Test suite failed to run

    Failed to load DBCAPI.

      at /Users/else/code/foo/node_modules/jest-runtime/build/index.js.requireModule (../node_modules/jest-runtime/build/index.js:372:31)
      at /Users/else/code/foo/node_modules/@sap/hana-client/lib/index.js.Object.<anonymous> (node_modules/@sap/hana-client/lib/index.js:127:14)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        1.022s
Ran all test suites.

Expected behavior

No error related to module loading

Link to repl or repo (highly encouraged)

https://transfer.sh/(/JOeLK/example.tar.gz).tar.gz

Run npx envinfo --preset jest

Paste the results here:

$ npx envinfo --preset jest
npx: installed 1 in 3.457s

  System:
    OS: macOS 10.14
    CPU: (4) x64 Intel(R) Core(TM) i7-5557U CPU @ 3.10GHz
  Binaries:
    Node: 8.12.0 - ~/.nvm/versions/node/v8.12.0/bin/node
    Yarn: 1.10.1 - ~/.nvm/versions/node/v8.12.0/bin/yarn
    npm: 6.4.1 - ~/.nvm/versions/node/v8.12.0/bin/npm
  npmPackages:
    jest: ^23.6.0 => 23.6.0

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:23 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
FourSpotProjectcommented, May 16, 2019

We were having the same issues combining the SAP Hana client library with Jest.

Setting environment variables was no solution for us, since we are working on different systems (Windows, Linux, Mac). The following steps solved it for us:

  1. set the global setup parameter in the jest config file:
globalSetup: "<rootDir>/sapHana.config.js",
  1. create a config file (in this example: sapHana.config.js in the root folder) which sets the environment variable:
var path = require('path');

module.exports = async () => {
    var extensions = {
        'darwin': 'dylib',
        'linux': 'so',
        'win32': 'dll'
    };
    
    // Look for prebuilt binary and DBCAPI based on platform
    var pb_subdir = null;
    if (process.platform === 'linux') {
        if (process.arch === 'x64') {
            pb_subdir = 'linuxx86_64-gcc48';
        } else if (process.arch.toLowerCase().indexOf('ppc') != -1 && os.endianness() === 'LE') {
            pb_subdir = 'linuxppc64le-gcc48';
        } else {
            pb_subdir = 'linuxppc64-gcc48';
        }
    } else if (process.platform === 'win32') {
        pb_subdir = 'ntamd64-msvc2010';
    } else if (process.platform === 'darwin') {
        pb_subdir = 'darwinintel64-xcode7';
    }
    
    var modpath = path.dirname(require.resolve("@sap/hana-client/README.md"));
    var pb_path = path.join(modpath, 'prebuilt', pb_subdir);
    var dbcapi = process.env['DBCAPI_API_DLL'] || path.join(pb_path, 'libdbcapiHDB.' + extensions[process.platform]);
    
    process.env['DBCAPI_API_DLL'] = dbcapi;
  };

Please note that this was tested with version 2.4.126 of @sap/hana-client and things might change with newer versions.

2reactions
elsbrockcommented, May 18, 2019

Thanks for sharing, IMO it’s much easier though to just require("@sap/hana-client/build.js"); in your jest config.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest: Error when trying to import Native Modules - Stack Overflow
I found the solution to this. I had been worried that perhaps the mocks weren't being tracked through all the import statement.
Read more >
Testing React Native Apps - Jest
Mock native modules using jest.mock. The Jest preset built into react-native comes with a few default mocks that are applied on a react-native...
Read more >
Testing React Native Apps · Jest
We recommend inspecting the native module's source code and logging the module when running a react native app on a real device and...
Read more >
Mocking RN modules | React Made Native Easy
i have introduced a native module called 'react-native-gps-state' in my personal project,but after running tests suits i'm getting error of "Invarient Voilation ...
Read more >
Testing React Native Apps - Jest - w3resource
All you need to do to run tests in Jest is to run yarn test. ... In other cases, you may like to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found