Skip to content
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

requiring json - odd behaviour #238

Closed
gnesher opened this issue Feb 4, 2015 · 8 comments
Closed

requiring json - odd behaviour #238

gnesher opened this issue Feb 4, 2015 · 8 comments

Comments

@gnesher
Copy link

gnesher commented Feb 4, 2015

I've just started using Jest and was requiring a JSON file with fake data to use in the tests and encountered the following behaviour:

If I don't use jest.dontMock the JSON file is imported, but boolean variables get removed.

Mind you, I should have used jest.dontMock to begin with.
Having said that I'm not sure why Jest would return all the properties except the boolean ones.

@dangerbell
Copy link

I just noticed this as well. Very confusing.

Here's a sample file and a jest test to show the behaviour:

SimpleObject.js:

module.exports = {
  "boolean": true,
  "stringly": "string",
  "numberly": 23
}

__ tests __/test.js

describe('Boolean Property Test', function() {
  it('should mock boolean object properties', function() {
    expect(require("../SimpleObject")).toEqual({
      "boolean": true,
      "stringly": "string",
      "numberly": 23
    });
  });
});

@arnihermann
Copy link

I've noticed this as well.

@isay-sosa
Copy link

I'm having the same issue with arrays

// user.json
{
  "user": {
    "id": 3,
    "comments": [
      {
        "id": 1,
        "body": "My Comment"
      }
    ]
  }
}

After requiring it:

// require('user.json')
{
  "user": {
    "id": 3,
    "comments": []
  }
}

@cmmcleod
Copy link

I had the same issue when importing JSON fixtures. However requireActual seems to do the trick:

var data = require.requireActual('./fixtures/some_data');

@cpojer
Copy link
Member

cpojer commented Feb 17, 2016

This will be fixed by #713 in 0.9.0 so I'm closing this issue out.

@cpojer cpojer closed this as completed Feb 17, 2016
@mgenev
Copy link

mgenev commented May 26, 2016

Mocking arrays is also broken, they come out empty with jest

@cpojer
Copy link
Member

cpojer commented Jun 1, 2016

No it is not. Mocking an array will give you an empty array. If that is not what you expect, you can explicitly define it or unmock it.

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants