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

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

Closed
rochejul opened this issue Oct 12, 2020 · 3 comments
Closed

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

rochejul opened this issue Oct 12, 2020 · 3 comments

Comments

@rochejul
Copy link
Contributor

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
Copy link
Member

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

@julien-reboul
Copy link

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
Copy link
Member

Fixed by #103.

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

3 participants