2

Trying to test a simple express rest, and im getting

 Uncaught TypeError: Cannot read property 'apply' of undefined
  at Immediate.<anonymous> (node_modules/express/lib/router/index.js:635:15)

Not sure what i am doing wrong.

I referenced similar threads but it wasn't specific to unit testing

Cannot read property 'apply ' of undefined

referencing this

https://codehandbook.org/unit-test-express-route/

router.spec.js

import chai from 'chai';
import chaiHttp from 'chai-http';
import router from '../routes/';

chai.use(chaiHttp);
chai.should();

const expect = chai.expect();

describe('index page should render, hello world', () => {
    it('should render hello world 200', (done) => {     
        chai.request(router).get('/').end( (err, res) => {   
            expect(200, "ok").
            expect(res.text).to.equal('Hello World');

            done();
        })    
    })
})

index.js

import express from 'express';

const router = express.Router();

router.get('/', (req, res) => {
    return res.status(200).json({
        message: "Hello World"
    })
})




export default router;

.babelrc

{
    "presets": ["@babel/preset-env"]  
}

package.json

{
  "name": "elinodereactapp",
  "version": "1.0.0",
  "description": "",
  "scripts": {
    "start": "nodemon --exec babel-node ./app.js",
    "test": "mocha --require @babel/register tests/*.js --exit",
    "build": "babel src --out-dir ./dist --source-maps",
    "serve": "node ./app.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "assert": "^1.4.1",
    "body-parser": "^1.18.3",
    "chai-http": "^4.2.1",
    "cookie-parser": "^1.4.4",
    "cors": "^2.8.5",
    "dotenv": "^7.0.0",
    "express": "^4.16.4",
    "morgan": "^1.9.1",
    "node-mocks-http": "^1.7.3"
  },
  "devDependencies": {
    "@babel/cli": "^7.4.3",
    "@babel/core": "^7.4.3",
    "@babel/node": "^7.2.2",
    "@babel/preset-env": "^7.4.3",
    "@babel/register": "^7.4.0",
    "babel-cli": "^6.26.0",
    "babel-core": "^7.0.0-bridge.0",
    "babel-loader": "^8.0.5",
    "babel-preset-env": "^1.7.0",
    "chai": "^4.2.0",
    "mocha": "^6.1.1",
    "nodemon": "^1.18.10"
  }
}
randal
  • 1,272
  • 3
  • 23
  • 48

1 Answers1

2

U need to use res.body.message instead of res.message

describe('index page should render, hello world', () => {
it('should render hello world 200', (done) => {     
    chai.request(router).get('/').then((res)=>{
                expect(res.body.message).to.equal("Hello World");
                expect(res).to.have.status(200);
                done();
            })
    })    
})
Shivam
  • 3,514
  • 2
  • 13
  • 27
  • i still get ` Uncaught TypeError: Cannot read property 'apply' of undefined` i think this has something to do with the babel. I think its a babel issue not being able to recognize es6. I think this answer does work, just could be an issue with mocha. – randal Apr 08 '19 at 20:16
  • That could be true as i never got this weird error. Try removing babel and use native version – Shivam Apr 08 '19 at 20:21
  • use this https://www.npmjs.com/package/babel-preset-es2015-native-modules ? – randal Apr 08 '19 at 20:24