UNPKG

9.16 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
126css:
127 baseUrl: /my-css
128 dir: ./assets/css
129```
130
131
132### CSS
133By 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:
134
135```yaml
136cssModules: .css
137```
138
139If 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:
140
141```yaml
142cssModules: .module.css
143```
144
145And if you wish to use several different extensions for [css-modules] you can specify a list:
146
147```yaml
148cssModules:
149 - .css
150 - .module.css
151```
152
153[css-loader]: https://github.com/webpack/css-loader
154[css-modules]: https://github.com/css-modules/css-modules
155
156## Building
157You 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.
158
159Declare a `build` section within the `.uiharness.yml` with the following fields:
160
161```yaml
162build:
163 prod: true # Minifies if true (default: false).
164 outputFolder: ./.build/my-folder
165
166 modules:
167 main: ./src/app.js
168 single:
169 - ./src/components/foo.jsx
170 - ./src/util/bar.js
171
172 vendor:
173 - react
174 - react-dom
175
176```
177
178##### Command Line
179The build function can be invoked from the command line. For example, you might add these as scripts to your `package.json`:
180
181```json
182 "scripts": {
183 "bundle": "node ./node_modules/ui-harness/build",
184 }
185```
186
187Producing the following output in the terminal window:
188
189![Terminal:Size](https://cloud.githubusercontent.com/assets/185555/12995230/c974f83a-d18a-11e5-914c-48589704df78.png)
190
191
192##### Building from the API
193You 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:
194
195```js
196import uiharness from 'ui-harness';
197
198uiharness.build({ settings }) // See YAML build settings above.
199 .then(result => { ... })
200 .catch(err => { ... });
201
202```
203
204
205## Environment
206When 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.
207
208```js
209if (__UIHARNESS__) {
210 describe('My visual spec', function() {
211 // This will load in the UIHarness
212 });
213}
214
215if (!__UIHARNESS__) {
216 describe('My unit-tests', function() {
217 // This will run within the server-side test runner.
218 });
219}
220```
221
222
223
224## Examples
225#### Simple Example
226From 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:
227
228 npm start
229
230#### Relay Example
231
232To see an example of Relay/GraphQL working within UIHarness run:
233
234 node example relay
235
236Based 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.
237
238```js
239uiharness.start({
240 entry: './example/relay/specs',
241 proxy: { '/graphql': 'http://localhost:8080' },
242 graphqlSchema: path.resolve('./example/relay/data/schema.js')
243})
244```
245
246From 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).
247
248To rebuild the schema (when changes have been made to it), simply delete the generated `schema.json` file and restart the UIHarness.
249
250
251# Links
252- [uiharness.com](http://uiharness.com)
253- [Documentation](https://philcockfield.gitbooks.io/ui-harness/content/index.html) ([edit](https://www.gitbook.com/book/philcockfield/ui-harness/edit))
254
255
256
257---
258### License: MIT