salesforce / lwc-test

LWC plugins and utilities for testing
MIT License
46 stars 31 forks source link

@lwc/jest-serializer: fails on null value #102

Closed rochejul closed 4 years ago

rochejul commented 4 years ago

Hi

When we want to use "toMatchSnapshot" with LWC and Jest, we could have the following error (when some properties of the JSON have got a null value):

PrettyFormatPluginError: Cannot destructure property `nodeType` of 'undefined' or 'null'.TypeError: Cannot destructure property `nodeType` of 'undefined' or 'null'.

at Object.test (node_modules/@lwc/jest-serializer/src/index.js:12:14)
      at __EXTERNAL_MATCHER_TRAP__ (node_modules/expect/build/index.js:342:30)
      at Object.expect [as toMatchSnapshot] (node_modules/expect/build/index.js:343:15)
      at it.each (src/main/modules/search_lightning/searchResultsAuraAdapter/__tests__/modelMapper.test.js:88:13)

When we look on the code, we have:

function test({ nodeType } = {}) {
    return (
        nodeType &&
        (nodeType === 1 || // element
        nodeType === 3 || // text
            nodeType === 6) // comment
    );
}

According to ES6, the default value is applied only if the parameter is undefined. If we have a null value, it will not be applied.

Then, we should made instead:

function test(node) {
    const nodeType = node && node.nodeType || undefined;
    return (
        nodeType &&
        (nodeType === 1 || // element
        nodeType === 3 || // text
            nodeType === 6) // comment
    );
}

Should I create a PR?

Many thanks

Regards

pmdartus commented 4 years ago

Thanks for reporting the issue @julien-reboul. Feel free to open a PR if you have the code ready to go.

julien-reboul commented 4 years ago

Thanks for reporting the issue @julien-reboul. Feel free to open a PR if you have the code ready to go.

@pmdartus you meant @rochejul 😄

pmdartus commented 4 years ago

Fixed by #103.