开玩笑+ react:it.each()等待变量
我在带有react的嘲笑文件中有此代码:
import React from 'react';
import thunk from 'redux-thunk';
import { Provider } from 'react-redux';
import ReactDOM from 'react-dom/client';
import { act } from 'react-dom/test-utils';
import HomePage from '../../pages/HomePage';
import configureStore from 'redux-mock-store';
const middlewares = [thunk];
const mockStore = configureStore(middlewares);
jest.setTimeout(15000);
global.IS_REACT_ACT_ENVIRONMENT = true;
describe('test table files', () => {
let list;
let allData;
let store = mockStore({});
let container = document.createElement('div');
beforeEach((done) => {
fetch('http://localhost:5050/files/data')
.then((res) => res.json())
.then((dat) => {
allData = dat;
fetch('http://localhost:5050/files/list')
.then((res) => res.json())
.then((fil) => {
list = fil.files;
done();
});
});
const allReducers = {
files: {
list,
allData,
},
};
store = mockStore(allReducers);
container = document.createElement('div');
document.body.appendChild(container);
});
afterEach(() => {
document.body.removeChild(container);
container = null;
});
it.each(allData)('testing $file', async ({ file, lines }) => {
console.log('file', file);
act(() => {
ReactDOM.createRoot(container).render(
<Provider store={store}>
<HomePage />
</Provider>
);
});
expect(lines).toMatchSnapshot();
});
});
i ran jest and the variable
allData
is undefinedi need to run one test for every children in
allData
Array获取的
alldata
是从API
请帮助我找到一个没有任何外部库的解决方案
请记住,我需要等待alldata
是从外部API中获取的
i have this code in a jest file with react:
import React from 'react';
import thunk from 'redux-thunk';
import { Provider } from 'react-redux';
import ReactDOM from 'react-dom/client';
import { act } from 'react-dom/test-utils';
import HomePage from '../../pages/HomePage';
import configureStore from 'redux-mock-store';
const middlewares = [thunk];
const mockStore = configureStore(middlewares);
jest.setTimeout(15000);
global.IS_REACT_ACT_ENVIRONMENT = true;
describe('test table files', () => {
let list;
let allData;
let store = mockStore({});
let container = document.createElement('div');
beforeEach((done) => {
fetch('http://localhost:5050/files/data')
.then((res) => res.json())
.then((dat) => {
allData = dat;
fetch('http://localhost:5050/files/list')
.then((res) => res.json())
.then((fil) => {
list = fil.files;
done();
});
});
const allReducers = {
files: {
list,
allData,
},
};
store = mockStore(allReducers);
container = document.createElement('div');
document.body.appendChild(container);
});
afterEach(() => {
document.body.removeChild(container);
container = null;
});
it.each(allData)('testing $file', async ({ file, lines }) => {
console.log('file', file);
act(() => {
ReactDOM.createRoot(container).render(
<Provider store={store}>
<HomePage />
</Provider>
);
});
expect(lines).toMatchSnapshot();
});
});
i ran jest and the variable allData
is undefined
i need to run one test for every children in allData
Array
the
allData
is fetched from api
please help me to find a solution without any external library
keep in mind that i need wait for allData
is fetched from a external api
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我解决了这个问题,如果您需要解决方案在下面删除
首先我创建
jest.config.js
file内容:
Store the fetched
allData
and thefileList
data in global我可以迟到,下一个创建
jest.setup.js
file内容:
仅对于组件中的提取错误
我全部,现在您可以访问获取的数据键入
global.alldata
或global.filelist.filelist
或您定义的任何变量
测试文件
示例:
i solve this, if you need the solution is decribed below
first i create a
jest.config.js
filewith content:
Store the fetched
allData
and thefileList
data in globali can read late, next create a
jest.setup.js
filewith content:
only for the fetch error in your components
i'ts all, now you can access to the fetched data typing
global.allData
orglobal.fileList
or any variable you define with
in you test files
example: