admin管理员组文章数量:1023195
app.test.js
I have the following code in my jest file:
'use strict';
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);
});
});
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. 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).
app.js
node.js /example POST route:
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.test.js
I have the following code in my jest file:
'use strict';
const request = require('supertest');
const app = require('./app');
//https://stackoverflow./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. 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).
app.js
node.js /example POST route:
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).
...
});
Share
Improve this question
edited Jun 20, 2020 at 9:12
CommunityBot
11 silver badge
asked Apr 22, 2020 at 19:31
bgmnbgmn
5082 gold badges10 silver badges28 bronze badges
4
- try changing the url from '/example' to '/example?identifier=1' in the test? – Odinn Commented Apr 22, 2020 at 19:37
- This isn't working unfortunately – bgmn Commented Apr 22, 2020 at 20:15
- even if you change req.app to req.query? – Odinn Commented Apr 22, 2020 at 20:17
- That's my node.js file, I am looking to change the jest.js file for the test. I apologise for not making that clear. – bgmn Commented Apr 22, 2020 at 20:20
1 Answer
Reset to default 1You can use app.set(name, value) to do this. E.g.
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);
});
});
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
app.test.js
I have the following code in my jest file:
'use strict';
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);
});
});
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. 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).
app.js
node.js /example POST route:
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.test.js
I have the following code in my jest file:
'use strict';
const request = require('supertest');
const app = require('./app');
//https://stackoverflow./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. 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).
app.js
node.js /example POST route:
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).
...
});
Share
Improve this question
edited Jun 20, 2020 at 9:12
CommunityBot
11 silver badge
asked Apr 22, 2020 at 19:31
bgmnbgmn
5082 gold badges10 silver badges28 bronze badges
4
- try changing the url from '/example' to '/example?identifier=1' in the test? – Odinn Commented Apr 22, 2020 at 19:37
- This isn't working unfortunately – bgmn Commented Apr 22, 2020 at 20:15
- even if you change req.app to req.query? – Odinn Commented Apr 22, 2020 at 20:17
- That's my node.js file, I am looking to change the jest.js file for the test. I apologise for not making that clear. – bgmn Commented Apr 22, 2020 at 20:20
1 Answer
Reset to default 1You can use app.set(name, value) to do this. E.g.
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);
});
});
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
本文标签: javascriptHow do I manually set the value of a variable in a test (jest)Stack Overflow
版权声明:本文标题:javascript - How do I manually set the value of a variable in a test (jest)? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745592816a2157986.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论