开玩笑+ react:it.each()等待变量

发布于 2025-01-21 18:01:32 字数 2011 浏览 2 评论 0原文

我在带有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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

审判长 2025-01-28 18:01:32

我解决了这个问题,如果您需要解决方案在下面删除

首先我创建jest.config.js file
内容:

const fetch = (...args) => import('node-fetch').then(({ default: fetch }) => fetch(...args));

module.exports = async () => {
  const allData = await (await fetch('http://localhost:5050/files/data')).json();
  const fileList = (await (await fetch('http://localhost:5050/files/list')).json()).files;
    
  return {
    setupFiles: ['./jest.setup.js'],
    testEnvironment: 'jsdom',
    resetMocks: true,
    globals: {
      allData,
      fileList,
    },
  };
};

Store the fetched allData and the fileList data in global

我可以迟到,下一个创建jest.setup.js file
内容:

require('Whatwg-fetch');

仅对于组件中的提取错误
我全部,现在您可以访问获取的数据键入global.alldataglobal.filelist.filelist
或您定义的任何变量

global。

测试文件
示例:

describe('test table files', () => {
  it.each(global.allData)('testing $file', async ({ file, lines }) => {
    act(() => {
      ReactDOM.createRoot(container).render(
        <Provider store={store}>
          <HomePage />
        </Provider>
      );
    });

    // expect(lines).toMatchSnapshot();
  });
}

i solve this, if you need the solution is decribed below

first i create a jest.config.js file
with content:

const fetch = (...args) => import('node-fetch').then(({ default: fetch }) => fetch(...args));

module.exports = async () => {
  const allData = await (await fetch('http://localhost:5050/files/data')).json();
  const fileList = (await (await fetch('http://localhost:5050/files/list')).json()).files;
    
  return {
    setupFiles: ['./jest.setup.js'],
    testEnvironment: 'jsdom',
    resetMocks: true,
    globals: {
      allData,
      fileList,
    },
  };
};

Store the fetched allData and the fileList data in global

i can read late, next create a jest.setup.js file
with content:

require('whatwg-fetch');

only for the fetch error in your components
i'ts all, now you can access to the fetched data typing global.allData or global.fileList
or any variable you define with

global.you_variable_defined in jest.config.js globals section

in you test files
example:

describe('test table files', () => {
  it.each(global.allData)('testing $file', async ({ file, lines }) => {
    act(() => {
      ReactDOM.createRoot(container).render(
        <Provider store={store}>
          <HomePage />
        </Provider>
      );
    });

    // expect(lines).toMatchSnapshot();
  });
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文