Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing NEXT.js api routes #20

Merged
merged 2 commits into from
Nov 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ jobs:
- run: yarn
- run: 'yarn ci:format'
- run: 'yarn ci:lint'
- run: yarn test
- run: yarn build
- name: Setup Cypress
uses: cypress-io/github-action@v2
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
"format": "yarn run ci:format --write",
"cy:open": "cypress open",
"cy:run": "cypress run",
"test:watch": "yarn test --watch",
"test": "jest --testPathIgnorePatterns tests/firestore --testPathIgnorePatterns cypress",
"test:cy": "firebase emulators:exec 'yarn start-server-and-test start:test http://localhost:3000 cy:run'",
"test:rules:watch": "firebase emulators:exec --only firestore 'jest --watch tests/firestore/rules.spec.ts --detectOpenHandles'",
"test:rules": "firebase emulators:exec --only firestore 'jest tests/firestore/rules.spec.ts --detectOpenHandles'"
Expand All @@ -29,6 +31,7 @@
"@fortawesome/react-fontawesome": "^0.1.12",
"firebase": "^7.0.0",
"framer-motion": "^2.9.4",
"isomorphic-unfetch": "^3.1.0",
"next": "10.0.1",
"next-connect": "^0.9.1",
"react": "^0.0.0-experimental-4ead6b530",
Expand All @@ -41,6 +44,7 @@
"@types/jest": "^26.0.15",
"@types/node": "^14.14.8",
"@types/react": "^16.9.56",
"@types/test-listen": "^1.1.0",
"@typescript-eslint/eslint-plugin": "^4.7.0",
"@typescript-eslint/parser": "^4.7.0",
"cypress": "^6.0.0",
Expand All @@ -53,6 +57,7 @@
"lint-staged": "^10.5.1",
"prettier": "^2.1.2",
"start-server-and-test": "^1.11.5",
"test-listen": "^1.1.0",
"ts-jest": "^26.4.4",
"typescript": "^4.0.5"
}
Expand Down
30 changes: 30 additions & 0 deletions tests/pages/api/webhook/github.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import http from 'http';
import fetch from 'isomorphic-unfetch';
import { apiResolver } from 'next/dist/next-server/server/api-utils';
import listen from 'test-listen';

import handler from '../../../../src/pages/api/webhook/github';

describe('/api/webhook/github', () => {
describe('GET /api/webhook/github ', () => {
let server: http.Server;
let url: string;

beforeAll(async () => {
server = http.createServer((req, res) =>
apiResolver(req, res, undefined, handler, undefined, undefined),
);
url = await listen(server);
});

afterAll((done) => {
server.close(done);
});

it('returns a json with foo bar', async () => {
const response = await fetch(url);

expect(await response.json()).toEqual({ foo: 'bar' });
});
});
});
25 changes: 25 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2963,6 +2963,13 @@
resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-2.0.0.tgz#7036640b4e21cc2f259ae826ce843d277dad8cff"
integrity sha512-RJJrrySY7A8havqpGObOB4W92QXKJo63/jFLLgpvOtsGUqbQZ9Sbgl35KMm1DjC6j7AvmmU2bIno+3IyEaemaw==

"@types/test-listen@^1.1.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@types/test-listen/-/test-listen-1.1.0.tgz#db7e5b0e277b4a3ee7b90770dae1a41b186458af"
integrity sha512-y6ZfbSzYHniCeY6ZAzsQjSAdJInNVoEz4Uhsb81W+RCoNYA59yoG/+XbqPqCPj2KCU3Wa6RFWSozutkGIHIsNQ==
dependencies:
"@types/node" "*"

"@types/[email protected]":
version "1.4.2"
resolved "https://registry.yarnpkg.com/@types/tinycolor2/-/tinycolor2-1.4.2.tgz#721ca5c5d1a2988b4a886e35c2ffc5735b6afbdf"
Expand Down Expand Up @@ -7790,6 +7797,14 @@ isobject@^3.0.0, isobject@^3.0.1:
resolved "https://registry.yarnpkg.com/isobject/-/isobject-3.0.1.tgz#4e431e92b11a9731636aa1f9c8d1ccbcfdab78df"
integrity sha1-TkMekrEalzFjaqH5yNHMvP2reN8=

isomorphic-unfetch@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/isomorphic-unfetch/-/isomorphic-unfetch-3.1.0.tgz#87341d5f4f7b63843d468438128cb087b7c3e98f"
integrity sha512-geDJjpoZ8N0kWexiwkX8F9NkTsXhetLPVbZFQ+JTW239QNOwvB0gniuR1Wc6f0AMTn7/mFGyXvHTifrCp/GH8Q==
dependencies:
node-fetch "^2.6.1"
unfetch "^4.2.0"

isstream@~0.1.2:
version "0.1.2"
resolved "https://registry.yarnpkg.com/isstream/-/isstream-0.1.2.tgz#47e63f7af55afa6f92e1500e690eb8b8529c099a"
Expand Down Expand Up @@ -12164,6 +12179,11 @@ test-exclude@^6.0.0:
glob "^7.1.4"
minimatch "^3.0.4"

test-listen@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/test-listen/-/test-listen-1.1.0.tgz#2ba614d96c3bc9157469003027b42a495dd83b6a"
integrity sha512-OyEVi981C1sb9NX1xayfgZls3p8QTDRwp06EcgxSgd1kktaENBW8dO15i8v/7Fi15j0IYQctJzk5J+hyEBId2w==

[email protected]:
version "1.0.0"
resolved "https://registry.yarnpkg.com/text-hex/-/text-hex-1.0.0.tgz#69dc9c1b17446ee79a92bf5b884bb4b9127506f5"
Expand Down Expand Up @@ -12514,6 +12534,11 @@ typescript@^4.0.5:
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.0.5.tgz#ae9dddfd1069f1cb5beb3ef3b2170dd7c1332389"
integrity sha512-ywmr/VrTVCmNTJ6iV2LwIrfG1P+lv6luD8sUJs+2eI9NLGigaN+nUQc13iHqisq7bra9lnmUSYqbJvegraBOPQ==

unfetch@^4.2.0:
version "4.2.0"
resolved "https://registry.yarnpkg.com/unfetch/-/unfetch-4.2.0.tgz#7e21b0ef7d363d8d9af0fb929a5555f6ef97a3be"
integrity sha512-F9p7yYCn6cIW9El1zi0HI6vqpeIvBsr3dSuRO6Xuppb1u5rXpCPmMvLSyECLhybr9isec8Ohl0hPekMVrEinDA==

unicode-canonical-property-names-ecmascript@^1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/unicode-canonical-property-names-ecmascript/-/unicode-canonical-property-names-ecmascript-1.0.4.tgz#2619800c4c825800efdd8343af7dd9933cbe2818"
Expand Down