Express.js,Jest,Supertest,Handlebars..js-呈现模板,尝试访问res.body变量进行测试

时间:2020-04-17 08:25:54

标签: express jestjs handlebars.js supertest express-handlebars

用玩笑/超级测试进行测试,以查看res.render主体变量(handlebars.js)

我正在尝试对查看一组URL的站点进行测试,并做一个简单的测试来检查是否存在“ pagetitle”,我正在使用: Express.js:开玩笑:Supertest和Handlebars.js,

我有一个测试,该测试将循环urls的数组,并想对已定义的可变“ pagetitle”的“ urls”进行测试。

问题 我无法访问变量pagetitle /任何其他变量。该测试正在运行,并将快速路由记录为代码200。我已经使用示例JSON对res.send进行了测试

{name :"john"} 

这确实有效,并返回res.body json对象。通过本地浏览器调用变量时,将成功返回变量。

我已经以各种方式对此进行了测试,并且怀疑它与res.render有关,它从路由中剥离了res.body / res.locals。 res.render或测试未在等待通过async函数成功返回,但是如果出现问题,我不知道如何进行此处理。另外,通过测试返回的res.render路由也使用返回的模板填充测试res.text,我认为这排除了asnyc问题。

可能与bodyparser,端口或服务器设置有关,但是对此进行了探索,排除了对超级测试功能的基本误解。

原则上,supertest在服务器上运行,该测试已启动并通过,并以200个代码开头,但是返回的路由中没有变量。

大量的console.log用于显示所查询的各个项目。

Package.json

  "scripts": {
    "test:watch": "jest  --detectOpenHandles  --watchAll --maxWorkers=1"
  },

快速路由

 exports.introduction = function(req, res) {
  console.log(res.locals.mybody)
  res.render(directory+'introduction',{
    pagetitle: 'Introduction | '+sitename+'',
    siteName : sitename,
  })
}; 

测试文件

const request = require('supertest');
const express = require('express');
const app = require('../index.js')
app.set('port', process.env.PORT || 3001);
var siteIndex = [{
  url: '/introduction',
  lastMod: new Date('2000-02-02'),
  changeFreq: 'weekly',
  requireslogin : false
}]

控制台错误

 FAIL  tests/fraternateendpoint.test.js

console.log index.js:119
  [Object: null prototype] { mybody: 'pass a variable' }
console.log node_modules/fraternate/controllers/fraternatepages.js:53
  pass a variable
console.log tests/fraternateendpoint.test.js:115
  This is empty {}
console.log tests/fraternateendpoint.test.js:116
  This is undefined undefined
console.log tests/fraternateendpoint.test.js:117
  This is undefined undefined
console.log tests/fraternateendpoint.test.js:118
  This is undefined undefined
console.log tests/fraternateendpoint.test.js:119
  This is undefined undefined
console.log tests/fraternateendpoint.test.js:120
  This is empty undefined
console.log tests/fraternateendpoint.test.js:121
  This is empty undefined   end point testing of sitemap › given "/introduction" endpoint should return a 200, and a pagetitle should exist.

TypeError: Cannot read property 'mybody' of undefined

  120 |               console.log('This is empty',res.body.mybody)
  121 |               console.log('This is empty',res.mybody)
> 122 |               console.log('This is undefined',res.locals.mybody)
      |                                                          ^
  123 |               console.log('This is undefined',res.req.locals.mybody)
  124 |               console.log('This is undefined',res.res.locals.mybody)
  125 |               console.log('This is undefined',res.exphbs.mybody)

  at Test.<anonymous> (tests/fraternateendpoint.test.js:122:58)
  at Test.assert (node_modules/supertest/lib/test.js:181:6)
  at Server.localAssert (node_modules/supertest/lib/test.js:131:12)

0 个答案:

没有答案