Skip to content
This repository has been archived by the owner on Aug 17, 2023. It is now read-only.

Commit

Permalink
add babel demo
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Jul 2, 2016
1 parent fb89929 commit 92cd9f4
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"presets": ["es2015"],
"plugins": ["transform-async-to-generator"]
}
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Summary
- [Examples](#examples)
- [Simple tests](#simple-tests)
- [Mocha style tests](#mocha-style-tests)
- [Es-next tests with babel](#es-next-tests-with-babel)
- [Tests in source code](#tests-in-source-code)
- [Practical tests](#practical-tests)
- [Complex tests](#complex-tests)
Expand Down Expand Up @@ -149,6 +150,39 @@ describe('mocha style', function () {
})
```

### [Es-next tests with babel](https://github.com/thunks/tman/tree/master/example/es-next.es)
`tman -r babel-register -r babel-polyfill example/es-next.es`:
```js
import assert from 'assert'
import tman from 'tman'

var count = 0
// async "after hook"
tman.after(async () => {
assert.strictEqual(await Promise.resolve(count++), 4)
})

tman.it('async/await asynchronous test', async function () {
assert.strictEqual(await Promise.resolve(count++), 0)
assert.strictEqual(await new Promise((resolve, reject) => {
setTimeout(() => {
resolve(count++)
}, 100)
}), 1)
})

tman.it('generator asynchronous test', function * () {
// yield Promise
assert.strictEqual(yield Promise.resolve(count++), 2)
// yield thunk function
assert.strictEqual(yield (done) => {
setTimeout(() => {
done(null, count++)
}, 100)
}, 3)
})
```

### [Tests in source code](https://github.com/thunks/tman/tree/master/example/tests-in-source-code.js)
It shows writing tests in source code. The tests will run in [test mode](#t-man-test-mode).

Expand Down
37 changes: 37 additions & 0 deletions example/es-next.es
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict'
// **Github:** https://github.com/thunks/tman
//
// **License:** MIT

// `babel-node --presets es2015 --plugins transform-async-to-generator bin/tman example/es-next.es`
// or (with .babelrc)
// `tman -r babel-register -r babel-polyfill example/es-next.es`

import assert from 'assert'
import tman from '..'

var count = 0
// async "after hook"
tman.after(async () => {
assert.strictEqual(await Promise.resolve(count++), 4)
})

tman.it('async/await asynchronous test', async function () {
assert.strictEqual(await Promise.resolve(count++), 0)
assert.strictEqual(await new Promise((resolve, reject) => {
setTimeout(() => {
resolve(count++)
}, 100)
}), 1)
})

tman.it('generator asynchronous test', function * () {
// yield Promise
assert.strictEqual(yield Promise.resolve(count++), 2)
// yield thunk function
assert.strictEqual(yield (done) => {
setTimeout(() => {
done(null, count++)
}, 100)
}, 3)
})
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@
"thunks": "^4.2.2"
},
"devDependencies": {
"babel-plugin-transform-async-to-generator": "^6.8.0",
"babel-polyfill": "^6.9.1",
"babel-preset-es2015": "^6.9.0",
"babel-register": "^6.9.0",
"coffee-script": "^1.10.0",
"istanbul": "^0.4.4",
"standard": "^7.1.2",
Expand Down

0 comments on commit 92cd9f4

Please sign in to comment.