Giter Site home page Giter Site logo

Preact testing about testing-javascript HOT 3 CLOSED

fondberg avatar fondberg commented on July 2, 2024
Preact testing

from testing-javascript.

Comments (3)

larrybotha avatar larrybotha commented on July 2, 2024

ye there's definitely an advantage to using just dom-testing-library - take a look at all the additional helpers you get with react-testing-library:
https://github.com/testing-library/react-testing-library/blob/master/src/index.js#L60-L85 and
https://github.com/testing-library/react-testing-library/blob/master/src/index.js#L146-L147

react-testing-library is an extension of dom-testing-library, so you get all the goodness of one, with the benefits of the other.

The only thing to be aware of is that because react-testing-library isn't written for Preact there's always the risk that something may not work, or some API changes limiting how much you can use it.

At least one is able to write whatever helpers they want to address any issues - if say the render function stops working, you can always create and import your own render helper. The render function here can be moved to somewhere where you can easily imprt it into whichever test you want:

const render = ui => {
const container = document.createElement('div');
Preact.render(ui, container);
return {
...getQueriesForElement(container),
container,
};
};

I use this a ton in my projects; pretty much anything where you need a Provider you can write your own renderer, or even just a small wrapper that wraps your component inside a specific provider.

e.g.

// tests/utils/renderers.js
import {h} from 'preact';
import {render} from '@testing-library/react';

import {AuthProvider} from 'src/components/context/auth';

const withAuth = (ui, options = {}) => {
  return <AuthProvider value={options}>{ui}</AuthProvider>;
};

const renderWithAuth = (ui, options = {}) => {
  return render(withAuth(ui, options));
};

export {renderWithAuth, withAuth}
// index.test.js
...
import {render as renderTl} from '@testing-library/react';
import {renderWithAuth, withAuth} from 'utils/renderers.js';

const render = ui => {
  return renderWithAuth(ui);
}

test('-> renders', () => {
  const utils = render(<MyComponent />);
  // or
  const utilsAgain = renderTl(withAuth(<MyComponent />));

  // make assertions
})

By separating withAuth from renderWithAuth you can easily wrap multiple helpers for tests, e.g.:

const render = ui => {
  // Provide both auth and theme contexts to your component
  const renderWithAuth(withTheme(ui));
}

from testing-javascript.

fondberg avatar fondberg commented on July 2, 2024

Thanks for the thorough explanation. I've used the react testing library a lot but only with React so I guess I'll give it a shot with my preact project.

from testing-javascript.

larrybotha avatar larrybotha commented on July 2, 2024

Ye, it's pretty painless! The only thing I've come across to look out for is the asynchronous rendering.

Kick some ass!

from testing-javascript.

Related Issues (3)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.