Skip to content

Commit

Permalink
Merge pull request #21 from sharkcore/add_failing_test
Browse files Browse the repository at this point in the history
Add failing test showing that this library does not work with res.write/res.end
  • Loading branch information
magicmark authored Dec 20, 2018
2 parents 097f316 + 9290bff commit 76f2559
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 21 deletions.
34 changes: 33 additions & 1 deletion __tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe('middleware', () => {
});
});

it('should work with res.end', done => {
it('should work with res.send', done => {
app.get('/foo', (req, res) => {
res.send('foo');
});
Expand All @@ -50,4 +50,36 @@ describe('middleware', () => {
done();
});
});

it('should work with res.send (json)', done => {
app.get('/foo', (req, res) => {
res.send({ foo: 'bar' });
});

request(app)
.get('/foo')
.end(() => {
expect(beforeFn).toHaveBeenCalled();
expect(JSON.parse(afterFn.mock.calls[0][0])).toMatchObject({ foo: 'bar' });
done();
});
});

it.skip('should work with res.end', done => {
app.get('/foo', (req, res) => {
res.set('Content-Type', 'text/plain');
res.write('foo');
res.write('bar');
res.write('baz');
res.end();
});

request(app)
.get('/foo')
.end(() => {
expect(beforeFn).toHaveBeenCalled();
expect(afterFn).toHaveBeenCalledWith('foobarbaz');
done();
});
});
});
23 changes: 10 additions & 13 deletions flow-typed/npm/express_v4.x.x.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
// flow-typed signature: f0e399a136d6e8dc8b1fbdc078e2850c
// flow-typed version: ed397013d1/express_v4.x.x/flow_>=v0.32.x

import type { Server } from 'http';
import * as http from 'http';
import type { Socket } from 'net';

declare type express$RouterOptions = {
Expand All @@ -21,7 +18,7 @@ declare type express$RequestParams = {

declare class express$Request extends http$IncomingMessage mixins express$RequestResponseBase {
baseUrl: string;
body: any;
body: mixed;
cookies: {[cookie: string]: string};
connection: Socket;
fresh: boolean;
Expand Down Expand Up @@ -161,20 +158,18 @@ declare class express$Router extends express$Route {
id: string
) => mixed
): void;

// Can't use regular callable signature syntax due to https://github.com/facebook/flow/issues/3084
$call: (req: http$IncomingMessage, res: http$ServerResponse, next?: ?express$NextFunction) => void;
(req: http$IncomingMessage, res: http$ServerResponse, next?: ?express$NextFunction): void;
}

declare class express$Application extends express$Router mixins events$EventEmitter {
constructor(): void;
locals: {[name: string]: mixed};
mountpath: string;
listen(port: number, hostname?: string, backlog?: number, callback?: (err?: ?Error) => mixed): Server;
listen(port: number, hostname?: string, callback?: (err?: ?Error) => mixed): Server;
listen(port: number, callback?: (err?: ?Error) => mixed): Server;
listen(path: string, callback?: (err?: ?Error) => mixed): Server;
listen(handle: Object, callback?: (err?: ?Error) => mixed): Server;
listen(port: number, hostname?: string, backlog?: number, callback?: (err?: ?Error) => mixed): ?http.Server;
listen(port: number, hostname?: string, callback?: (err?: ?Error) => mixed): ?http.Server;
listen(port: number, callback?: (err?: ?Error) => mixed): ?http.Server;
listen(path: string, callback?: (err?: ?Error) => mixed): ?http.Server;
listen(handle: Object, callback?: (err?: ?Error) => mixed): ?http.Server;
disable(name: string): void;
disabled(name: string): boolean;
enable(name: string): express$Application;
Expand All @@ -187,6 +182,8 @@ declare class express$Application extends express$Router mixins events$EventEmit
set(name: string, value: mixed): mixed;
render(name: string, optionsOrFunction: {[name: string]: mixed}, callback: express$RenderCallback): void;
handle(req: http$IncomingMessage, res: http$ServerResponse, next?: ?express$NextFunction): void;
// callable signature is not inherited
(req: http$IncomingMessage, res: http$ServerResponse, next?: ?express$NextFunction): void;
}

declare module 'express' {
Expand Down
27 changes: 20 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@
"main": "lib/index.js",
"author": "Mark Larah <mark@larah.me>",
"license": "MIT",
"keywords": ["express", "pyramid", "tween", "middleware", "nodejs", "node"],
"keywords": [
"express",
"pyramid",
"tween",
"middleware",
"nodejs",
"node"
],
"homepage": "https://github.com/sharkcore/tweenz",
"scripts": {
"precommit": "yarn run lint",
"prepublishOnly": "make build",
"test": "jest --coverage",
"lint": "eslint src",
Expand All @@ -21,14 +27,21 @@
"babel-preset-flow": "^6.23.0",
"eslint": "^4.16.0",
"express": "^4.16.2",
"flow-bin": "^0.65.0",
"flow-copy-source": "^1.2.1",
"husky": "^0.14.3",
"jest": "^22.0.0",
"flow-bin": "^0.89.0",
"flow-copy-source": "^2.0.2",
"husky": "^1.2.1",
"jest": "^23.6.0",
"supertest": "^3.0.0"
},
"peerDependencies": {
"express": "^4.x.x"
},
"files": ["lib"]
"files": [
"lib"
],
"husky": {
"hooks": {
"pre-commit": "yarn run lint"
}
}
}

0 comments on commit 76f2559

Please sign in to comment.