Skip to content

Commit 7ef18a3

Browse files
committed
fix(koa-better-body): make tests passing, update deps
Signed-off-by: Charlike Mike Reagent <opensource@tunnckocore.com>
1 parent f0dd7ef commit 7ef18a3

File tree

12 files changed

+297
-334
lines changed

12 files changed

+297
-334
lines changed

jest/test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const path = require('path');
22
const utils = require('../@tunnckocore/utils/src');
33

44
const ROOT = path.dirname(__dirname);
5-
const { alias, workspaces } = utils.createAliases(ROOT, 'src');
5+
const { /* alias, */ workspaces } = utils.createAliases(ROOT, 'src');
66

77
module.exports = {
88
rootDir: ROOT,
@@ -13,10 +13,10 @@ module.exports = {
1313
/(?:__)?(?:fixtures?|supports?|shared)(?:__)?/.toString(),
1414

1515
// ! todo remove when fixed
16-
/koa-better-body/.toString(),
16+
// /koa-better-body/.toString(),
1717
/jest-runner-rollup/.toString(),
1818
],
19-
moduleNameMapper: alias,
19+
// moduleNameMapper: alias,
2020
moduleFileExtensions: ['js', 'jsx', 'ts', 'tsx'],
2121
// runner: './@tunnckocore/jest-runner-babel/src/index.js',
2222
};

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"builtin-modules": "^3.1.0",
3333
"enquirer": "^2.3.2",
3434
"eslint": "^6.4.0",
35+
"esm": "^3.2.25",
3536
"jest": "^24.9.0",
3637
"lerna": "^3.16.4",
3738
"prettier": "^1.18.2",

packages/koa-better-body/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@
5353
],
5454
"scripts": {},
5555
"dependencies": {
56-
"extend-shallow": "3.0.2",
57-
"formidable": "1.2.1",
58-
"koa-body-parsers": "git+https://github.com/tunnckoCore/body-parsers.git"
56+
"extend-shallow": "^3.0.2",
57+
"formidable": "^1.2.1",
58+
"koa-body-parsers": "tunnckoCore/body-parsers#patch-1"
5959
},
6060
"devDependencies": {
6161
"is-buffer": "2.0.3",

packages/koa-better-body/src/utils.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ export function setParsers(ctx, opts) {
114114
ctx.qs; // alias
115115

116116
bodyParsers(ctx);
117+
118+
// to do: when using koa-body-parsers v3.1 - it adds support for this and
119+
// probably will break us in some way.
120+
// delete ctx.request.body;
121+
117122
ctx.request.multipart = multipart.bind(ctx);
118123
return ctx;
119124
}

packages/koa-better-body/test/buffer.js

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,53 +5,49 @@
55
* Released under the MIT license.
66
*/
77

8-
import { promisify } from 'util';
98
import isBuffer from 'is-buffer';
109
import request from 'supertest';
1110
import koa from 'koa';
1211
import betterBody from '../src';
1312

1413
test('should get the raw buffer body (options.buffer: true)', async () => {
1514
const server = koa().use(betterBody({ buffer: true }));
15+
1616
// eslint-disable-next-line require-yield
1717
server.use(function* koaCustomPlugin() {
18-
test.strictEqual(isBuffer(this.request.body), true);
18+
expect(isBuffer(this.request.body)).toStrictEqual(true);
1919
this.body = this.request.body.toString('utf8');
2020
});
21-
await promisify(
22-
request(server.callback())
23-
.post('/')
24-
.type('text')
25-
.send('qux')
26-
.expect(200)
27-
.expect('qux').end,
28-
)();
21+
22+
await request(server.callback())
23+
.post('/')
24+
.type('text')
25+
.send('qux')
26+
.expect(200)
27+
.expect('qux');
2928
});
29+
3030
test('should throw if the buffer body is too large (options.buffer: true)', async () => {
3131
const server = koa().use(betterBody({ buffer: true, bufferLimit: '2b' }));
3232

33-
await promisify(
34-
request(server.callback())
35-
.post('/')
36-
.type('text')
37-
.send('too large')
38-
.expect(413).end,
39-
)();
33+
await request(server.callback())
34+
.post('/')
35+
.type('text')
36+
.send('too large')
37+
.expect(413);
4038
});
39+
4140
test('should get json if `options.buffer` is false (that is the default)', async () => {
4241
const server = koa().use(betterBody());
4342
// eslint-disable-next-line require-yield
4443
server.use(function* koaCustomPlugin() {
45-
test.strictEqual(typeof this.request.fields, 'object');
46-
test.deepEqual(this.request.fields, { 'too large': '' });
44+
expect(this.request.fields).toMatchObject({ 'too large': '' });
4745
this.body = this.request.fields;
4846
});
4947

50-
await promisify(
51-
request(server.callback())
52-
.post('/')
53-
.send('too large')
54-
.expect(200)
55-
.expect(/"too large":/).end,
56-
)();
48+
await request(server.callback())
49+
.post('/')
50+
.send('too large')
51+
.expect(200)
52+
.expect(/"too large":/);
5753
});

packages/koa-better-body/test/custom.js

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
* Released under the MIT license.
66
*/
77

8-
import { promisify } from 'util';
98
import request from 'supertest';
109
import koa from 'koa';
1110
import betterBody from '../src';
@@ -16,11 +15,12 @@ test('should accept opts.extendTypes.custom `foo/bar-x` as text', async () => {
1615
extendTypes: {
1716
custom: ['foo/bar-x'],
1817
},
18+
1919
handler: function* handler(ctx, opts) {
20-
test.strictEqual(typeof ctx, 'object');
21-
test.strictEqual(typeof this, 'object');
22-
test.strictEqual(typeof ctx.request.text, 'function');
23-
test.strictEqual(typeof this.request.text, 'function');
20+
expect(typeof ctx).toStrictEqual('object');
21+
expect(typeof this).toStrictEqual('object');
22+
expect(typeof ctx.request.text).toStrictEqual('function');
23+
expect(typeof this.request.text).toStrictEqual('function');
2424

2525
this.request.body = yield this.request.text(opts);
2626
},
@@ -29,22 +29,19 @@ test('should accept opts.extendTypes.custom `foo/bar-x` as text', async () => {
2929

3030
app = app
3131
.use(function* abc(next) {
32-
test.strictEqual(typeof this.request.body, 'string');
33-
test.strictEqual(this.request.body, 'message=lol');
32+
expect(this.request.body).toStrictEqual('message=lol');
3433
this.body = this.request.body;
3534
yield* next;
3635
})
3736
// eslint-disable-next-line require-yield
3837
.use(function* abc() {
39-
test.strictEqual(this.body, 'message=lol');
38+
expect(this.body).toStrictEqual('message=lol');
4039
});
4140

42-
await promisify(
43-
request(app.callback())
44-
.post('/')
45-
.type('foo/bar-x')
46-
.send('message=lol')
47-
.expect(200)
48-
.expect('message=lol').end,
49-
)();
41+
await request(app.callback())
42+
.post('/')
43+
.type('foo/bar-x')
44+
.send('message=lol')
45+
.expect(200)
46+
.expect('message=lol');
5047
});

packages/koa-better-body/test/json.js

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
* Released under the MIT license.
66
*/
77

8-
import { promisify } from 'util';
98
import request from 'supertest';
109
import koa from 'koa';
1110
import betterBody from '../src';
@@ -22,44 +21,39 @@ const app = koa()
2221
.use(postBody());
2322

2423
test('should parse a json body', async () => {
25-
await promisify(
26-
request(app.callback())
27-
.post('/')
28-
.send({ foo: 'lol' })
29-
.expect(200)
30-
.expect({ foo: 'lol' }).end,
31-
)();
24+
await request(app.callback())
25+
.post('/')
26+
.send({ foo: 'lol' })
27+
.expect(200)
28+
.expect({ foo: 'lol' });
3229
});
30+
3331
test('should parse a string json body', async () => {
34-
await promisify(
35-
request(app.callback())
36-
.post('/')
37-
.type('application/json')
38-
.send('{"fao":"nato"}')
39-
.expect(200)
40-
.expect({ fao: 'nato' }).end,
41-
)();
32+
await request(app.callback())
33+
.post('/')
34+
.type('application/json')
35+
.send('{"fao":"nato"}')
36+
.expect(200)
37+
.expect({ fao: 'nato' });
4238
});
39+
4340
test('should throw on json non-object body in strict mode (default)', async () => {
44-
await promisify(
45-
request(app.callback())
46-
.post('/')
47-
.type('json')
48-
.send('"lol"')
49-
.expect(400).end,
50-
)();
41+
await request(app.callback())
42+
.post('/')
43+
.type('json')
44+
.send('"lol"')
45+
.expect(400);
5146
});
47+
5248
test('should not throw on non-objects in non-strict mode', async () => {
5349
const server = koa()
5450
.use(betterBody({ jsonStrict: false }))
5551
.use(postBody());
5652

57-
await promisify(
58-
request(server.callback())
59-
.post('/')
60-
.type('json')
61-
.send('"foobar"')
62-
.expect(200)
63-
.expect(/foobar/).end,
64-
)();
53+
await request(server.callback())
54+
.post('/')
55+
.type('json')
56+
.send('"foobar"')
57+
.expect(200)
58+
.expect(/foobar/);
6559
});

0 commit comments

Comments
 (0)