javascript - 无法使用 mocha 和 supertest 测试 OPTIONS 方法

标签 javascript node.js testing mocha.js supertest

相关问题Cant test DELETE method using mocha and supertest它目前的两个答案并没有解决我的问题:(

下面是测试定义:

api = supertest(url);

describe('when OPTIONS', function () {
    it('should return only method GET', function (done) {
      api
        .options('/')
        .expect('Allow', 'GET')
        .expect(200, done);
    });
  });

它不断触发以下错误:

Uncaught TypeError: Cannot read property 'header' of undefined
      at _stream_readable.js:944:16

以及何时:

 api
        .options('/')
        .end(function(error,res){
          if (error) return done(error);
          done()
        });

它说:

SyntaxError: Unexpected token G
      at Object.parse (native)
      at _stream_readable.js:944:16

我已经尝试了很多东西,但无法完成这项工作。

我正在使用 super 测试 ^1.0.1,它在 ~0.13.0 版本上运行良好。

相关问题在 https://github.com/visionmedia/supertest/issues/272

谢谢

最佳答案

我已经检测到这个问题,我在 API 中返回了一个带有 Content-Type: application/json 的 OPTIONS 响应,但正文是一个无效的 JSON(它是 GET,HEAD) .

关于javascript - 无法使用 mocha 和 supertest 测试 OPTIONS 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32147948/

相关文章:

Javascript 无法读取 null 的属性 'innerText' - puppeteer

javascript - 如何在 JavaScript 中访问 JSON 或对象的特定属性

django - 如何测试具有 transaction.atomic(using=myDb) 的 Django View ?

maven - 如何将 Maven 日志中的测试运行次数与源代码匹配?

javascript - 新行之前的正则表达式检查字符不起作用

javascript - 无法使用 autoprefixer 使 grunt.js 任务 postcss 工作

javascript - 文本区域的自定义占位符

javascript - 从 angular2 构建的新 Cordova 在 android 上构建白屏

visual-studio - 在 Visual Studio 中为 REST Api 负载测试选择哪个测试组合模型选项?

JavaScript:过滤数组元素/对象