-
-
Notifications
You must be signed in to change notification settings - Fork 379
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b7b11fb
commit 3fd5cc2
Showing
4 changed files
with
39 additions
and
26 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import treeHandler from 'pages/api/tree' | ||
import { mockServer } from 'tests/helper' | ||
|
||
describe('/api/tree', () => { | ||
let app: mockServer | ||
|
||
beforeEach(async () => { | ||
app = mockServer(treeHandler) | ||
}) | ||
|
||
afterEach(() => { | ||
app.server.close() | ||
}) | ||
|
||
test('fetch tree', async () => { | ||
const result = await app.request.get('/api/tree').expect(200) | ||
|
||
expect(result.body).toBeDefined() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { createServer, IncomingMessage, ServerResponse } from 'http' | ||
import { ApiRequest, ApiResponse } from 'libs/server/connect' | ||
import { NextConnect } from 'next-connect' | ||
import { apiResolver } from 'next/dist/server/api-utils' | ||
import supertest from 'supertest' | ||
|
||
export const mockServer = (handler: NextConnect<ApiRequest, ApiResponse>) => { | ||
const requestHandler = (request: IncomingMessage, response: ServerResponse) => | ||
apiResolver(request, response, undefined, handler, {} as any, true) | ||
const server = createServer(requestHandler) | ||
|
||
return { | ||
server, | ||
request: supertest(server), | ||
} | ||
} | ||
|
||
export type mockServer = ReturnType<typeof mockServer> |