admin管理员组文章数量:1366902
In order that I can test multiple data files against multiple criteria, I have some (distilled) code that looks like this:
describe.each([ ['Data 1', 'd1'], ['Data 2', 'd2']])('Test group using %s with tag %s', (datafile, tag) =>
{
let groupData = [{'id':10},{'id':20}];
let groupTag = '';
beforeEach(() => {
groupTag = tag;
console.log('Beginning tests for '+tag+' using '+datafile);
if(tag == 'd1') groupData = [{'id':30},{'id':40}];
if(tag == 'd2') groupData = [{'id':50},{'id':60}];
console.log(JSON.stringify(groupData[0]));
});
test.each(groupData)('Test case $# [tag:sandbox]', row => {
console.log(groupTag+' - Running test '+row.id);
expect(row.id).toBeDefined(); //example test
});
});
The console.log output is like this (with my comments added):
- Beginning tests for d1 using Data 1
- {"id":30}
- d1 - Running test 10 **should be 30**
- Beginning tests for d1 using Data 1
- {"id":30}
- d1 - Running test 20 **should be 40**
- Beginning tests for d2 using Data 2
- {"id":50}
- d2 - Running test 10 **should be 50**
- Beginning tests for d2 using Data 2
- {"id":50}
- d2 - Running test 20 **should be 60**
So my question is twofold:
- Is my expectation that
beforeEach
can be used to reset thetest.each
data table incorrect (i.e. the table is invariant at runtime)? - How can I achieve the result I am looking for?
In order that I can test multiple data files against multiple criteria, I have some (distilled) code that looks like this:
describe.each([ ['Data 1', 'd1'], ['Data 2', 'd2']])('Test group using %s with tag %s', (datafile, tag) =>
{
let groupData = [{'id':10},{'id':20}];
let groupTag = '';
beforeEach(() => {
groupTag = tag;
console.log('Beginning tests for '+tag+' using '+datafile);
if(tag == 'd1') groupData = [{'id':30},{'id':40}];
if(tag == 'd2') groupData = [{'id':50},{'id':60}];
console.log(JSON.stringify(groupData[0]));
});
test.each(groupData)('Test case $# [tag:sandbox]', row => {
console.log(groupTag+' - Running test '+row.id);
expect(row.id).toBeDefined(); //example test
});
});
The console.log output is like this (with my comments added):
- Beginning tests for d1 using Data 1
- {"id":30}
- d1 - Running test 10 **should be 30**
- Beginning tests for d1 using Data 1
- {"id":30}
- d1 - Running test 20 **should be 40**
- Beginning tests for d2 using Data 2
- {"id":50}
- d2 - Running test 10 **should be 50**
- Beginning tests for d2 using Data 2
- {"id":50}
- d2 - Running test 20 **should be 60**
So my question is twofold:
- Is my expectation that
beforeEach
can be used to reset thetest.each
data table incorrect (i.e. the table is invariant at runtime)? - How can I achieve the result I am looking for?
1 Answer
Reset to default 0const dataArray = [['Data 3', 'd3'], ['Data 4', 'd4']];
dataArray.forEach(row => {
describe('Test group using %s with tag %s', () =>
{
let groupData = [{'id':'01'},{'id':'02'}];
let groupTag = '';
let datafile = '';
switch(row[1])
{
case 'd3': groupData = [{'id':'31'},{'id':'32'}]; break;
case 'd4': groupData = [{'id':'41'},{'id':'42'}]; break;
}
datafile = row[0];
groupTag = row[1];
console.log('Beginning tests for '+groupTag+' using '+datafile+'\r\n groupData set to '+JSON.stringify(groupData[0])+' and '+JSON.stringify(groupData[1]));
groupData.forEach(num => test('Test case ${num} [tag:sandbox]', () =>
{
console.log(groupTag+' - Running test '+num.id);
expect(num.id).toBeDefined(); //example test
}));
});
});
This got the 'desired' result, defining all the tests first, then running them
console.log Beginning tests for d3 using Data 3 groupData set to {"id":"31"} and {"id":"32"} Beginning tests for d4 using Data 4 groupData set to {"id":"41"} and {"id":"42"} d3 - Running test 31 d3 - Running test 32 d4 - Running test 41 d4 - Running test 42
hat tip to the linked question Using jest's test.each vs. looping with forEach
and https://stackoverflow/users/3001761/jonrsharpe for his comment.
版权声明:本文标题:jestjs - Trying to use beforeEach to modify the table sent to test.each within describe.each - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743782279a2538051.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
beforeEach
is only going to be called at test run time, whereastest.each
is called at test discovery time. If you already know all of the data anyway, why not just write regular JS loops (ref)? – jonrsharpe Commented yesterday