yahoo / serialize-javascript

Serialize JavaScript to a superset of JSON that includes regular expressions and functions.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Line returns added in serialized function when running through jest

eslachance opened this issue · comments

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.

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.

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.