Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

Commit

Permalink
feat: stdin
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Jun 14, 2018
1 parent f63acda commit 2d34b74
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 33 deletions.
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ extendable utilities for testing
* [Finally](#finally)
* [Nock](#nock)
* [Environment Variables](#environment-variables)
* [Run](#run)
* [Do](#do)
* [Add](#add)
* [Stdin Mocking](#stdin-mocking)
* [Stdout/Stderr Mocking](#stdoutstderr-mocking)
* [Done](#done)
* [Retries](#retries)
Expand Down Expand Up @@ -246,6 +247,25 @@ describe('add', () => {
})
```

Stdin Mocking
-------------

Mocks stdin. You may have to pass a delay to have it wait a bit until it sends the event.

```js
describe('stdin test', () => {
fancy
.stdin('whoa there!\n')
.stdout()
.it('mocks', () => {
process.stdin.setEncoding('utf8')
process.stdin.once('data', data => {
// data === 'whoa there!\n'
})
})
})
```

Stdout/Stderr Mocking
---------------------

Expand Down
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
"author": "Jeff Dickey @jdxcode",
"bugs": "https://github.com/jdxcode/fancy-test/issues",
"dependencies": {
"@types/chai": "^4.1.3",
"@types/chai": "^4.1.4",
"@types/lodash": "^4.14.109",
"@types/mocha": "^5.2.0",
"@types/mocha": "^5.2.2",
"@types/nock": "^9.1.3",
"@types/node": "^10.3.0",
"@types/node": "^10.3.3",
"@types/sinon": "^5.0.1",
"lodash": "^4.17.10",
"mock-stdin": "^0.3.1",
"stdout-stderr": "^0.1.9"
},
"devDependencies": {
Expand All @@ -20,12 +21,12 @@
"chalk": "^2.4.1",
"http-call": "^5.1.4",
"mocha": "^5.2.0",
"nock": "^9.3.0",
"sinon": "^5.0.10",
"ts-node": "^6.0.5",
"nock": "^9.3.3",
"sinon": "^6.0.0",
"ts-node": "^6.1.1",
"tslib": "^1.9.2",
"tslint": "^5.10.0",
"typescript": "^2.9.1"
"typescript": "^2.9.2"
},
"engines": {
"node": ">=8.0.0"
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import _catch from './catch'
import {expect} from './chai'
import env from './env'
import * as Nock from './nock'
import {stderr, stdout} from './stdmock'
import {stderr, stdin, stdout} from './stdmock'
import stub from './stub'

import * as FancyTypes from './types'
Expand All @@ -12,6 +12,7 @@ export const fancy = base
.register('catch', _catch)
.register('env', env)
.register('stub', stub)
.register('stdin', stdin)
.register('stderr', stderr)
.register('stdout', stdout)
.register('nock', Nock.nock)
Expand Down
12 changes: 12 additions & 0 deletions src/stdmock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,15 @@ const create = <T extends 'stdout' | 'stderr'>(std: T) => (opts: {print?: boolea

export const stdout = create('stdout')
export const stderr = create('stderr')
export const stdin = (input?: string, delay = 0) => {
let stdin: any
return {
run: () => {
stdin = require('mock-stdin').stdin()
setTimeout(() => stdin.send(input), delay)
},
finally() {
stdin.restore()
},
}
}
22 changes: 22 additions & 0 deletions test/stdmock.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,25 @@ describe('stdout', () => {
.do(c => expect(c.stdout).to.equal('bar\n'))
.it('resets stdout')
})

describe('stdin', () => {
fancy
.stdin('whoa there, broseph\n')
.stdout()
.it('mocks', (_, done) => {
process.stdin.setEncoding('utf8')
process.stdin.once('data', data => {
done(data === 'whoa there, broseph\n' ? undefined : 'invalid stdin')
})
})

fancy
.stdin('whoa there again, broseph\n')
.stdout()
.it('mocks again', (_, done) => {
process.stdin.setEncoding('utf8')
process.stdin.once('data', data => {
done(data === 'whoa there again, broseph\n' ? undefined : 'invalid stdin')
})
})
})
60 changes: 36 additions & 24 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,32 @@
dependencies:
samsam "1.3.0"

"@types/chai@^4.1.3":
version "4.1.3"
resolved "https://registry.yarnpkg.com/@types/chai/-/chai-4.1.3.tgz#b8a74352977a23b604c01aa784f5b793443fb7dc"
"@types/chai@^4.1.4":
version "4.1.4"
resolved "https://registry.yarnpkg.com/@types/chai/-/chai-4.1.4.tgz#5ca073b330d90b4066d6ce18f60d57f2084ce8ca"

"@types/lodash@^4.14.109":
version "4.14.109"
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.109.tgz#b1c4442239730bf35cabaf493c772b18c045886d"

"@types/mocha@^5.2.0":
version "5.2.0"
resolved "https://registry.yarnpkg.com/@types/mocha/-/mocha-5.2.0.tgz#b3c8e69f038835db1a7fdc0b3d879fc50506e29e"
"@types/mocha@^5.2.2":
version "5.2.2"
resolved "https://registry.yarnpkg.com/@types/mocha/-/mocha-5.2.2.tgz#202d2b8fe1364c5b617b439b26a54f0e75eac0a7"

"@types/nock@^9.1.3":
version "9.1.3"
resolved "https://registry.yarnpkg.com/@types/nock/-/nock-9.1.3.tgz#1d445679375b9e25afd449dc56585f81729454e8"
dependencies:
"@types/node" "*"

"@types/node@*", "@types/node@^10.3.0":
"@types/node@*":
version "10.3.0"
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.3.0.tgz#078516315a84d56216b5d4fed8f75d59d3b16cac"

"@types/node@^10.3.3":
version "10.3.3"
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.3.3.tgz#8798d9e39af2fa604f715ee6a6b19796528e46c3"

"@types/sinon@^5.0.1":
version "5.0.1"
resolved "https://registry.yarnpkg.com/@types/sinon/-/sinon-5.0.1.tgz#a15b36ec42f1f53166617491feabd1734cb03e21"
Expand Down Expand Up @@ -111,6 +115,10 @@ browser-stdout@1.3.1:
version "1.3.1"
resolved "https://registry.yarnpkg.com/browser-stdout/-/browser-stdout-1.3.1.tgz#baa559ee14ced73452229bad7326467c61fabd60"

buffer-from@^1.0.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/buffer-from/-/buffer-from-1.1.0.tgz#87fcaa3a298358e0ade6e442cfce840740d1ad04"

builtin-modules@^1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/builtin-modules/-/builtin-modules-1.1.1.tgz#270f076c5a72c02f5b65a47df94c5fe3a278892f"
Expand Down Expand Up @@ -374,6 +382,10 @@ mocha@^5.2.0:
mkdirp "0.5.1"
supports-color "5.4.0"

mock-stdin@^0.3.1:
version "0.3.1"
resolved "https://registry.yarnpkg.com/mock-stdin/-/mock-stdin-0.3.1.tgz#c657d9642d90786435c64ca5e99bbd4d09bd7dd3"

ms@2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"
Expand All @@ -388,9 +400,9 @@ nise@^1.3.3:
path-to-regexp "^1.7.0"
text-encoding "^0.6.4"

nock@^9.3.0:
version "9.3.0"
resolved "https://registry.yarnpkg.com/nock/-/nock-9.3.0.tgz#4dcfdd37bd249836754d05bbac5a1f05e12e0f16"
nock@^9.3.3:
version "9.3.3"
resolved "https://registry.yarnpkg.com/nock/-/nock-9.3.3.tgz#d9f4cd3c011afeadaf5ccf1b243f6e353781cda0"
dependencies:
chai "^4.1.2"
debug "^3.1.0"
Expand Down Expand Up @@ -456,9 +468,9 @@ semver@^5.3.0, semver@^5.5.0:
version "5.5.0"
resolved "https://registry.yarnpkg.com/semver/-/semver-5.5.0.tgz#dc4bbc7a6ca9d916dee5d43516f0092b58f7b8ab"

sinon@^5.0.10:
version "5.0.10"
resolved "https://registry.yarnpkg.com/sinon/-/sinon-5.0.10.tgz#a282b36a7475664c9f98719108e5546907129023"
sinon@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/sinon/-/sinon-6.0.0.tgz#f26627e4830dc34279661474da2c9e784f166215"
dependencies:
"@sinonjs/formatio" "^2.0.0"
diff "^3.5.0"
Expand All @@ -468,10 +480,11 @@ sinon@^5.0.10:
supports-color "^5.4.0"
type-detect "^4.0.8"

source-map-support@^0.5.3:
version "0.5.4"
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.4.tgz#54456efa89caa9270af7cd624cc2f123e51fbae8"
source-map-support@^0.5.6:
version "0.5.6"
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.6.tgz#4435cee46b1aab62b8e8610ce60f788091c51c13"
dependencies:
buffer-from "^1.0.0"
source-map "^0.6.0"

source-map@^0.6.0:
Expand Down Expand Up @@ -515,17 +528,16 @@ text-encoding@^0.6.4:
version "0.6.4"
resolved "https://registry.yarnpkg.com/text-encoding/-/text-encoding-0.6.4.tgz#e399a982257a276dae428bb92845cb71bdc26d19"

ts-node@^6.0.5:
version "6.0.5"
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-6.0.5.tgz#977c1c931da7a2b09ae2930101f0104a5c2271e9"
ts-node@^6.1.1:
version "6.1.1"
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-6.1.1.tgz#19607140acb06150441fcdb61be11f73f7b6657e"
dependencies:
arrify "^1.0.0"
chalk "^2.3.0"
diff "^3.1.0"
make-error "^1.1.1"
minimist "^1.2.0"
mkdirp "^0.5.1"
source-map-support "^0.5.3"
source-map-support "^0.5.6"
yn "^2.0.0"

tslib@1.9.0, tslib@^1.7.1, tslib@^1.8.0, tslib@^1.8.1:
Expand Down Expand Up @@ -605,9 +617,9 @@ type-detect@^4.0.0, type-detect@^4.0.8:
version "4.0.8"
resolved "https://registry.yarnpkg.com/type-detect/-/type-detect-4.0.8.tgz#7646fb5f18871cfbb7749e69bd39a6388eb7450c"

typescript@^2.9.1:
version "2.9.1"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.9.1.tgz#fdb19d2c67a15d11995fd15640e373e09ab09961"
typescript@^2.9.2:
version "2.9.2"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.9.2.tgz#1cbf61d05d6b96269244eb6a3bce4bd914e0f00c"

wrappy@1:
version "1.0.2"
Expand Down

0 comments on commit 2d34b74

Please sign in to comment.