toThrow不接受匹配器。根据文档,可选错误参数有四个选项:您可以提供一个可选参数来测试是否引发了特定错误:
7.1. toThrow(error?) Use .toThrow to test that a function throws when it is called. 代码语言:javascript 代码运行次数:0 运行 AI代码解释 function myfn(){ console.log(X); } test('throws on octopus', () => { expect(() => { myfn(); }).toThrow(); }); ...
Use Jest’s built-in toThrow() matcher to test error scenarios. For example: test('throws an error', () => { expect(() => someFunction()).toThrow('Error message'); }); With these approaches, you can easily debug and fix issues in your Jest test cases.Tags...
Use Jest’s built-in toThrow() matcher to test error scenarios. For example: test('throws an error', () => { expect(() => someFunction()).toThrow('Error message'); }); With these approaches, you can easily debug and fix issues in your Jest test cases.Tags...
In the case where you want to test that a particular function throws an error when it's called, you need to use toThrow.function compileAndroidCode() { throw new ConfigError('the JDK you are using is wrong'); } test('compiling android will go as expected', () => { expect(compile...
if(flavor === 'octopus'){ throw new DisgustingFlavorError('yuck,octopus flavor'); } } it('throws on octopus', () => { function testFun(){ drinkFlavor('octopus') //必须将其写着方法里,否则无法捕获错误,断言失败 } expect(testFun).toThrowError('yuck,octopus flavor') ...
Note: when you supply a test callback function then the test.todo throws an error. In the case where you have already implemented the test and the test is broken and you don?t want it to run, then you can use test.skip instead. ...
test('fetchError throws an error', async () => { expect.assertions(1); // Make sure at least one assertion runs try { await fetchError(); // Call the async function } catch (error) { expect(error).toMatch('Something went wrong'); // Check if the error is as expected } });...
error Functions Instead of passing body, it is also possible to pass a function that returns a promise. The promise should resolve with a string or an object containing body and init props i.e: fetch.mockResponse(() => callMyApi().then((res) => ({ body: 'ok' }))); // OR ...
Use .toThrowWithMessage when checking if a callback function throws an error with a given error type and given error message. Message can either be a String or a RegExp.test('throws an error of type TypeError with message "hello world"', () => { expect(() => { throw TypeError("...