UNPKG

9.12 kBMarkdownView Raw
1# ui-harness
2[![Build Status](https://travis-ci.org/philcockfield/ui-harness.svg?branch=master)](https://travis-ci.org/philcockfield/ui-harness)
3[![npm version](https://badge.fury.io/js/ui-harness.svg)](https://badge.fury.io/js/ui-harness)
4[![js-style](https://img.shields.io/badge/code%20style-airbnb-brightgreen.svg?style=flat)](https://github.com/airbnb/javascript)
5
6Isolate, test and document modular UI with React using familiar `describe/it` testing semantics.
7
8http://uiharness.com
9
10![ScreenShot](https://cloud.githubusercontent.com/assets/185555/10448258/0471dece-71e8-11e5-983a-028dd7df7a1a.png)
11
12
13
14
15## Quick Start (1-minute)
16With the UIHarness you can go from an empty NPM module, to cleanly building isolated React components using best-practices in well under a minute.
17
18See the quick-start sample repo https://github.com/philcockfield/ui-harness-sample
19
20 git clone https://github.com/philcockfield/ui-harness-sample.git
21 cd ui-harness-sample
22 npm install
23 npm start
24
25
26
27
28## Manual Setup
29The UIHarness is just a simple NPM module.
30
31 npm install --save-dev ui-harness
32
33Simply add it to you `package.json` file, with a `start` script:
34
35```json
36{
37 "name": "my-components",
38 "version": "1.0.0",
39 "scripts": {
40 "uih": "node ./node_modules/ui-harness/start --entry=./src/specs",
41 },
42 "devDependencies": {
43 "ui-harness": "^3.3.0"
44 },
45 "engines": { "node": ">=5.5.0" }
46}
47```
48
49If you are using TypeScript, you will also need to ensure you have type definitions for your dependencies, such as React and React-DOM.
50
51From here you can start developing your React components. All the core dependencies for `react` and `babel` transpiling are made available to your module by virtue of the one `ui-harness` dependency.
52
53Now simply run:
54
55 npm run uih
56
57And navigate your browser to `http://localhost:3030`
58
59#### Project Structure
60The `package.json` above assumes a project structure like this:
61
62 my-components
63 |— src
64 |— components # React components here.
65 |— specs # Spec files here.
66 |— index.js # --entry to the "describe/it" visual spec files.
67
68For a working example see the [ui-harness-sample](https://github.com/philcockfield/ui-harness-sample) repo. To deviate from this typical structure simply organize your module folders as you wish and change the `--entry` path passed to the start script.
69
70------
71
72
73## Conceptual Introduction
74The [UIHarness](http://uiharness.com) allows you to add a one-line startup script to your node module that provides a complete visual test and build harness for creating and bundling your components.
75
76#### Creating
77As a tool for crafting your components and their API's in isolation, the UIHarness dramatically improves the quality and reuse potential of your UI investment. You will be falling into the "pit of success" conforming to best-practices that also make your job faster and easier, and importantly - **more fun**.
78
79#### Documentation
80Creating components in this manner has the knock-on effect of providing a visual API and documentation explorer for your component system. Sharing and socializing code and API's, both within your team, and publicly to the world, is a big part of making software a success. Functional documentation is a by-product of building components in the UIHarness.
81
82#### Publishing
83If you are isolating your UI components into their own discreet modules (a very good idea!), you can add the UIHarness as a dev-dependency (`--save-dev`), create and test your components within it, and then [publish](https://docs.npmjs.com/cli/publish) your module to NPM with the UIHarness as the startup option (`npm start`).
84
85This makes your components easy to explore, evaluate, and understand by other developers. The UIHarness adds no run-time overhead to your module, as consumers of the components will only be requiring the isolated component, not the UIHarness itself, so it never gets built (via [Webpack](https://webpack.github.io/)) into the resulting application bundle.
86
87
88## Startup Arguments and Configuration
89The following arguments can be passed to the UIHarness at startup as command-line arguments:
90
91- `--entry` Path to the specs files (comma separated if more than one).
92
93- `--port` The port to run the harness on. Default: `3030`
94
95For example:
96
97```json
98"scripts": {
99 "start": "node ./node_modules/ui-harness/start --entry=./src/specs --port=1234"
100}
101```
102
103
104#### .uiharness.yml
105These values can alternatively be declared in a `.uiharness.yml` configuration file in the root of your project, with the following additional values that can only be declared within a YAML configuration:
106
107- `graphqlSchema`
108A path to the [GraphQL](https://facebook.github.io/graphql/) `schema.js` file. If not specified [Relay](https://facebook.github.io/relay/) will not be enabled.
109
110- `proxy` An object containing `{ path, host }` mappings to proxy server requests to ([reference](https://webpack.github.io/docs/webpack-dev-server.html#proxy)).
111
112- `images` An object containing `{ baseUrl, dir }` that declares where images are served from. Default image values are:
113 - `baseUrl: <package-name>`
114 - `dir: ./images`
115
116
117```yml
118entry: ./src/specs # Path, comma-separated paths, or array of paths.
119port: 3030
120graphqlSchema: ./data/schema.js
121proxy:
122 /graphql: http://localhost:8080
123images:
124 baseUrl: /my-images
125 dir: ./assets/images
126```
127
128
129### CSS
130By default the UIHarness supports the webpack [css-loader] for `*.css` files. If however you wish to use [css-modules] simply declare the file extension of your modules in the `.uiharness.yml` with a regular-expression like this:
131
132```yaml
133cssModules: .css
134```
135
136If you wish to retain the default [css-loader] behavior but still want to use [css-modules], you can specify [css-modules] to only work on certain extensions:
137
138```yaml
139cssModules: .module.css
140```
141
142And if you wish to use several different extensions for [css-modules] you can specify a list:
143
144```yaml
145cssModules:
146 - .css
147 - .module.css
148```
149
150[css-loader]: https://github.com/webpack/css-loader
151[css-modules]: https://github.com/css-modules/css-modules
152
153## Building
154You can use the UIHarness to output your built JS bundles. You can also use this function to keep an eye on the size of your JS before it's too late to do anything about it.
155
156Declare a `build` section within the `.uiharness.yml` with the following fields:
157
158```yaml
159build:
160 prod: true # Minifies if true (default: false).
161 outputFolder: ./.build/my-folder
162
163 modules:
164 main: ./src/app.js
165 single:
166 - ./src/components/foo.jsx
167 - ./src/util/bar.js
168
169 vendor:
170 - react
171 - react-dom
172
173```
174
175##### Command Line
176The build function can be invoked from the command line. For example, you might add these as scripts to your `package.json`:
177
178```json
179 "scripts": {
180 "bundle": "node ./node_modules/ui-harness/build",
181 }
182```
183
184Producing the following output in the terminal window:
185
186![Terminal:Size](https://cloud.githubusercontent.com/assets/185555/12995230/c974f83a-d18a-11e5-914c-48589704df78.png)
187
188
189##### Building from the API
190You can invoke a build via the API by passing an object of the same structure as the `build` settings within `.uiharness.yml` to the build function:
191
192```js
193import uiharness from 'ui-harness';
194
195uiharness.build({ settings }) // See YAML build settings above.
196 .then(result => { ... })
197 .catch(err => { ... });
198
199```
200
201
202## Environment
203When running the `__UIHARNESS__` environment variable is set to `true`. Use this as a switch if you wish to consolidate UIHarness specs with unit-tests such as Mocha or Jasmine, or BDD feature/step files.
204
205```js
206if (__UIHARNESS__) {
207 describe('My visual spec', function() {
208 // This will load in the UIHarness
209 });
210}
211
212if (!__UIHARNESS__) {
213 describe('My unit-tests', function() {
214 // This will run within the server-side test runner.
215 });
216}
217```
218
219
220
221## Examples
222#### Simple Example
223From within the `/ui-harness` project folder, to see the core set of specs used to build the UIHarness itself, along with specs for associated component libraries, run:
224
225 npm start
226
227#### Relay Example
228
229To see an example of Relay/GraphQL working within UIHarness run:
230
231 node example relay
232
233Based on the [relay-starter-kit](https://github.com/relayjs/relay-starter-kit), this takes a command-line argument of `--graphqlSchema`, which is the path to the GraphQL `schema.js` file.
234
235```js
236uiharness.start({
237 entry: './example/relay/specs',
238 proxy: { '/graphql': 'http://localhost:8080' },
239 graphqlSchema: path.resolve('./example/relay/data/schema.js')
240})
241```
242
243From there the UIHarness will build the `schema.json` output, and compile it into the Webpack output sent to the client using the [babel-relay-plugin](https://www.npmjs.com/package/babel-relay-plugin).
244
245To rebuild the schema (when changes have been made to it), simply delete the generated `schema.json` file and restart the UIHarness.
246
247
248# Links
249- [uiharness.com](http://uiharness.com)
250- [Documentation](https://philcockfield.gitbooks.io/ui-harness/content/index.html) ([edit](https://www.gitbook.com/book/philcockfield/ui-harness/edit))
251
252
253
254---
255### License: MIT