Skip to content

Dropin with supertest #77

@zeta-squared

Description

@zeta-squared

While attempting to run an end-to-end test with jest and supertest, I was getting the following error:
TypeError: Invalid URL
After some troubleshooting I realised this is an issue with the ultimate-express application. I switched the application back to a standard express application and with no other changes the tests ran as expected with no exceptions. I have included a dummy application that can recreate this problem.

index.ts

import express from 'ultimate-express';

export default function createApp() {
  const app = express();

  app.use(express.json());
  app.use(express.urlencoded());
  app.get('/:name', (req, res): any => {
    const name = req.params.name;
    return res.status(200).json({hello: name ? `${name}` : 'world'});
  })

  return app;
}

index.test.ts

import { describe, test, expect } from '@jest/globals';
import request from 'supertest';
import createApp from './index';

const app = createApp();

describe('dummy test', () =>{
  test('basic route', async () => {
    const name = 'name';
    const response = await request(app).get(`/${name}`);
    expect(response.status).toEqual(200);
    expect(response.body.hello).toBe(name);
  })
})

package.json

{
  "name": "test",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "test": "jest"
  },
  "author": "",
  "license": "ISC",
  "description": "",
  "dependencies": {
    "express": "^4.21.1",
    "ultimate-express": "^1.3.17"
  },
  "devDependencies": {
    "@jest/globals": "^29.7.0",
    "@types/express": "^5.0.0",
    "@types/node": "^22.10.1",
    "@types/supertest": "^6.0.2",
    "jest": "^29.7.0",
    "supertest": "^7.0.0",
    "ts-jest": "^29.2.5",
    "typescript": "^5.7.2"
  }
}

tsconfig.json

{
  "include": ["./index.ts"],
  "compilerOptions": {
    "esModuleInterop": true,
    "skipLibCheck": true
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions