简体   繁体   English

如何在测试(开玩笑)中手动设置变量的值?

[英]How do I manually set the value of a variable in a test (jest)?

app.test.js app.test.js


I have the following code in my jest file:我的笑话文件中有以下代码:

'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);
    });
});

The test is fine so far, but I would like to set a variable (in my node.js file) called Identifier equal to the value 1 for the duration of this particular test.到目前为止测试很好,但我想在这个特定测试期间设置一个名为Identifier的变量(在我的 node.js 文件中)等于值1 How can I do this via jest?我怎么能通过开玩笑来做到这一点? (I tried reading the documentation for jest and looked at similar question on SO but was unable to find a more specific answer). (我尝试阅读文档以开玩笑,并查看了关于 SO 的类似问题,但无法找到更具体的答案)。

app.js应用程序.js


node.js /example POST route: 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).

    ...

});

You can use app.set(name, value) to do this.您可以使用app.set(name, value)来执行此操作。 Eg例如

app.js : 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 : 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);
  });
});

integration test results:集成测试结果:

 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM