generated from hackforla/.github-hackforla-base-repo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 97
Add testing for application (backend, frontend, end2end) #270
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
Merged
alex-anakin
merged 44 commits into
hackforla:development
from
nickbeaird:add_frontend_testing
Aug 28, 2020
Merged
Changes from all commits
Commits
Show all changes
44 commits
Select commit
Hold shift + click to select a range
9909e41
Rebase development onto branch
nickbeaird a984d06
Move files the backend dir for jest setup
nickbeaird e90091f
Rebase changes on development
nickbeaird c23bf91
Get backend working with slack
nickbeaird f2c8d06
Get a working client and backend script
nickbeaird a09962b
Add backend jest environment
nickbeaird 9b8f069
Get working unit tests with jest
nickbeaird 2d6b731
Do not commit test db or db config file
nickbeaird 688a752
Getting a simple api test
nickbeaird 5ea1dea
Refactor server and app for testability
nickbeaird 23ecc0b
Disable slack app to enable testing
nickbeaird 94f7179
Add working proxy settings for front and backend
nickbeaird 03b2b5e
Get some example test cases
nickbeaird 343369d
Fix typo in server file
nickbeaird 5fafaf2
Rename test files
nickbeaird 34c418f
Fix Answer model and add unit test
nickbeaird 8b68a37
Fix Answer model and add unit test
nickbeaird 243c286
Add Checkin model unit test
nickbeaird 2d0f073
Add Events unit test
nickbeaird d0e117d
Get working Project unit test
nickbeaird 32d087a
Add unit tests for projectTeamMember model
nickbeaird da88bdd
Return formatting to preserve inline comments
nickbeaird 46449a3
Add unit tests for Question Model
nickbeaird fdf28a2
Add recurringEvent unit test
nickbeaird e19a469
Add user unit test
nickbeaird 234c114
Resolve import statements
nickbeaird ef62493
Update README for setting up the project
nickbeaird c09cf46
Add api test for Event creation
nickbeaird 7c36425
Add documentation for the tests
nickbeaird afc8514
Make a workaround to get the tests working
nickbeaird 57b8906
Reinstate Slack server
nickbeaird eb49661
Add in Cypress test framework
nickbeaird b735b24
Get a working test
nickbeaird 26ed64f
Move from npm to yarn
nickbeaird 1ccac6c
Downgrade package versions to work with react reqs
nickbeaird 43b71d2
Add a working frontend unit test
nickbeaird 42f766d
Get a working Cypress test
nickbeaird 7ff41ec
Update the cypress test naming
nickbeaird 07dde5a
Make all of the tests executable
nickbeaird afe32ae
Remove all hard coded env variables
nickbeaird dcaa1ad
Merge remote-tracking branch 'upstream/development' into add_frontend…
nickbeaird f82b003
Fix merge conflicts with npm vs yarn
nickbeaird e33cece
Add changes for better README
nickbeaird 7f9815f
Add changes for review
nickbeaird File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1,7 +1,12 @@ | ||
| /node_modules | ||
|
|
||
| node_modules/ | ||
| npm-debug.log | ||
| .DS_Store | ||
| /*.env | ||
| client/.env | ||
| .env | ||
| /.idea | ||
| test.db | ||
| globalConfig.json | ||
| videos/ | ||
| screenshots/ | ||
This file contains hidden or 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,8 @@ | ||
| { | ||
| "workbench.colorCustomizations": { | ||
| "editorRuler.foreground": "#ff4081", | ||
| "activityBar.background": "#322F03", | ||
| "titleBar.activeBackground": "#464204", | ||
| "titleBar.activeForeground": "#FDFBE1" | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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,55 @@ | ||
| # Backend | ||
|
|
||
| ## Running Tests | ||
|
|
||
| To maintain consistency across the front end and back end development, we are using Jest | ||
| as a test runner. | ||
nickbeaird marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ### Execute Tests | ||
|
|
||
| You will need to be in the backend directory for this to work. | ||
|
|
||
| 1. Run all tests: `npm test` | ||
| 1. Run a single test: `npm test <testFileName>` | ||
|
|
||
| ### Writing Tests | ||
|
|
||
| To maintain idempotent tests, we have opted to use in memory test databases. Jest, like | ||
| most test runners, has hooks or methods for you to call before or after tests. We can | ||
| call the `beforeAll` and `afterAll` methods setup and tear down our database before each | ||
| test. | ||
|
|
||
| ```js | ||
| // You will need to require the db-handler file. | ||
| const dbHandler = require("./db-handler"); | ||
|
|
||
| // Call the dbHanlder methods in your beforeAll and afterAll methods. | ||
| beforeAll(async () => await dbHandler.connect()); | ||
| afterAll(async () => await dbHandler.closeDatabase()); | ||
| ``` | ||
|
|
||
| In addition to hooks to call after the file is completed, Jest also has hooks/methods to | ||
| call before and after each test case. **At this time, the `dbHandler` method for clearing | ||
| the database does not work, so you can remove each collection manually if needed. | ||
|
|
||
| ```js | ||
| // You will need to require the db-handler file. | ||
| const dbHandler = require("./db-handler"); | ||
|
|
||
| // You will need to get the Model. | ||
| const Event = require("../models/event.model.js"); | ||
|
|
||
| // Then you can delete the Collection in the db beforeAll or afterAll. | ||
| afterEach(async () => await Event.remove({})); | ||
| ``` | ||
|
|
||
| ### Unit Tests | ||
|
|
||
| Unit tests are tests to write around a single unit. These can be tests around validation | ||
| of a Model, or testing the boundaries on a class. | ||
|
|
||
| ### Integration Tests | ||
|
|
||
| Integration Tests are tests that verify that differing components work together. A common | ||
| example of an integration test is to verify that data is saved correctly in the database | ||
| based on the use of the API. | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -1,23 +1,18 @@ | ||
| // server.js - Entry point for our application | ||
| // app.js - Entry point for our application | ||
|
|
||
| // Load in all of our node modules. Their uses are explained below as they are called. | ||
| const express = require("express"); | ||
| require("dotenv").config(); | ||
| const mongoose = require("mongoose"); | ||
| const morgan = require("morgan"); | ||
| const bodyParser = require("body-parser"); | ||
| // const helmet = require('helmet'); | ||
| // const cors = require('cors'); | ||
nickbeaird marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const path = require("path"); | ||
| const cron = require("node-cron"); | ||
| const fetch = require("node-fetch"); | ||
| const morgan = require("morgan"); | ||
| const path = require("path"); | ||
|
|
||
| require("dotenv").config(); | ||
|
|
||
| // Create a new application using the Express framework | ||
| const app = express(); | ||
|
|
||
| // Load config variables | ||
| const { DATABASE_URL, PORT } = require("./config/database"); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We refactored server.js into app.js and server.js. (see comments from above) |
||
|
|
||
| // Required to view Request Body (req.body) in JSON | ||
| app.use(bodyParser.json()); | ||
| app.use(bodyParser.urlencoded({ extended: true })); | ||
|
|
@@ -31,9 +26,6 @@ app.use(morgan("dev")); | |
| // Cross-Origin-Resource-Sharing | ||
| // app.use(cors()); | ||
|
|
||
| // Let mongoose access Promises | ||
| mongoose.Promise = global.Promise; | ||
|
|
||
| // WORKERS | ||
| // const runOpenCheckinWorker = require("./workers/openCheckins")(cron, fetch); | ||
nickbeaird marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // const runCloseCheckinWorker = require("./workers/closeCheckins")(cron, fetch); | ||
|
|
@@ -64,7 +56,7 @@ app.use("/api/projects", projectsRouter); | |
| app.use("/api/recurringevents", recurringEventsRouter); | ||
| app.use("/api/projectteammembers", projectTeamMembersRouter); | ||
| app.use("/api/slack", slackRouter); | ||
| const CLIENT_BUILD_PATH = path.join(__dirname, "./client/build"); | ||
| const CLIENT_BUILD_PATH = path.join(__dirname, "../client/build"); | ||
nickbeaird marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Serve static files from the React frontend app | ||
| app.use(express.static(path.join(CLIENT_BUILD_PATH))); | ||
|
|
@@ -76,53 +68,4 @@ app.get("*", (req, res) => { | |
| res.sendFile(index); | ||
| }); | ||
|
|
||
| // Make the server functions available to testing | ||
| let server; | ||
|
|
||
| async function runServer(databaseUrl, port = PORT) { | ||
| await mongoose | ||
| .connect(databaseUrl, { | ||
| useNewUrlParser: true, | ||
| useCreateIndex: true, | ||
| useUnifiedTopology: true, | ||
| useFindAndModify: false, | ||
| }) | ||
| .catch((err) => err); | ||
|
|
||
| server = app | ||
| .listen(port, () => { | ||
| console.log( | ||
| `Mongoose connected from runServer() and is listening on ${port}` | ||
| ); | ||
| }) | ||
| .on("error", (err) => { | ||
| mongoose.disconnect(); | ||
| return err; | ||
| }); | ||
| } | ||
|
|
||
| async function closeServer() { | ||
| await mongoose.disconnect().then(() => { | ||
| return new Promise((resolve, reject) => { | ||
| console.log("Closing Mongoose connection. Bye"); | ||
|
|
||
| server.close((err) => { | ||
| if (err) { | ||
| return reject(err); | ||
| } | ||
|
|
||
| resolve(); | ||
| }); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| if (require.main === module) { | ||
| runServer(DATABASE_URL).catch((err) => console.error(err)); | ||
| } | ||
|
|
||
| // app.listen(process.env.PORT || PORT, () => { | ||
| // console.log(`Server is listening on port: ${process.env.PORT || PORT}`); | ||
| // }); | ||
|
|
||
| module.exports = { app, runServer, closeServer }; | ||
| module.exports = app; | ||
This file contains hidden or 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,2 @@ | ||
| exports.PORT = process.env.BACKEND_PORT; | ||
| exports.DATABASE_URL = process.env.DATABASE_URL; | ||
nickbeaird marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or 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,12 @@ | ||
| module.exports = { | ||
| mongodbMemoryServerOptions: { | ||
| binary: { | ||
| version: "4.0.3", | ||
| skipMD5: true, | ||
| }, | ||
| instance: { | ||
| dbName: "jest", | ||
| }, | ||
| autoStart: false, | ||
| }, | ||
| }; | ||
nickbeaird marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or 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,4 @@ | ||
| module.exports = { | ||
| testEnvironment: "node", | ||
| preset: "@shelf/jest-mongodb", | ||
| }; | ||
nickbeaird marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or 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,34 @@ | ||
| const mongoose = require("mongoose"); | ||
|
|
||
| mongoose.Promise = global.Promise; | ||
|
|
||
| const answerSchema = mongoose.Schema({ | ||
| question: { | ||
| questionId: { type: String }, | ||
| htmlName: { type: String }, | ||
| }, | ||
| user: { | ||
| userId: { type: String }, | ||
| }, | ||
| selectedAnswer: { type: String }, | ||
| createdDate: { type: Date, default: Date.now }, | ||
| }); | ||
|
|
||
| answerSchema.methods.serialize = function () { | ||
| return { | ||
| id: this._id, | ||
| question: { | ||
| questionID: this.question.questionId, | ||
| htmlName: this.question.htmlName, | ||
| }, | ||
| user: { | ||
| userId: this.user.userId, | ||
| }, | ||
| selectedAnswer: this.selectedAnswer, | ||
| createdDate: this.createdDate, | ||
| }; | ||
| }; | ||
|
|
||
| const Answer = mongoose.model("Answer", answerSchema); | ||
|
|
||
| module.exports = Answer; |
This file contains hidden or 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,24 @@ | ||
| const mongoose = require("mongoose"); | ||
|
|
||
| mongoose.Promise = global.Promise; | ||
|
|
||
| const checkInSchema = mongoose.Schema({ | ||
| userId: { type: String }, | ||
| eventId: { type: String }, | ||
| checkedIn: { type: Boolean, default: true }, | ||
| createdDate: { type: Date, default: Date.now }, | ||
| }); | ||
|
|
||
| checkInSchema.methods.serialize = function () { | ||
| return { | ||
| id: this._id, | ||
| userId: this.userId, | ||
| eventId: this.eventId, | ||
| checkedIn: this.checkedIn, | ||
| createdDate: this.createdDate, | ||
| }; | ||
| }; | ||
|
|
||
| const CheckIn = mongoose.model("CheckIn", checkInSchema); | ||
|
|
||
| module.exports = CheckIn; |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.