Skip to content

Commit

Permalink
Merge pull request #166 from 29ki/functions-jest-tests
Browse files Browse the repository at this point in the history
Jest tests in functions
  • Loading branch information
gewfy committed Aug 22, 2022
2 parents 33855cc + 0a29715 commit 237ee21
Show file tree
Hide file tree
Showing 4 changed files with 1,699 additions and 21 deletions.
6 changes: 6 additions & 0 deletions functions/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testPathIgnorePatterns: ['<rootDir>/lib'],
};
6 changes: 5 additions & 1 deletion functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
"serve": "yarn build && firebase emulators:start --project demo-29k-cupcake --import emulatorSeed --export-on-exit",
"shell": "yarn build && firebase functions:shell",
"start": "yarn serve",
"test:jest": "jest",
"test:lint": "eslint --ext .js,.ts",
"test:ts": "tsc --noEmit",
"test": "yarn build:content && yarn test:ts && yarn test:lint"
"test": "yarn build:content && yarn test:ts && yarn test:lint && yarn test:jest"
},
"dependencies": {
"@koa/router": "^12.0.0",
Expand All @@ -33,6 +34,7 @@
"yup": "^0.32.11"
},
"devDependencies": {
"@types/jest": "^28.1.7",
"@types/koa": "^2.13.5",
"@types/koa__router": "^8.0.11",
"@types/node-fetch": "2",
Expand All @@ -46,7 +48,9 @@
"eslint-plugin-prettier": "^4.2.1",
"firebase-functions-test": "^2.1.0",
"firebase-tools": "^11.6.0",
"jest": "^28.1.3",
"prettier": "^2.7.1",
"ts-jest": "^28.0.8",
"typescript": "^4.5.4"
}
}
30 changes: 30 additions & 0 deletions functions/src/api/lib/firebaseBodyParser.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import firebaseBodyParser, {FirebaseContext} from './firebaseBodyParser';

describe('firebaseBodyParser', () => {
it('adds the already parsed firebase body to the koa request object', async () => {
const middleware = firebaseBodyParser();

const ctx = {
request: {
body: '{"foo":"bar"}',
},
req: {
body: {foo: 'bar'},
},
};
const next = jest.fn();

await middleware(ctx as FirebaseContext, next);

expect(ctx).toEqual({
request: {
body: {foo: 'bar'},
},
req: {
body: {foo: 'bar'},
},
});

expect(next).toBeCalledTimes(1);
});
});
Loading

0 comments on commit 237ee21

Please sign in to comment.