expect(shoppingList).toContain('milk') expect(new Set(shoppingList)).toContain('milk') }) // 异常 test('Exceptions', () => { function compileAndroidCode() { throw new Error('you are using the wrong JDK') // throw 'you are using the wrong JDK' } // toThrow 可以测试调用某个特定...
expect('How are you?').toEqual(expect.not.stringContaining(expected)); }); }); 1. 2. 3. 4. 5. 6. 7. expect.not.stringMatching(string | regexp) 匹配不匹配预期regexp的接收字符串,它与expect.stringMatching.相反。 describe('not.stringMatching', () => { const expected = /Hello world!
expect.any(constructor) 匹配给定构造函数所创建的任何内容。你可以在内部使用toEqual或toBeCalledWith而不是文字值。如果你想检查一个模拟函数是否被调用时带有一个数字: 1 2 3 4 5 6 7 8 9 function randocall(fn) { return fn(Math.floor(Math.random() * 6 + 1)); } test('randocall calls its ...
expect.any(constructor) 匹配给定构造函数所创建的任何内容。你可以在内部使用toEqual或toBeCalledWith而不是文字值。例如,如果你想检查一个模拟函数是否被调用时带有一个数字。 代码语言:javascript 复制 function randocall(fn) { return fn(Math.floor(Math.random() * 6 + 1)); } test('randocall calls its...
你是对的,jestjs不能比较序列化的函数。您可以使用expect.any(constructor)。
function expect(result) { return { toBe: function (actual) { if (result !== actual) { throw new Error(`预期值和实际值不相等,预期${actual}结果却是${result}`); } } } } function test(desc, fn) { try { fn(); console.log(`${desc}通过测试`); } catch (e) { console.log(`${...
看起来expect.anything在设计上不匹配未设置的值。如果你需要一个“真正的任何东西”匹配器,这里有一个...
expect(bestLaCroixFlavor()).toBe('grapefruit'); }); The matcher function here is toBe. Different matcher functions exist, and to help you test different things, we have documented them below: The argument to expect has to be the value that your code produces, and any argument to the matc...
测试中包含了多种断言,以便展示在 Jest 中如何使用expect函数。你可以宽松地只期望对象包含某个id,而忽略其他属性。另一方面,你也可以严格要求整个对象完全等于传入的值。 在测试的最后,你期望模拟服务的getQuotes方法被调用一次,并且参数为1。 如果你运行npm t或npm test,测试将通过,并显示以下输出: ...
expect.any-expect.not.stringContaining 这几个通常搭配toBeCalledWith一起使用 //objectContaining常可以用来校验函数的入参test('onPress gets called with the right thing',()=>{constonPress=jest.fn();simulatePresses(onPress);expect(onPress).toBeCalledWith(expect.objectContaining({x:expect.any(Number),...