Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/456.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在Jest中清除函数中的值_Javascript_Jestjs_React Testing Library - Fatal编程技术网

Javascript 在Jest中清除函数中的值

Javascript 在Jest中清除函数中的值,javascript,jestjs,react-testing-library,Javascript,Jestjs,React Testing Library,我正在尝试测试一个表单,该表单使用从sessionStorage读取值,但我的测试失败,因为在每个测试之间没有清除状态: import React from 'react' import { render } from '@testing-library/react' import { StateMachineProvider, createStore } from 'little-state-machine' import MyFormComponent from './MyForm'

我正在尝试测试一个表单,该表单使用从
sessionStorage
读取值,但我的测试失败,因为在每个测试之间没有清除状态:

import React from 'react'
import { render } from '@testing-library/react'

import { StateMachineProvider, createStore } from 'little-state-machine'

import MyFormComponent from './MyForm'

const goodState = { // some value }
const badState = { // some value }
 
describe("MyComponent with good data", () => {
    beforeEach(() => {
      createStore(goodState, { name: "TestState" })
    })

    it("should render with state machine values", () => {
      render(
        <StateMachineProvider>
          <MyFormComponent />
        </StateMachineProvider>
      )

      // tests here work
    })
})

describe("MyComponent with bad data", () => {
    beforeEach(() => {
      createStore(badData, { name: "TestState" })
    })

    it("should render with state machine values", () => {
      render(
        <StateMachineProvider>
          <MyFormComponent />
        </StateMachineProvider>
      )

      // tests here don't work and are using values from previous describe
    })
})
从“React”导入React
从'@testing library/react'导入{render}
从“小状态机”导入{StateMachineProvider,createStore}
从“./MyForm”导入MyFormComponent
const goodState={//some value}
const badState={//some value}
描述(“具有良好数据的MyComponent”,()=>{
在每个之前(()=>{
createStore(goodState,{name:“TestState”})
})
它(“应使用状态机值进行渲染”,()=>{
渲染(
)
//这里的测试有效
})
})
描述(“数据不正确的MyComponent”,()=>{
在每个之前(()=>{
createStore(badData,{name:“TestState”})
})
它(“应使用状态机值进行渲染”,()=>{
渲染(
)
//这里的测试不起作用,使用的是前面描述的值
})
})
如何确保每次测试时都重置
createStore
中的值?

beforeach中的sessionStorage.clear()?