Test authors can't turn on custom testers for certain assertions and turn them off for others (a custom matcher should be used instead if that behavior is desired). Other times, however, a test author may want to allow for some flexibility in their test, and toBeWithinRange may be a more appropriate assertion.Ĭustom equality testers are good for globally extending Jest matchers to apply custom equality logic for all equality comparisons. Sometimes a test author may want to assert two numbers are exactly equal and should use toBe. For example, the toBeWithinRange example in the expect.extend section is a good example of a custom matcher. A tester is a method used by matchers that do equality checks to determine if objects are the same.Ĭustom matchers are good to use when you want to provide a custom assertion that test authors can use in their tests. Matchers are methods available on expect, for example expect().toEqual(). See the example in the Recursive custom equality testers section for more details. If you use this function, pass through the custom testers your tester is given so further equality checks equals applies can also use custom testers the test author may have configured. It optionally takes a list of custom equality testers to apply to the deep equality checks. This is a deep-equality function that will return true if two objects have the same values (recursively). These helper functions and properties can be found on this inside a custom tester: this.equals(a, b, customTesters?) a class instance with fields a and b will not equal a literal object with fields a and b.Ĭustom testers are functions that return either the result ( true or false) of comparing the equality of the two given arguments or undefined if the tester does not handle the given objects and wants to delegate equality to other testers (for example, the builtin equality testers).Ĭustom testers are called with 3 arguments: the two objects to compare and the array of custom testers (used for recursive testers, see the section below). undefined items are taken into account, e.g.Object containing house features to be testedĪmenities : , You might want to check that drink gets called for 'lemon', but not for 'octopus', because 'octopus' flavour is really weird and why would anything be octopus-flavoured? You can do that with this test suite: toEqual uses.įor example, let's say you have a drinkAll(drink, flavour) function that takes a drink function and applies it to all available beverages. The arguments are checked with the same algorithm that. toHaveBeenCalledWith to ensure that a mock function was called with specific arguments. rewrite expect(received).not.toBe(expected) as expect(Object.is(received, expected)).toBe(false).rewrite expect(received).toBe(expected) as expect(Object.is(received, expected)).toBe(true).For example, to assert whether or not elements are the same instance: If differences between properties do not help you to understand why a test fails, especially if the report is large, then you might move the comparison into the expect function. toBe matcher checks referential identity, it reports a deep comparison of values if the assertion fails. For example, due to rounding, in JavaScript 0.2 + 0.1 is not strictly equal to 0.3. Here's how you would test that:ĭon't use. Let's say you have a method bestLaCroixFlavor() which is supposed to return the string 'grapefruit'. It's easier to understand this with an example. Instead, you will use expect along with a "matcher" function to assert something about a value. The expect function is used every time you want to test a value. expect.addSnapshotSerializer(serializer).toThrowErrorMatchingInlineSnapshot(inlineSnapshot) toMatchInlineSnapshot(propertyMatchers?, inlineSnapshot) toMatchSnapshot(propertyMatchers?, hint?) toHaveBeenNthCalledWith(nthCall, arg1, arg2. If the RHS is a single value, then it is treated as a singleton array. The array (sequence) inclusion operator returns Boolean true if the value of the LHS is included in the array of values on the RHS. The 'greater than or equals' operator returns Boolean true if the LHS is numerically greater than or equal to the RHS. The 'greater than' operator returns Boolean true if the LHS is numerically greater than the RHS. The inequality operator returns Boolean false if both operands are the same (type and value, deep equality). Objects must have the same key/value pairs (order is not relevant). Arrays must have the same values in the same order. Arrays and objects are checked for deep equality. The equality operator returns Boolean true if both operands are the same (type and value).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |