Skip to content

Commit

Permalink
fix: e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
liximomo committed Dec 11, 2020
1 parent 58146b3 commit b148127
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 44 deletions.
2 changes: 1 addition & 1 deletion packages/shuvi/src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ class Api extends Hookable implements IApi {
let serverMiddleware: Runtime.IServerMiddleware[];
try {
// this.resources.server maybe don't exist
serverMiddleware = this.resources.server.server.serverMiddleware;
serverMiddleware = this.resources.server.server.serverMiddleware || [];
} catch (error) {
serverMiddleware = [];
}
Expand Down
2 changes: 2 additions & 0 deletions packages/shuvi/src/shuvi/shuvi.base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export default abstract class Shuvi {
configFile,
mode: this.getMode()
});

this._handlePageRequest = this._handlePageRequest.bind(this);
}

async ready(): Promise<void> {
Expand Down
4 changes: 2 additions & 2 deletions packages/types/src/runtime.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,15 +211,15 @@ export type IServerAppNext = Koa.Next;
export type IServerAppResponse = Koa.Response;

export interface IServerModule {
onViewDone(
onViewDone?(
req: IncomingMessage,
res: ServerResponse,
payload: {
html: string | null;
appContext: any;
}
): void;
serverMiddleware: IServerMiddleware[];
serverMiddleware?: IServerMiddleware[];
}

export interface IServerMiddlewareOptions {
Expand Down
7 changes: 4 additions & 3 deletions test/e2e/serverMiddleware.prod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,13 @@ describe('serverMiddleware production', () => {
jest.spyOn(console, 'log');
const page = await ctx.browser.page(ctx.url('/testorder'));

expect(console.log).toBeCalledTimes(5);
expect(console.log).toBeCalledTimes(6);
expect(console.log).toHaveBeenNthCalledWith(1, -1);
expect(console.log).toHaveBeenNthCalledWith(2, 0);
expect(console.log).toHaveBeenNthCalledWith(3, 'server.js');
expect(console.log).toHaveBeenNthCalledWith(2, 'user default order');
expect(console.log).toHaveBeenNthCalledWith(3, 'plugin default order');
expect(console.log).toHaveBeenNthCalledWith(4, 1);
expect(console.log).toHaveBeenNthCalledWith(5, 9);
expect(console.log).toHaveBeenNthCalledWith(6, 10);
await page.close();
});
});
65 changes: 28 additions & 37 deletions test/fixtures/serverMiddleware/plugins/pluginServerMiddleware.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,38 @@
module.exports = api => {
api.addServerMiddleware(
{
path: 'testorder',
handler: async (ctx, next) => {
console.log(1);
await next();
}
api.addServerMiddleware({
path: 'testorder',
handler: async (ctx, next) => {
console.log(1);
await next();
},
{ order: 1 }
);
order: 1
});

api.addServerMiddleware(
{
path: 'testorder',
handler: async (ctx, next) => {
console.log(0);
await next();
}
},
{ order: 0 }
);
api.addServerMiddleware({
path: 'testorder',
handler: async (ctx, next) => {
console.log('plugin default order');
await next();
}
});

api.addServerMiddleware(
{
path: 'testorder',
handler: async (ctx, next) => {
console.log(-1);
await next();
}
api.addServerMiddleware({
path: 'testorder',
handler: async (ctx, next) => {
console.log(-1);
await next();
},
{ order: -1 }
);
order: -1
});

api.addServerMiddleware(
{
path: 'testorder',
handler: async (ctx, next) => {
console.log(9);
await next();
}
api.addServerMiddleware({
path: 'testorder',
handler: async (ctx, next) => {
console.log(9);
await next();
},
{ order: 9 }
);
order: 9
});

api.addServerMiddleware({
path: 'pluginservermiddleware',
Expand Down
10 changes: 9 additions & 1 deletion test/fixtures/serverMiddleware/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,16 @@ export const serverMiddleware = [
{
path: '/testorder',
handler: (ctx, next) => {
console.log('server.js');
console.log('user default order');
return next();
}
},
{
path: '/testorder',
handler: (ctx, next) => {
console.log(10);
return next();
},
order: 10
}
];

0 comments on commit b148127

Please sign in to comment.