min-react-env
minimal browser mocks for testing react-dom code in node.js
Last updated 2 years ago by goto-bus-stop .
Apache-2.0 · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install min-react-env 
SYNC missed versions from official npm registry.

min-react-env

minimal browser mocks for testing react-dom code in node.js

It uses min-document with a few tiny addons, mocking just enough for react-dom to be able to run. This is much smaller than using a full JSDOM instance or similar. If you don't do fancy DOM things in your components, min-document could well be enough.

You could just use min-document, but react-dom depends on a few more things to be available on the window object, and this package encapsulates that.

Install - Usage - License: Apache-2.0

npm travis standard

Install

npm install --save-dev min-react-env

Usage

min-react-env exports window and document variables. Assign them to the global object:

import { window, document } from 'min-react-env'
import React from 'react'
import ReactDOM from 'react-dom'

global.window = window
global.document = document

// Assuming a test framework like Mocha or Jest
describe('My Tests', () => {
  const wrapper = document.createElement('main')
  ReactDOM.render(<MyComponent />, wrapper, () => {
    assert.strictEqual(
      wrapper.toString(),
      '<main><div class="MyComponent"></div></main>'
    )
  })
})

Now you can run your react-dom tests in Node!

If you use CommonJS (eg. you don't have a compile step), you can use this pattern:

const test = require('tape')
Object.assign(global, require('min-react-env'))

test('My Tests', (t) => {
  t.plan(1)
  const wrapper = document.createElement('main')
  ReactDOM.render(<MyComponent />, wrapper, () => {
    t.strictEqual(
      wrapper.toString(),
      '<main><div class="MyComponent"></div></main>'
    )
  })
})

The tests for this package actually do something very similar!

License

Apache-2.0

Current Tags

  • 1.0.1                                ...           latest (2 years ago)

2 Versions

  • 1.0.1                                ...           2 years ago
  • 1.0.0                                ...           2 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 2
Last Day 0
Last Week 1
Last Month 4
Dependencies (1)
Dev Dependencies (5)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |