Javascript ValidateUsername是只读的| TS,带有Jest/Enzyme
我有一个小的实用功能,我正在我的React TS项目中使用Jest和Ezyme进行一些测试。在该项目的JS文件中,我得到以下错误:Javascript ValidateUsername是只读的| TS,带有Jest/Enzyme,javascript,reactjs,typescript,jestjs,enzyme,Javascript,Reactjs,Typescript,Jestjs,Enzyme,我有一个小的实用功能,我正在我的React TS项目中使用Jest和Ezyme进行一些测试。在该项目的JS文件中,我得到以下错误: "validateUsername" is read-only. 这就是实用程序本身: export const validateUsername = value => listUsers() .then(({ data }) => { if (Array.isArray(data) && data.find(u
"validateUsername" is read-only.
这就是实用程序本身:
export const validateUsername = value =>
listUsers()
.then(({ data }) => {
if (Array.isArray(data) && data.find(userData => userData.username === value)) {
throw 'Username already exists';
}
})
.catch(error => {
throw serverErrorResponseUtil(error);
});
下面是对它的测试:
describe('Validate Username', () => {
const validateUsernameFn = jest.fn();
beforeEach(() => {
validateUsername = validateUsernameFn;
});
it('Should throw an error if the given value exists', async () => {
try {
await validateUsername('username');
} catch (e) {
expect(e).toEqual('Username already exists');
}
});
it('Accept the data if the passed userName is unique', async () => {
expect(() => validateUsername('Username is unique')).not.toThrow();
});
});
我在这里得到错误:
validateUsername=validateUsernameFn代码>。问题是这个文件是js文件。为什么我得到一个关于只读的ts错误。你们能帮我一下吗?您可能已经找到了解决方案,但问题是您需要导入validateUsername
,然后在beforeach()中重新定义它。
我不知道您为什么需要beforeach()中的代码,但是如果您想访问initialvalidateUsername
+享受模拟的好处,我建议使用jest.spyOn
在beforeach()
中,如果需要,可以清除测试之间的模拟