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

Line returns added in serialized function when running through jest #103

Open
eslachance opened this issue Oct 11, 2020 · 2 comments
Open

Comments

@eslachance
Copy link

I always get the weirdest issues. So, serialize-javascript works fine in actual production, but it somehow outputs a different thing while running a test. In the example of the object with all the types including the function, I get an odd line return within the serialized function.

Environment:

  • Node 14.13.0
  • Jest 26.5.2
  • serialize-javascript 5.0.1

Reproduction:
(Alternatively, see https://gist.github.com/eslachance/6dbd88820f09de4509d4f19c4c0e7ad6 for reproduction)

  • Create a jest test
  • import serialize-javascript
  • Add a console.log of the serialized example.
test("serializes javascript", () => {
  console.log(serialize({
    str  : 'string',
    num  : 0,
    obj  : {foo: 'foo'},
    arr  : [1, 2, 3],
    bool : true,
    nil  : null,
    undef: undefined,
    inf  : Infinity,
    date : new Date("Thu, 28 Apr 2016 22:02:17 GMT"),
    map  : new Map([['hello', 'world']]),
    set  : new Set([123, 456]),
    fn   : function echo(arg) { return arg; },
    re   : /([^\s]+)/g,
    big  : BigInt(10),
  }))
});

Expected result:

{"str":"string","num":0,"obj":{"foo":"foo"},"arr":[1,2,3],"bool":true,"nil":null,"undef":undefined,"inf":Infinity,"date":new Date("2016-04-28T22:02:17.000Z"),"map":new Map([["hello","world"]]),"set":new Set([123,456]),"fn":function echo(arg) { return arg; },"re":new RegExp("([^\\s]+)", "g"),"big":BigInt("10")}

Actual Result:

    {"str":"string","num":0,"obj":{"foo":"foo"},"arr":[1,2,3],"bool":true,"nil":null,"undef":undefined,"inf":Infinity,"date":new Date("2016-04-28T22:02:17.000Z"),"map":new Map([["hello","world"]]),"set":new Set([123,456]),"fn":function echo(arg) {
          return arg;
        },"re":new RegExp("([^\\s]+)", "g"),"big":BigInt("10")}

The expected result was a simple nodejs file which outputs the serialized data and nothing else.

This is confusing me very much, and I don't know where to begin in troubleshooting it.

@papb
Copy link

papb commented Oct 11, 2020

Hello, I watch several random repositories and I have seen some odd issues involving things working in production but failing within jest... My guess is that jest is taking your source code and messing with it somehow before running the test, in a way that usually does not make a difference... Of course adding newlines to a function definition like this only makes a difference if you are stringifying the function content (which is extremely rare), so jest developers probably didn't even think about it.

The above is just a guess...

I don't know where to begin in troubleshooting it.

I suggest using another testing library to see if the problem persists. I like ava. This would be a good step to decide whether or not jest is the culprit.

@eslachance
Copy link
Author

I am, however, not convinced this is only jest-related, however.
https://repl.it/@evie_codes/BogusTrustworthyBases#index.js for another way to reproduce this. The data is being sent to repl.it's database and returns with the exact same line returns. I don't know what causes this but jest doesn't seem to be the only thing.

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

No branches or pull requests

2 participants