SuperTest app encounters an error: App is not recognized

I am currently delving into the world of test driven development and am taking on the challenge of using supertest to enhance my skills. I am facing an issue where I keep encountering the error message "app is not defined" when making calls to request(app), as highlighted in my code snippet below. I have attempted to consult the documentation for guidance, but unfortunately, it appears to be lacking. My routes are all contained within a file named "middleware", which begins as shown:

 var bodyParser = require('body-parser');
    var helpers = require('./helpers.js'); // our custom middleware
    var db = require('../DB/DB.js');
    var router = require('../routes.js');
    var path = require('path');
    var fs = require('fs');
    var gm = require('gm');


    module.exports = function (app, express) {}

In the actual file where I utilize supertest to test the routes within the middleware file:

    var chai = require('chai')
    var assert = chai.assert;
    var should = chai.should();
    var expect = chai.expect;
    var helpers = require("../config/helpers.js");
    var middleware = require("../config/middleware.js");
    // for when we eventually want to test against mock data
    var fs = require('fs');
    var path = require('path');
    var supertest = require("supertest")(middleware);

describe('middleware API', function() {


  it('responds with binary data', function(done) {
    var imagePath = path.join(__dirname, '/../assets/drawings/', userName + '.png');
    **request(app)**
      .get(imagePath)
      .expect(201)
      .expect('Content-Type', 'image.png')
      .parse(binaryParser)
      .end(function(err, res) {
        if (err) return done(err);

        // binary response data is in res.body as a buffer
        assert.ok(Buffer.isBuffer(res.body));
        console.log("res=", res.body);

        done();
      });
  });

  it('sends back one image', function(done) {
    **request(app)**
      .get('/game/')
      .expect(201)
      .expect('Content-Type', 'image.png')
      .expect('Content-Length', '1')
      .parse(binaryParser)
      .end(function(err, res) {
        if (err) return done(err);

        // binary response data is in res.body as a buffer
        assert.ok(Buffer.isBuffer(res.body));
        console.log("res=", res.body);

        done();
      })

  })
})

Answer №1

To begin, ensure that express and app are defined at the top of your file like so:

const express = require('express');
const app = express();

Additionally, be certain that express is installed by running the following command in the node.js command line:

npm install express --save

Using "--save" will not only add express to your package.json file for easy version tracking, but it will also automatically install express if someone else installs your project using npm install.

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

What is the process for verifying a Bootstrap form?

I have created a form using Bootstrap (Form component in ReactJS), but when I attempt to click on the submit button without entering any input, the form is still submitted. How can I implement form validation so that it only submits when all input fields a ...

Error message 'Access is Denied' occurs when using Angular.js xhr.open()

Currently, I am developing an angular web application that needs to be compatible with IE10. One of the requirements is to make a cross-domain call to our enterprise salesforce server. When using Chrome (not officially supported but commonly used for devel ...

pressing a button unrelated to the 'close' button still triggers the close event

I have a notification bar that features a button in the center that links to another website. There is also a 'close' button on the far right. However, whenever I click the center button, it also triggers the close button. I tried moving the #cl ...

Tips for enabling selection of list items in an input tag

Below is the code I have written to create an InputFilter. MyFilter = function(args) { var dataUrl = args.url; var divID = args.divID; var div = document.getElementById(divID); var myTable = '<input type="text" id="myInput" on ...

Implement a jQuery DataTable using JSON data retrieved from a Python script

I am attempting to create an HTML table from JSON data that I have generated after retrieving information from a server. The data appears to be correctly formatted, but I am encountering an error with DataTable that says 'CIK' is an unknown para ...

What is the best way to eliminate blank values ("") from an array?

I am working with a two-dimensional array that was generated from an HTML table using jQuery, but I have noticed that some values are empty and are displaying as "". How can I go about removing these empty values from the array? <table> ...

What are some ways to create a div section within a Google Map interface?

Is there a way to create a div area within the Google map iframe? Some of my code is already prepared here (). The image in this link (https://i.sstatic.net/92gkt.png) illustrates exactly what I'm trying to achieve. ...

JavaScript form callbacks in Rails 3 are failing to trigger

My Rails 3 callbacks are not triggering for some unknown reason. Here's the form code I'm using: <%= form_tag('/create', :method => "post", :remote => true ,:id => "create") do %> <% end %> And this is the javascr ...

Leveraging TipTap.dev for building a joint editing platform -

I have integrated the collaboration feature from tiptap.dev into my NextJS application. Initially, I used their CLI command for the Hocuspocus server which worked well on port 1234 locally and synchronized text editing across browsers seamlessly. However, ...

Is it feasible to develop ExpressJS/Node applications using CoffeeScript without the need for compilation? Similar to connect-assets for server-side

I've been exploring the connect-assets tool, which allows me to write CoffeeScript and Stylus (CSS) files without the need for manual compiling. It handles everything for me. I'm curious, is there a similar solution for server-side development? C ...

Utilizing jQuery for displaying or hiding list elements

To view all the code, click on this link: http://jsfiddle.net/yrgK8/ A section titled "news" is included in the code snippet below: <ul id="news"> <li><p>asfdsadfdsafdsafdsafdsafdsafdsafdsa</p></li> <li>&l ...

Toggle the visibility of text boxes based on the checkbox selection

After doing some research, I decided to revise the question after receiving feedback that it was causing some concern. When a checkbox is clicked, the content of the corresponding div should be visible and vice versa. How can I achieve this? Thank you. JQ ...

The performance of Jquery Animate is acting strangely after the upgrade to version 1.11

Take a look at http://jsfiddle.net/integerz/k19x8L1b/2/ which uses version 1.7.x $(function () { $("#clickme").toggle(function () { $(this).parent().animate({right:'0px'}); }, function () { $(this).parent().animate({righ ...

Error: Attempted to submit an invalid or unexpected input token

I want to display my ship registration number from the database in an AJAX response. I am using a method to send my field and then show the ship registration number in another function. Here is the code snippet that I have debugged: show_name(2d1c9a71586 ...

Is it possible to access a PHP variable from a different file in an HTML file?

Can someone help me with separating the javascript from this php file? I need to specify where the javascript should look for the php file, as currently it only executes within the php file. <?php date_default_timezone_set('Europe/London'); r ...

javascript enables smooth and continuous scrolling

<html> <style type="text/css"> #news { position: relative; box-shadow: 1px 4px 5px #aaa; text-align: left; padding: 5px; line-height: 20px; height: 235px; background: white; border: 1px solid #ccc; border-radius: 15px; background: #eee; wi ...

What steps do I need to take to implement AJAX form authentication?

I have set up a login screen that validates username and password credentials through a php script. When a user enters their information and submits the form, the authenticate.php file executes an LDAP bind. If the credentials are correct, the user is redi ...

Steps to activate a particular Bootstrap tab upon clicking a hyperlink

Trying to implement a Bootstrap tab panel in the following manner: <ul class="nav nav-tabs" role="tablist"> <li class="nav-item"> <a class="nav-link active" data-toggle="tab" href ...

Utilizing highlight.js for seamless integration with vue2-editor (Quill)

I am having trouble connecting vue2-editor (based on quill) with highlight.js Despite my efforts, I keep encountering an error message that reads: Syntax module requires highlight.js. Please include the library on the page before Quill. I am using nu ...

Obtain the URL from a Span Class located within a table

As I embark on my journey to learn javascript and jQuery, it's clear that my knowledge is quite rudimentary at this point. An attempt to make edits to a script written in Tampermonkey by a friend has led me down a path of extensive Googling with littl ...