I was studying about TDD and unit testing using Jasmine JS and I have a question about their methods.
I found two methods and would like to know what is the difference.
describe('Teste do toContain', () => {
var name = 'Lucas de Brito Silva'
it('Deve demonstrar o uso do toContain', () => {
expect(name).toContain('Lucas');
});
});
describe('Teste do toMatch', function () {
var text = 'Lucas de Brito Silva'
it('deve validar o uso do toMatch', () => {
expect(text).toMatch('Brito');
});
})
expect(something). toContain(other) will be roughly like checking something. includes(other) === true .
It checks whether something is matched for a regular expression. You can use the toMatch matcher to test search patterns.
The toBeGreaterThan and toBeLessThan matchers check if something is greater than or less than something else.
ToEqual() ToEqual() is the simplest matcher present in the inbuilt library of Jasmine. It just matches whether the result of the operation given as an argument to this method matches with the result of it or not.
The difference is partly what they operate on but also what they will do.
Here is the example usage from version 2 of Jasmine (but this runs the examples using the latest version):
it("The 'toMatch' matcher is for regular expressions", function() {
var message = "foo bar baz";
expect(message).toMatch(/bar/);
expect(message).toMatch("bar");
expect(message).not.toMatch(/quux/);
});
describe("The 'toContain' matcher", function() {
it("works for finding an item in an Array", function() {
var a = ["foo", "bar", "baz"];
expect(a).toContain("bar");
expect(a).not.toContain("quux");
});
it("also works for finding a substring", function() {
var a = "foo bar baz";
expect(a).toContain("bar");
expect(a).not.toContain("quux");
});
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/jasmine/3.4.0/jasmine.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/3.4.0/jasmine.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/3.4.0/jasmine-html.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/3.4.0/boot.min.js"></script>
It does demonstrate what they can do.
toContain
will work on both arrays and strings. It will essentially be the same using Array#includes
or String#includes
- the array or string will be checked if it has an item (for arrays) or a subsequence (for strings) that matches the argument. expect(something).toContain(other)
will be roughly like checking something.includes(other) === true
.toMatch
instead uses a regular expression. So, first of all, it only works on strings, not arrays. Second, if given a string as an argument generates a regex from it. So, expect(something).toMatch(other)
will actually be resolved as if new RegExp(other).test(something)
. This does mean that if you want to use it for simple string matching you should be careful to not use special characters:it("The 'toMatch' matcher generates a regex from the input", function() {
var message = "foo\\dbar";
expect(message).toMatch(message);
});
it("The generated matcher will obey regex restrictions", function() {
var pattern = "foo\\dbar";
expect(pattern).not.toMatch(pattern);
expect("foo4bar").toMatch(pattern);
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/jasmine/3.4.0/jasmine.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/3.4.0/jasmine.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/3.4.0/jasmine-html.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/3.4.0/boot.min.js"></script>
Here, the value of the message
string is foo\dbar
but if you generate a regex from that, then it wouldn't match the same string, since \d
denotes a digit - foo4bar
will match but not foo\dbar
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With