Merge pull request #588 from koajs/539-babel-test
test: add a babel example
This commit is contained in:
commit
da6c63da59
7 changed files with 86 additions and 3 deletions
|
@ -3,13 +3,19 @@ parser: babel-eslint
|
|||
|
||||
extends: standard
|
||||
|
||||
plugins: [
|
||||
"babel"
|
||||
]
|
||||
|
||||
rules:
|
||||
arrow-parens: 0
|
||||
babel/arrow-parens: [2, "as-needed"]
|
||||
|
||||
eqeqeq: 0
|
||||
no-var: 2
|
||||
semi: [2, always]
|
||||
space-before-function-paren: [2, never]
|
||||
yoda: 0
|
||||
arrow-parens: [2, "as-needed"]
|
||||
arrow-spacing: 2
|
||||
dot-location: [2, "property"]
|
||||
prefer-arrow-callback: 2
|
||||
|
|
3
Makefile
3
Makefile
|
@ -5,7 +5,8 @@ REQUIRED = --require should --require should-http
|
|||
TESTS = test/application/* \
|
||||
test/context/* \
|
||||
test/request/* \
|
||||
test/response/*
|
||||
test/response/* \
|
||||
test/babel/index.js
|
||||
|
||||
lint:
|
||||
@./node_modules/.bin/eslint lib test
|
||||
|
|
|
@ -85,6 +85,29 @@ app.use(function *(){
|
|||
app.listen(3000);
|
||||
```
|
||||
|
||||
## Async Functions with Babel
|
||||
|
||||
To use `async` functions in Koa, we recommend using [babel's require hook](http://babeljs.io/docs/usage/require/).
|
||||
|
||||
```js
|
||||
require('babel-core/register');
|
||||
// require the rest of the app that needs to be transpiled after the hook
|
||||
const app = require('./app');
|
||||
```
|
||||
|
||||
To parse and transpile async functions,
|
||||
you should at a minimum have the [transform-async-to-generator](http://babeljs.io/docs/plugins/transform-async-to-generator/)
|
||||
or [transform-async-to-module-method](http://babeljs.io/docs/plugins/transform-async-to-module-method/) plugins.
|
||||
For example, in your `.babelrc` file, you should have:
|
||||
|
||||
```json
|
||||
{
|
||||
"plugins": ["transform-async-to-generator"]
|
||||
}
|
||||
```
|
||||
|
||||
Currently, you could also use the [stage-3 preset](http://babeljs.io/docs/plugins/preset-stage-3/).
|
||||
|
||||
## Settings
|
||||
|
||||
Application settings are properties on the `app` instance, currently
|
||||
|
|
|
@ -42,10 +42,12 @@
|
|||
"vary": "^1.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"babel": "^5.0.0",
|
||||
"babel-core": "^6.1.2",
|
||||
"babel-eslint": "^4.1.3",
|
||||
"babel-plugin-transform-async-to-generator": "^6.0.14",
|
||||
"eslint": "^1.6.0",
|
||||
"eslint-config-standard": "^4.4.0",
|
||||
"eslint-plugin-babel": "^2.1.1",
|
||||
"eslint-plugin-standard": "^1.3.1",
|
||||
"istanbul": "^0.4.0",
|
||||
"mocha": "^2.0.1",
|
||||
|
|
3
test/babel/.babelrc
Normal file
3
test/babel/.babelrc
Normal file
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"plugins": ["transform-async-to-generator"]
|
||||
}
|
42
test/babel/_test.js
Normal file
42
test/babel/_test.js
Normal file
|
@ -0,0 +1,42 @@
|
|||
'use strict';
|
||||
|
||||
const request = require('supertest');
|
||||
const Koa = require('../..');
|
||||
|
||||
describe('require("babel-core/register")', () => {
|
||||
describe('app.use(fn)', () => {
|
||||
it('should compose middleware w/ async functions', done => {
|
||||
const app = new Koa();
|
||||
const calls = [];
|
||||
|
||||
app.use(async (ctx, next) => {
|
||||
calls.push(1);
|
||||
await next();
|
||||
calls.push(6);
|
||||
});
|
||||
|
||||
app.use(async (ctx, next) => {
|
||||
calls.push(2);
|
||||
await next();
|
||||
calls.push(5);
|
||||
});
|
||||
|
||||
app.use(async (ctx, next) => {
|
||||
calls.push(3);
|
||||
await next();
|
||||
calls.push(4);
|
||||
});
|
||||
|
||||
const server = app.listen();
|
||||
|
||||
request(server)
|
||||
.get('/')
|
||||
.expect(404)
|
||||
.end(err => {
|
||||
if (err) return done(err);
|
||||
calls.should.eql([1, 2, 3, 4, 5, 6]);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
6
test/babel/index.js
Normal file
6
test/babel/index.js
Normal file
|
@ -0,0 +1,6 @@
|
|||
'use strict';
|
||||
|
||||
// http://babeljs.io/docs/setup/#babel_register
|
||||
|
||||
require('babel-core/register');
|
||||
require('./_test.js');
|
Loading…
Reference in a new issue