Skip to content

Commit

Permalink
minor: Add stdMock(), stdRestore() and give full mono context
Browse files Browse the repository at this point in the history
  • Loading branch information
Atinux committed Oct 20, 2017
1 parent db3c66a commit 2cfb7dc
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 29 deletions.
75 changes: 54 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,26 @@ npm install --save-dev mono-test-utils
## Utils

```js
const { start, stop, url, $get, $post, $put, $del } = require('mono-test-utils')
const {
start, stop,
url,
mockStd, restoreStd
$get, $post, $put, $del
} = require('mono-test-utils')
```

Start a Mono project from `dir` directory with `NODE_ENV=test`:
## Contents

- [Start & Stop Mono]()
- [Get Mono url]()
- [Mock stdout and stderr]()
- [Make API calls]()

### Start a Mono project from `dir` directory with `NODE_ENV=test`:

```js
const { app, server, conf } = await start(dir, options = {})
const context = await start(dir, options = {})
// context = { log, conf, app, server, stdout, stderr }
```

Default `options`:
Expand All @@ -34,22 +47,42 @@ Default `options`:
}
```

Stop Mono server:
### Stop Mono server:

```js
await stop(server)
await stop(context.server)
```

Get the url of your mono server:
### Get the url of your mono server:

```js
// return http://localhost:8000 (port updates depending of your mono conf
url()
// return http://localhost:8000/test
// http://localhost:8000 (port updates depending of the mono conf)
url('/test')
// http://localhost:8000/test
```
### Mock `stdout` and `stderr`

This is useful for capturing logs (based on [std-mocks](https://github.com/neoziro/std-mocks)).

Example:

```js
// Start mocking stdout and stderr
stdMock()

console.log('test log')
console.error('test error')

const { stdout, stderr } = stdRestore()
// stdout = ['test log\n']
// stderr = ['test error\n']
```

Make HTTP requests to the API:
- `stdMock()` will call `stdMocks.use(options)`
- `stdRestore()` will call `stdMocks.restore()` and return `stdMocks.flush()`.

### Make HTTP requests to the API:

```js
await $get(path, options = {})
Expand Down Expand Up @@ -84,32 +117,32 @@ const { join } = require('path')

const { start, stop, $get, $post } = require('mono-test-utils')

let ctx
let context

// Start server
test.before('Start Mono app', async (t) => {
ctx = await start(join(__dirname, 'fixtures/example/'))
context = await start(join(__dirname, 'fixtures/example/'))
})

// Test API Endpoints
test('Call GET - /example', async (t) => {
const { stdout, stderr, statusCode, body } = await $get('/example')
t.true(stdout[0].includes('GET /example'))
t.is(stderr.length, 0)
t.is(statusCode, 200)
const { stdout, stderr, statusCode, body } = await $get('/example')
t.true(stdout[0].includes('GET /example'))
t.is(stderr.length, 0)
t.is(statusCode, 200)
 // Imagine that GET - /example returns { hello: 'world' }
t.deepEqual(body.body, { hello: 'world' })
t.deepEqual(body.body, { hello: 'world' })
})

test('Call POST - /example', async (t) => {
const { statusCode, body } = await $post('/example', {
body: { foo: 'bar' }
})
t.is(statusCode, 200)
const { statusCode, body } = await $post('/example', {
body: { foo: 'bar' }
})
t.is(statusCode, 200)
})

// Close server
test.after('Close Mono server', async (t) => {
await close(ctx.server)
await close(context.server)
})
```
22 changes: 14 additions & 8 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ const stdMocks = require('std-mocks')

let port = 8000

const stdMock = exports.stdMock = () => stdMocks.use()

const stdRestore = exports.stdRestore = () => {
stdMocks.restore()
return stdMocks.flush()
}

exports.start = async (dir, options) => {
// Options
options = options || {}
Expand All @@ -15,21 +22,22 @@ exports.start = async (dir, options) => {
process.env.NODE_ENV = options.env
const mono = require(options.monoPath)
// Start mono
let ctx
let context
try {
ctx = await mono(dir)
context = await mono(dir)
} catch (err) {
const { stdout, stderr } = stdMocks.flush()
err.stdout = stdout
err.stderr = stderr
stdMocks.restore()
throw err
}
port = ctx.conf.mono.http.port
port = context.conf.mono.http.port
// Flush logs output
const { stdout, stderr } = stdMocks.flush()
stdMocks.restore()
return { conf: ctx.conf, app: ctx.app, server: ctx.server, stdout, stderr }
// Mono returns { log, conf, appDir, app, server }
return Object.assign({}, context, { stdout, stderr })
}

exports.stop = (server) => {
Expand Down Expand Up @@ -83,7 +91,7 @@ exports.$delete = exports.$del = (path, options) => {

const wrapLogs = async (apiCall) => {
// Store logs output
stdMocks.use()
stdMock()
// Call API & check response
let res = null
let err = null
Expand All @@ -93,9 +101,7 @@ const wrapLogs = async (apiCall) => {
err = error
}
// Get logs ouput & check logs
const { stdout, stderr } = stdMocks.flush()
// Restore logs output
stdMocks.restore()
const { stdout, stderr } = stdRestore()
// Return err, res and output
const body = (err ? err.response.body : res.body)
const statusCode = (err ? err.statusCode : res.statusCode)
Expand Down
12 changes: 12 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ test('url("/test")', async (t) => {
t.is(url, 'http://localhost:5678/test')
})

/*
** stdMock() and stdRestore()
*/
test('stdMock() and stdRestore()', async (t) => {
utils.stdMock()
console.log('log test') // eslint-disable-line no-console
console.error('log error') // eslint-disable-line no-console
const { stdout, stderr } = utils.stdRestore()
t.deepEqual(stdout, ['log test\n'])
t.deepEqual(stderr, ['log error\n'])
})

/*
** Test API calls
*/
Expand Down

0 comments on commit 2cfb7dc

Please sign in to comment.