javascript - Chai 单元测试意外的令牌 { 在 JSON 中的位置 25
问题描述
试图测试一个休息端点,但我得到了
索引页面应该呈现,你好世界
应该呈现 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;
解决方案
我决定采取不同的方法并使用超测。他们是 chai 读取 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;
});
});
})
推荐阅读
- css - 如何在 Dulst 中为卡片添加新的主要属性?
- tcp - 从以太网连接的控制器检索数据时出现问题
- python - 如何在一个脚本中使用 enchant 和 requests.future
- java - 分区方法中退出循环的问题
- javascript - 无法获取旧(以前)购物车的价值
- winapi - 访问物理内存地址
- c++ - Visual Studio C++ 2019,破碎不详细的智能感知
- excel - 以日期列为条件的 MAXIFS
- r - 使用 ggcompetingrisks 将审查形状添加到累积发生率图中
- javascript - 找不到模块'../../../../utils/Postgres'