json5 / json5-spec

The JSON5 Data Interchange Format
https://spec.json5.org
MIT License
49 stars 11 forks source link

Support for undefined for testing purposes #5

Closed silkentrance closed 5 years ago

silkentrance commented 6 years ago

With TypeScript, it is possible to denote a given property for being optional, e.g.

interface Node {

  parent?: Node;
  children: Node[];
}

Now, when creating an instance of Node

const n1 = { children: [] };

And running this through for example Mocha/Chai using a data driven test approach, with the following test data

{
  children: []
}

Will result in differences since chai.assert.deepEqual will now see a

{
  parent: undefined,
  children: []
}

and comparing that to the test data will yield said differences since the test data cannot contain any undefined properties.

A possible work around for this is to define the interface as such

interface Node {
  parent: Node | null;
  children: Node[];
}

In which case you can have augmented test data such as

{
  parent: null,
  children: []
}

which will then pass the chai.assert.deepEqual() assertion.

So, for the sake of completeness I propose that we can also have the 'undefined' in addition to '+Infinity', '-Inifinity', 'NaN' and 'null'.

What do you think?

aseemk commented 6 years ago

I hear you on this request.

Unfortunately, separate distinctions of undefined vs. null is mostly unique to JS. So if JSON5 supported undefined, it wouldn't translate well to many other languages.

Thus, I lean towards keeping undefined out of JSON5. Hopefully your workaround is workable!