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.

Testing with Jest not working

See original GitHub issue

I got

> spring2016@0.0.1 test /Users/keen/dev/work_native/spring2016
> jest

Using Jest CLI v0.10.0, jasmine2, babel-jest
 FAIL  __tests__/hackathon-test.js
● Runtime Error
TypeError: Cannot read property 'debugHosts' of undefined
    at Object.<anonymous> (/Users/keen/dev/work_native/spring2016/node_modules/realm/lib/browser/index.js:30:69)
    at Object.<anonymous> (/Users/keen/dev/work_native/spring2016/node_modules/realm/lib/index.js:29:18)
    at Object.<anonymous> (/Users/keen/dev/work_native/spring2016/services/hack-service.js:2:12)
    at Object.<anonymous> (/Users/keen/dev/work_native/spring2016/__tests__/hackathon-test.js:5:18)
npm ERR! Test failed.  See above for more details.

message.

My Package.json is shown below.

{
  "name": "spring2016",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node node_modules/react-native/local-cli/cli.js start",
    "test": "jest"
  },
  "dependencies": {
    "react-native": "^0.21.0",
    "react-native-action-button": "^1.1.3",
    "react-native-facebook-login": "^1.0.3",
    "react-native-image-picker": "^0.18.3",
    "react-native-list-popover": "^1.0.5",
    "react-native-material-design": "^0.3.3",
    "react-native-material-kit": "^0.3.0",
    "react-native-vector-icons": "^1.3.2",
    "realm": "^0.11.0"
  },
  "rnpm": {
    "ios": {
      "project": "ios/spring2016.xcodeproj"
    }
  },
  "jest": {
    "setupEnvScriptFile": "<rootDir>/node_modules/react-native/jestSupport/env.js",
    "testPathIgnorePatterns": [
      "/node_modules/",
      "packager/react-packager/src/Activity/"
    ],
    "testFileExtensions": [
      "js"
    ],
    "unmockedModulePathPatterns": [
      "promise",
      "source-map",
      "<rootDir>/node_modules/react-native",
      "<rootDir>/node_modules/realm"
    ]
  },
  "devDependencies": {
    "babel-jest": "*",
    "babel-preset-react-native": "^1.5.6",
    "jest-cli": "*",
    "react-native": "^0.21.0"
  }
}
<script src="https://gist.github.com/ehrudxo/684b5aeb7211c18237092aece45f29fb.js"></script>

https://gist.github.com/ehrudxo/684b5aeb7211c18237092aece45f29fb

and .babelrc is like

{ “presets”: [“react-native”] }

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:21 (6 by maintainers)

github_iconTop GitHub Comments

8reactions
hyb175commented, May 19, 2017

Thanks @hybrisCole for a basic mock. I created one for my use based on this. https://gist.github.com/hyb175/beb9ceed4c34300ba7c77d3d6d44ae52

5reactions
hybrisColecommented, Jan 6, 2017

I have this very basic realm mock

export default class Realm {
    schema: any = [];
    data: any = [];
    constructor(params: any) {
        require('lodash').each(params.schema, (schema) => {
            this.data[schema.name] = [];
            this.data[schema.name].filtered = () => {
                return this.data[schema.name];
            };
        });
        this.schema = params.schema;
    }
    objects(schemaName) {
        return this.data[schemaName];
    }
    write(fn) {
        fn();
    }
    create(schemaName, data) {
        this.data[schemaName].push(data);
        return data;
    }
};
jest.mock('realm', () => {
    return require('../../mocks/realm').default;
});

That .default is bc I’m on typescript heh, you should take it away if you go with es*

Read more comments on GitHub >

github_iconTop Results From Across the Web

Troubleshooting - Jest
Troubleshooting. Uh oh, something went wrong? Use this guide to resolve issues with Jest. Tests are Failing and You Don't Know Why​.
Read more >
npm test/ jest not working - Stack Overflow
Check if the npm package is installed successfully. If the installation is successful check if it is installed globally/locally.
Read more >
Troubleshooting · Jest
Use this guide to resolve issues with Jest. Tests are Failing and You Don't Know Why #. Try using the debugging support built...
Read more >
Tests not running · Issue #2219 · facebook/jest - GitHub
Hi @zigomir, this seems like an issue with npm . I've installed deps with yarn and tests are running. Try removing node_modules and...
Read more >
A guide to module mocking with Jest - Emma Goto
When writing Jest unit tests, I always struggle to remember the syntax for mocking modules. So this post is intended as a part-guide,...
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