Hi,I'm writing tests in order to check if all my CRUD routes are working well. I must test a post route, that receives an object. This object contains an array of objects made of several informations provided by the user in a form (the structure has no importance here). The route then parses it, and create a new entry in a postgresql database,made with Knex and Objection.js.Here is the test:import express from 'express' import request from 'supertest' import MyModel from 'server/models/MyModel' import apiRouter from './apiRouter' describe('Test MyModel CRUD', () => { let app let mymodel const firstModel = { id: 1, name: 'My great company', fields: { data: [ { activity: 'Selling great stuffs', turnover: '10K$', employees: 2, }, { code: '34567', location: 'NY', followers: 5, }, ], }, } beforeEach(async () => { model = await MyModel.query().insertAndFetch(firstModel) app = express() app.use(apiRouter) }) it('should create a company', async () => { const companiesList = await request(app).get('/companies') const response = await request(app) .post('/companies', firstMapping) .expect(200) const companiesListAfterCreation = await request(app).get('/companies') expect(companiesListAfterCreation).toHaveLength(companiesList.length + 1) expect(response).toEqual(firstModel) }) The route is:router.post( '/companies', asyncHandler(async (req, res) => { const model = await Model.query().insertAndFetch({ name: req.body.name, fields: { data: req.body.fields }, }) res.status(201).send(model) }), ) The test fails. Why?
Submitted July 11, 2019 at 09:26AM by MonsieurLeland
No comments:
Post a Comment