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

TypeError: dompurify_1.default.sanitize is not a function #353

Closed
marcelo-umg opened this issue Aug 5, 2019 · 7 comments
Closed

TypeError: dompurify_1.default.sanitize is not a function #353

marcelo-umg opened this issue Aug 5, 2019 · 7 comments

Comments

@marcelo-umg
Copy link

I am trying to run my tests and I am getting an error

 FAIL  src/components/articles/ArticleTeaser.test.tsx (5.781s)
  ArticleTeaser
     renders null without article (6ms)
     renders correctly with article (10ms)
     todo is clickable

   ArticleTeaser  renders correctly with article

    TypeError: dompurify_1.default.sanitize is not a function

      21 |         <div className='content-teaser__date content-teaser__date--article'>{formatDate(article.created)}</div>
      22 |         <p className='content-teaser__summary--article'>
    > 23 |           <span dangerouslySetInnerHTML={{ __html: createDOMPurify.sanitize(article.body) }} />
         |                                                                    ^
      24 |         </p>
      25 |       </Link>
      26 |     </div>

This is the test:

import React from 'react';
import ArticleTeaser from './ArticleTeaser';
import { dummyArticle } from '../../shared/models/Article.model';
import { shallowRender } from '../../shared/services/testHelper';

describe('ArticleTeaser', () => {
  it('renders null without article', () => {
    const tree = shallowRender(<ArticleTeaser />);
    expect(tree).toBe(null);
  });

  it('renders correctly with article', () => {
    const tree = shallowRender(<ArticleTeaser article={dummyArticle} />);
    expect(tree).toMatchSnapshot();
  });

  it.todo('is clickable');
});

And the component:

import React, { FC } from 'react';
import createDOMPurify from 'dompurify';
import { Link } from 'react-router-dom';
import Article from '../../shared/models/Article.model';
import { formatDate } from '../../shared/services/date';

interface Props {
  article?: Article;
}

const ArticleTeaser: FC<Props> = props => {
  const { article } = props;

  if (!article) return null;

  return (
    <div className='content-teaser content-teaser--article'>
      <Link to={`/article/${article.id}`}>
        <p className='content-teaser__summary--article'>
          <span dangerouslySetInnerHTML={{ __html: createDOMPurify.sanitize(article.body) }} />
        </p>
      </Link>
    </div>
  );
};

export default ArticleTeaser;

The import import createDOMPurify from 'dompurify'; I already tried with

import DOMPurify from 'dompurify'; or import { sanitize } from 'dompurify'; and I am still getting the same error.

Versions:

"@types/dompurify": "0.0.33",
"dompurify": "^1.0.11",
"react": "^16.8.6",

So what could I be missing?

@cure53
Copy link
Owner

cure53 commented Aug 6, 2019

I have no idea :D @tdeekens any clue?

@tdeekens
Copy link
Collaborator

tdeekens commented Aug 6, 2019

Isn‘t this invocation wrong? createDOMPurify.sanitize(article.body)? You need to invoke the factory?

@ChristopherGillis
Copy link

@tdeekens Can you give an example? Dompurify works in the browser, but fails in jest.

@tdeekens
Copy link
Collaborator

tdeekens commented Aug 9, 2019

Yes cause I think in your test you are not invoking the createDOMPurify function.

You need to

const DOMPurify = createDOMPurify(window);

const clean = DOMPurify.sanitize(dirty);

@ChristopherGillis
Copy link

As far as I'm aware, Jest doesn't have access to "window", so this would still fail?

@ChristopherGillis
Copy link

Ahh, found the solution here: #29 (comment)

I would suggest that this ^ should probably be in the official docs/examples.

Thanks @tdeekens!

@cure53
Copy link
Owner

cure53 commented Aug 11, 2019

@ChristopherGillis PRs are welcome :)

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

4 participants