Travis is checking to see if the undefined value is being

My experience with jasmine testing has been successful when done locally. However, I am encountering issues on Travis CI where all the API tests are returning undefined values. Here is an example:

4) Checking Server Status for GET /api/v1/orders - Expected 200 Message: The value returned was expected to be 200 but instead it was undefined. Stack: Error: The value returned was expected to be 200 but instead it was undefined. at

Here is a snippet from the test cases:

  describe('GET /api/v1/orders', function () {
    var data = {};
    beforeAll(function (done) {
      Request.get('http://localhost:3001/api/v1/orders', function (error, response, body) {
        data.status = response.statusCode;
        data.body = JSON.parse(body);
        data.number = data.body.length;
        done();
      });
    });
    it('Should have a status of 200', function () {
      expect(data.status).toBe(200);
    });
    it('Expected return of three Items', function () {
      expect(data.number).toBe(3);
    });
  });

Could the issue possibly stem from the 'http://localhost:3001/api/v1/orders' URL?

Answer №1

It appears that your server is not being started anywhere, which means localhost:3001 is currently unavailable.

An effective solution would be to utilize a tool such as supertest. This will enable you to implement the following:

app.js

const express = require('express');
const bodyParser = require('body-parser');

const app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

const routes = require('./api/routes/routes.js')(app);

// We only start listening if this file is executed directly
if (require.main === module) {
    const server = app.listen(3001, () => {
        console.log('Server is running on port %s...', server.address().port);
    });
} else {
    // If this file is required elsewhere, we export our app
    module.exports = app;
}

spec.routes.js

'use strict';

var request = require('supertest');

var app = require('../../app.js');

describe("Testing the server", () => {
    // Manually starting the server here
    const server = app.listen();

    // Closing the server after all tests are completed
    afterAll(() => {
        server.close();
    });

    it("should properly return orders", async () => {
        // In case async/await is not supported, use a callback function
        await request(server)
            .get('/api/v1/orders')
            .expect(res => {
                expect(res.body.length).toBe(3);
                expect(res.status).toBe(200);
            });
    });
});

By using Supertest, you can make requests without depending on a specific port, URL, or other factors.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Dealing with the output from the response.write() function in Express within an Angular $http request

Currently, I am attempting to upload a CSV file using the ng-file-upload library. Below is a snippet of my code: Upload.upload({ url: baseUrl + '/file-upload', data: { file: file } }) .then(function(res) { console.log(' ...

Creating a CSS full-width navigation menu

Recently, I came across a menu code on the web that seemed great. However, I wanted to make my menu responsive and full width. Since I am new to CSS and HTML, adjusting the menu code has been a bit of a challenge for me. .menu, .menu ul { list-style: ...

Need to know how to show a DIV for a specific time frame using Javascript?

One of the features I am looking to implement in my Django application is a display that notifies the user when one of their posts is about to start. The idea is to show a DIV element with the message: <div>“Will start soon”</div>, 15 minut ...

Iterate over Observable data, add to an array, and showcase all outcomes from the array in typescript

Is there a way to iterate through the data I've subscribed to as an Observable, store it in an array, and then display the entire dataset from the array rather than just page by page? Currently, my code only shows data from each individual "page" but ...

Utilizing node.js for manipulating files (JSON) through reading and writing operations

There is an issue with my function that reads a JSON file and updates it using the fs.writeFile method. When I invoke this function multiple times, it fails to update the file properly. After the first call, it adds extra curly brackets at the end of the J ...

Creating an asynchronous function using EventEmitter

I am new to node.js and I'm trying to take advantage of asynchronous and event-driven behavior in my code. I used to think that in node, anything involving an Event object would result in asynchronous execution. So I decided to test this theory with ...

Showing a restricted number of rows in the JSP page

<table class="grid_alt" cellspacing="0" rules="all" border="1" id="id1" style="width:720px;border-collapse:collapse;"> <tbody> <tr align="left"> <th scope="col"><%=partner %></th><th scope="col"><%=item %>< ...

Issue encountered: Inability to implement asynchronous functionality within a forEach loop while making an API request

When making a GET API call, the code looks like this router.get('/review', async (req, res) => { try { const entity = await Entity.find(); const entityId = []; Object.keys(entity).forEach((key) => { entityId.push(entity[ ...

Ways to retrieve a variable from outside of a function

I am in need of sending the following batch data to the database, but I am facing difficulties in including the course_id along with the batchData. Currently, I am retrieving the course_id from another service that fetches data from a course table. I am ...

Removing other objects with Mongoose after an update

I'm facing an issue with my update query in mongoose. I can't figure out why other objects are getting deleted when I only intend to update one specific object. The code is functioning correctly in terms of updating, but it's causing all the ...

Analyzing the HTTP status codes of various websites

This particular element is designed to fetch and display the HTTP status code for various websites using data from a file called data.json. Currently, all sites are shown as "Live" even though the second site does not exist and should ideally display statu ...

A JavaScript right-click menu that updates a variable when clicked

Within my three-dimensional application created with three.js, I have implemented a functionality where right-clicking on floors (BoxGeometry) triggers a context menu to select from various textures. While this feature works as intended for a single floor, ...

Error: Invalid Syntax Detected in React

I encountered an error that reads as follows: events.js:72 throw er; // Unhandled 'error' event ^ SyntaxError: /vagrant/resources/assets/js/react/react_app.js: Unexpected token (366:10) 364 | var InvestorsTable = React.cr ...

Trouble with Vue.js: Failure to render data

Currently, I am delving into the Vue.js framework and experimenting with ways to effectively utilize this powerful JavaScript framework. Although my example is straightforward, I am facing difficulties in properly showcasing the data {} as outlined in bot ...

Utilize the onChangeText function in React Native to filter out each individual value within a nested array

I currently have an array with nested children in it, where the children can range from one to multiple values. I am working on implementing a local filter within my component. Whenever a user types anything into the textInput, the application will display ...

Serializing ajax calls using `WHEN` and `DONE` in jQuery

I have several ajax methods that need to be executed, and I want to run some code after all of them have successfully completed. I am unable to modify or redefine the ajax methods. Can you please advise me on how to achieve this? I attempted to use WHEN b ...

What could be the reason for not seeing any console.log output while executing findOne using Mongoose?

My goal is to query my MongoDB database using Mongoose. I am searching for the string 13 in the field eClassSegment within the collection eclasses. When I run the code, something gets printed in the console. Why is that? Here is the code I am using: var ...

Alternative names for Firefox's "error console" in different browsers

Are there similar tools to Firefox's "Error console" available in other web browsers? I rely on the error console for identifying JavaScript errors, but I haven't found a straightforward way to view error messages in other browsers like Internet ...

Refresh the view when the URL is modified

Utilizing angularjs alongside ui-router (using the helper stateHelperProvider) to organize views and controllers on the page. Encountering an issue where the views are not updating as expected. The relevant code snippet config.js app.config(function($h ...

Implementing JavaScript to Retrieve and Insert Full HTML Tags into a Textarea

I am currently trying to extract an HTML source code value and insert it into a specific textarea or div upon clicking a button. However, I am encountering issues where I am not receiving the entire HTML tags - it seems to begin with a Meta tag and is remo ...