javascript - 如何在测试中手动设置变量的值( Jest )?

标签 javascript node.js express jestjs supertest

app.test.js

<小时/>

我的 Jest 文件中有以下代码:

'use strict';
const request = require('supertest');
const app = require('./app');

//https://stackoverflow.com/questions/1714786/query-string-encoding-of-a-javascript-object
function serialise (obj) {
    return Object.keys(obj).map(k => `${encodeURIComponent(k)}=${encodeURIComponent(obj[k])}`).join('&');
}
describe('Test other /', () => {
    test('POST /example succeeds (200 OK) if checkboxes are ticked', () => {
        const toSend = {
            check: 'Spiderman'
        };
        return request(app).post('/example')
             .send(serialise(toSend))
             .expect(200);
    });
});

到目前为止测试很好,但我想在持续时间内设置一个名为 Identifier 的变量(在我的 node.js 文件中)等于值 1这个特定的测试。我怎样才能通过 Jest 做到这一点? (我尝试阅读 Jest 的文档,并查看了类似的问题,但无法找到更具体的答案)。

app.js

<小时/>

node.js/example POST 路由:

app.post('/example', (req, res) => {
    var checked = req.body.check;
    var Identifier = req.app.get('identifier'); // Accessed like a global variable (value set in previous block of code).

    ...

});

最佳答案

您可以使用app.set(name, value)去做这个。例如

app.js:

const express = require('express');
const bodyParser = require('body-parser');
const app = express();

app.use(bodyParser.urlencoded({ extended: false }));
app.post('/example', (req, res) => {
  const checked = req.body.check;
  const Identifier = req.app.get('identifier');
  console.log('Identifier:', Identifier);
  res.sendStatus(200);
});

module.exports = app;

app.test.js:

const request = require('supertest');
const app = require('./app');

function serialise(obj) {
  return Object.keys(obj)
    .map((k) => `${encodeURIComponent(k)}=${encodeURIComponent(obj[k])}`)
    .join('&');
}

describe('Test other /', () => {
  test('POST /example succeeds (200 OK) if checkboxes are ticked', () => {
    const toSend = {
      check: 'Spiderman',
    };
    return request(app).post('/example').send(serialise(toSend)).expect(200);
  });
  test('POST /example succeeds (200 OK) if Identifier is set', () => {
    const toSend = {
      check: 'Spiderman',
    };
    app.set('identifier', 1);
    return request(app).post('/example').send(serialise(toSend)).expect(200);
  });
});

集成测试结果:

 PASS  stackoverflow/61373586/app.test.js (12.805s)
  Test other /
    ✓ POST /example succeeds (200 OK) if checkboxes are ticked (159ms)
    ✓ POST /example succeeds (200 OK) if Identifier is set (9ms)

  console.log stackoverflow/61373586/app.js:9
    Identifier: undefined

  console.log stackoverflow/61373586/app.js:9
    Identifier: 1

Test Suites: 1 passed, 1 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        14.642s

关于javascript - 如何在测试中手动设置变量的值( Jest )?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61373586/

相关文章:

javascript - 有没有办法从 c3 data.columns 获取数据名称?

node.js - 如何在 Lambda 中使用 putParameter 和 aws-sdk for nodejs 获取过去的错误?

node.js - 保护 Twitter Webhook

mysql - 从 Heroku 上的 Node.js 连接到 CloudSQL

javascript - 将包含html的动态变量从angularjs加载到jade模板

javascript - 使用 Webstorm 调试 Nodewebkit

node.js - res.sendfile() 不能很好地服务于 javascript

javascript - 在加载时执行匿名函数,然后使用 setTimeout 每 30 秒再次执行一次

javascript - 如何包装 HTML 内容?

javascript - CouchDB 键中的关联数组?