在Chai中进行单元测试时无法读取未定义的属性“应用”

试图测试一个简单的快速休息,我得到


 Uncaught TypeError: Cannot read property 'apply' of undefined

  at Immediate.<anonymous> (node_modules/express/lib/router/index.js:635:15)

不知道我在做什么错。


我引用了类似的线程,但它不是特定于单元测试的


无法读取未定义的属性“ apply”


引用这个


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"

  }

}


繁华开满天机
浏览 207回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript