From 4a428f0ed977405ffd73c47e4faa4d0e99f7431f Mon Sep 17 00:00:00 2001 From: Ben Silverman Date: Mon, 27 Jul 2020 15:55:09 -0400 Subject: [PATCH] #17 setup test DB and JWT mock for API testing --- jest.config.js | 1 + src/__mocks__/next-auth/jwt.js | 17 +++++++++++++++++ src/setupEnv.js | 2 ++ 3 files changed, 20 insertions(+) create mode 100644 src/__mocks__/next-auth/jwt.js create mode 100644 src/setupEnv.js diff --git a/jest.config.js b/jest.config.js index b2daf8d5..d4d8cf76 100644 --- a/jest.config.js +++ b/jest.config.js @@ -4,6 +4,7 @@ module.exports = { '!**/*.d.ts', '!**/node_modules/**', ], + setupFiles: ['/src/setupEnv.js'], setupFilesAfterEnv: ['/src/setupTests.js'], testPathIgnorePatterns: ['/node_modules/', '/.next/'], transform: { diff --git a/src/__mocks__/next-auth/jwt.js b/src/__mocks__/next-auth/jwt.js new file mode 100644 index 00000000..76462166 --- /dev/null +++ b/src/__mocks__/next-auth/jwt.js @@ -0,0 +1,17 @@ +const { getObjectId } = require('mongo-seeding'); + +const jwt = jest.genMockFromModule('next-auth/jwt'); + +function getJwt() { + return { + exp: 1000, + user: { + id: getObjectId('test-user'), + name: 'Test User', + email: 'test@example.com', + }, + }; +} + +jwt.getJwt = getJwt; +module.exports = jwt; diff --git a/src/setupEnv.js b/src/setupEnv.js new file mode 100644 index 00000000..9d8ec3c0 --- /dev/null +++ b/src/setupEnv.js @@ -0,0 +1,2 @@ +process.env.MONGODB_URI = 'mongodb://localhost:27017/as4-test'; +process.env.DB_NAME = 'as4-test';