柴单元测试意外令牌{在JSON中的位置25

时间:2019-04-09 03:06:36

标签: javascript express mocha chai

尝试测试其他端点,但即时通讯

  

索引页应该呈现,您好,世界

     

应该呈现hello world 200:

 SyntaxError: Unexpected token { in JSON at position 25

不确定我在做什么错。

引用此

https://github.com/chaijs/chai

router.spec.js

import chai from 'chai';
import { expect } from 'chai';
import chaiHttp from 'chai-http';
import { assert } from 'assert'
import router from '../routes/';

chai.use(chaiHttp);


// simple test
// describe('Array', () => {
//       it('should return -1 when the value is not present', () => {
//         assert.equal([1, 2, 3].indexOf(4), -1);
//       });
// });



describe('index page should render, hello world', () => {
it('should render hello world 200', () => {     
  return chai.request(router)
   .get('/')
   .end((err, res) => {
        res.should.have.status(200);
        res.body.should.be.a('object');
        res.body.length.should.be.eql(0);
     done();
   });
 });   
})

index.js

import express from 'express';

const app = express();

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

export default app;

1 个答案:

答案 0 :(得分:0)

我决定采用其他方法,并使用了超级测试。他们是柴阅读json对象的问题。

import chai from 'chai';
import { expect } from 'chai';
import chaiHttp from 'chai-http';
import { assert } from 'assert'
import router from '../routes/';
import request from 'supertest';

chai.use(chaiHttp);


// simple test
// describe('Array', () => {
//       it('should return -1 when the value is not present', () => {
//         assert.equal([1, 2, 3].indexOf(4), -1);
//       });
// });



describe('index page should render 200 request', () => {
  it('should get index 200', () => {     
    request(router)
      .get('/')
      .set('Accept', 'application/json')
      .expect('Content-Type', /json/)
      .expect(200)
      .end((err, res) => {
        if (err) throw err;
      });
    });  
})