How to make a mail request using node using chai in mocha and mongoDB

  • How to make a POST request using node using chai in mocha and mongoDB. I have a test file containing both my receive request and the mail request. I have the code below equal to my receive request, which passed test 1, for which I configured it, but it is difficult for me to create my request for sending, and I do not understand what I should do. GET request:

    const chai = require('chai'); const expect = chai.expect; const chaiHttp = require('chai-http') chai.use(chaiHttp) describe('site', () => { // Describe what you are testing it('Should have home page', (done) => { // Describe // In this case we test that the home page loads chai.request('localhost:3000') chai.get('/') chai.end((err, res) => { if (err) { done(err) } res.status.should.be.equal(200) done() // Call done if the test completed successfully. }) }) }) 

  1. This is my post / create a route:

  2. The pseudocode of this request is:

  3. // How many messages exist now?
  4. // Make a request to create another
  5. // Make sure there is another entry in the database.
  6. // Make sure the answer is successful

  7. POST request:

     const chai = require('chai') const chaiHttp = require('chai-http') const should = chai.should() chai.use(chaiHttp) const Post = require('../models/post'); describe('Posts', function() { this.timeout(10000); let countr; it('should create with valid attributes at POST /posts', function(done) { // test code Post.find({}).then(function(error, posts) { countr = posts.count; let head = { title: "post title", url: "https://www.google.com", summary: "post summary" } chai.request('localhost:3000') chai.post('/posts').send(head) chai.end(function(error, res) { //console.log('success') }) }).catch(function(error) { done(error) }) }); }) 
  8. Any pointers to what I am doing wrong are appreciated. My output for this error:

1) Messages should return a new message on POST / posts: Error: timeout exceeded by 10000 ms. For asynchronous tests and interceptors, make sure that "done ()" is called; if renewing a promise, make sure it is resolved.

npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! reddit_clone@1.0.0 test: mocha

+5
source share
1 answer

The test gives time because the request is never sent.

Based on docs, I don't see anything that tells you that you can execute a post simply by passing data to the post function. There should be many assumptions made by the library here, for example. serialization type, headers, etc.

The correct way to execute a POST request with a JSON payload would be:

 chai .request('http://localhost:3000') .post('/posts') .send(head) .end((err, res) => { ... }); 
0
source

Source: https://habr.com/ru/post/1274913/


All Articles