Git Product home page Git Product logo

thbland / dom-testing-library-with-anything Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kentcdodds/dom-testing-library-with-anything

0.0 1.0 0.0 923 KB

Use DOM Testing Library to test any JS framework on TestingJavaScript.com

Home Page: https://testingjavascript.com/playlists/use-dom-testing-library-to-test-any-js-framework-ae28

License: Other

JavaScript 82.46% TypeScript 16.53% Svelte 1.01%

dom-testing-library-with-anything's Introduction

Use DOM Testing Library to test any JS framework


Anything you can render to the DOM, you can test with DOM Testing Library.

This repo is a bunch of simple examples of using DOM Testing Library to test a Counter component in various frameworks. If your framework of choice is not listed here, please add it!

Contributing

NOTE: In the interest of full disclosure, this repository will be used by me to create a course on testing for which I will be paid.

The prime example is this react version:

// adds handy assertions we'll use
import '@testing-library/jest-dom/extend-expect'

// framework imports
import * as React from 'react'
import ReactDOM from 'react-dom'

// DOM Testing Library utilities
// note: if your framework does not apply updates to the DOM synchronously
// then you can use the userEventAsync export in ./user-event-async.js
// see hyperapp.test.js for an example of this.
import {getQueriesForElement} from '@testing-library/dom'
import userEvent from '@testing-library/user-event'

// the component in your framework
function Counter() {
  const [count, setCount] = React.useState(0)
  const increment = () => setCount(c => c + 1)
  return (
    <div>
      <button onClick={increment}>{count}</button>
    </div>
  )
}

// a generic "render" method that you could use for any component for
// your framework
function render(ui) {
  const container = document.createElement('div')
  document.body.appendChild(container)
  ReactDOM.render(ui, container)
  return {
    container,
    ...getQueriesForElement(container),
  }
}

// the test.
// This test _should_ look almost identical between each framework
// that's the idea that I'm trying to get across in this repo!
test('renders a counter', () => {
  const {getByText} = render(<Counter />)
  const counter = getByText('0')
  userEvent.click(counter)
  expect(counter).toHaveTextContent('1')

  userEvent.click(counter)
  expect(counter).toHaveTextContent('2')
})

If you can make your example resemble that, I would be thrilled :)

IMPORTANT Notes

I want to keep things as simple as possible, but I also want to be true to what's typical for a given framework. If your framework strongly encourages the use of TypeScript for example, then please feel free to use TypeScript (Jest should already be configured to pick it up properly).

If Jest is not the testing framework of choice for your web framework, I'd still prefer to stick with Jest. Hopefully it shouldn't make much of a difference for the test itself.

Try really hard to keep everything in a single file, even if that means authoring your component in a slightly non-typical way.

This project is setup with prettier, husky, and lint-staged. That means that when you commit, a git commit hook will automatically format the files you're changing and run the tests relevant to those files. Neat right?

LICENSE

MIT

dom-testing-library-with-anything's People

Contributors

agubler avatar ajcrites avatar aprillion avatar ashsearle avatar danjordan avatar jennings avatar karthikiyengar avatar kentcdodds avatar michaeldeboey avatar mini-eggs avatar navimarella avatar schuchard avatar

Watchers

 avatar

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.