After managing to write a test for a get request on my app, I am trying a post request. After some research online I managed to write the following test:
const request = require('supertest')
const app = require('../main/http')
describe('Testing the /new-comp post', () => {
it('should post a new comp', async () => {
const res = await request(app)
.post('/new-comp')
.send({
id: 'Some ID',
compName: 'Another name',
place: 'Poznan',
time: '10 am',
subscriptions: [],
date: '1596135253446',
cost: {
currency: 'EUR',
amount: 50,
},
})
expect(res.StatusCode).toEqual(201)
expect(res.body).toHaveProperty('post')
})
})
The path I am testing is in the following bunch of code:
import { eventApplication } from './compositionRoot'
import { Schema } from 'mongoose'
export const mongoose = require('mongoose')
const express = require('express')
export const app = express()
const bodyParser = require('body-parser')
//const urlEncodedParser = bodyParser.urlencoded({ extended: false })
mongoose.connect('mongodb://localhost:27017/CompetitionEvent')
export const db = mongoose.connection
db.on('error', console.error.bind(console, 'An error has occured: '))
db.once('open', function () {
console.log('Connected to Mongodb')
})
const CompetitionSchema = new Schema({
id: String,
compName: String,
place: String,
time: String,
subscriptions: [],
date: Date,
cost: {
currency: String,
amount: Number,
},
})
const CompetitionModel = mongoose.model('CompetitionModel', CompetitionSchema)
app.use(bodyParser.json())
app.get('/events', (_req: any, res: any) => {
res.send(eventApplication.getAll())
})
app.post('/event', async (req: any, res: any) => {
await eventApplication.createAnEvent(req.body)
res.json({
success: true,
})
})
app.post('/new-comp', async (req: any, res: any) => {
const newComp = CompetitionModel(req.body)
const data = await newComp.save()
res.json(data)
})
// NEED TO ADD COMPETITION NAME FIELD!!!
app.listen(5000)
I keep having an issue with "TypeError app.address is not a function"
Also I noticed an async issue in the code that I am not (yet) able to identify, being a beginner in node. Anyone has an idea?
Cheers
Aucun commentaire:
Enregistrer un commentaire