Testing arguments with toBeCalledWith() in Jest - javascript

I have function that uses puppetteer's page object to evaluate and return some data.
I would like to write a unit test with jest to check if page.evaluate() takes specified parameters
This is the function
async function cinemasfromState(page, state) {
const CINEMA_SELECTOR = `div[data-state=$[STATE]] div.top-select-option a.eccheckbox`;
let res = await page.evaluate(
(elementPath, state) => {
let results = Array.from(document.querySelectorAll(elementPath)).map(
function(cin, index) {
return {
//Stuff
};
return result;
},
{ state }
);
},
CINEMA_SELECTOR.replace("$[STATE]", state),
state
);
return res;
}
Below is what my test looks like
describe("cinemasfromState", () => {
let page_mock = {
click: jest.fn(() => Promise.resolve()),
evaluate: jest.fn((selector, state) => Promise.resolve())
};
test("page.evaluate called correctly ", async () => {
await cinemasfromState(page_mock, "KAN");
expect(page_mock.evaluate).toBeCalledTimes(1);
expect(
page_mock.evaluate)toBeCalledWith(
"div[data-state=KAN] div.top-select-option a.eccheckbox",
"KAN"
);
});
});
And I get the below error as my test output
● cinemasfromState › page.evaluate called correctly
expect(jest.fn()).toBeCalledWith(expected)
Expected mock function to have been called with:
"div[data-state=KAN] div.top-select-option a.eccheckbox"
as argument 1, but it was called with
[Function anonymous].
Difference:
Comparing two different types of values. Expected string but received function.
"KAN"
as argument 2, but it was called with
"div[data-state=KAN] div.top-select-option a.eccheckbox".
undefined
as argument 3, but it was called with
"KAN".
Difference:
Comparing two different types of values. Expected undefined but received string.
52 | expect(page_mock1.evaluate).toBeCalledTimes(1);
53 | expect(page_mock1.evaluate).toBeCalledWith(
> 54 | "div[data-state=KAN] div.top-select-option a.eccheckbox",
| ^
55 | "KAN"
56 | );
57 | });
Any help on writing test to verify the arguments?

If you read your error log, you'll notice it's trying to match three arguments, but you are only asserting against two. .toBeCalledWith in jest will perform an exact match of the arguments passed to the function along with their order.
For instance, if you call func(arg1, arg2), then expect(func).toBeCalledWith(arg2) will fail because you did not also assert on arg1. This is what is happening in your case because the first argument to page.evaluate() is actually an anonymous function.
So, your test will need to be something like this:
expect(page_mock.evaluate).toBeCalledWith(
expect.any(Function),
"div[data-state=KAN] div.top-select-option a.eccheckbox",
"KAN"
);

Related

Array is not array anymore when passed into Vuex action function

EDIT: Added extra code in the filterEvents snippet for more context.
I'm not quite understanding what's going on with my code. I'm trying to pass an array into an action function inside of my Vuex store. If I return a Promise inside of that action function, then the parameter being passed isn't of type Array and is instead an Object, which results in the reject() error that I have for the Promise.
Here's some code for context:
filterEvents({ commit }, events) {
console.log(Array.isArray(events)); //this ends up false
console.log(events);
return new Promise((resolve, reject) => {
if (!Array.isArray(events)) {
reject("Invalid argument: is not of type Array.");
}
let filtered = events.filter((event) => {
let now = new Date();
let event_stop = new Date(event.stop_time);
if (event_stop >= now || event_stop == null) {
return event;
}
});
resolve(filtered);
});
}
Here's where I call filterEvents; inside of getEvents;
getEvents({ state, commit, dispatch }, searchParams) {
.....
eventful.getEvents(searchParams).then(async (res) => {
.....
console.log(Array.isArray(res.data.events.event)); //this ends up true
console.log(res.data.events.event);
/* where I call it */
await dispatch("filterEvents", res.data.events.event).then((res) => {
.....
});
}).catch((err) => {
.....
});
}
Here's the output from the Chrome developer console. First two outputs are from getEvents and last two are from filterEvents
Would really like an explanation as to why this is the case. I'm going to bet it's something small, but it's 3 a.m. at the moment and my brain can't wrap around why it's not of type Array when passed into filterEvents.
I always try to check the length prop of the array which helps me out in such cases.
...
return new Promise((resolve, reject) => {
if (!Array.isArray(events) && !events.length) {
reject("Invalid argument: is not of type Array.");
}
.....
});
...
I finally understood what my issue was after taking another look at the object that was being logged on the console. I did not know that Vuex actions HAD to have two arguments if you want to pass in a payload into that function. For example, I initially did this
filterEvents(events) {
.....
}
but what I really needed to do was
filterEvents(context, events) {
.....
}
The context argument is the object that allows you to do things such as commit and dispatch. I usually destructure the context object (i.e. { commit, dispatch} ), so I for some reason never thought twice about it. You don't have to destructure the context object to use commit and dispatch; if you don't it would just be like
context.commit('function', payload);

Varible passed to page.evaluate is undefined

When I use page.evaluate and pass varibles to it, they are undefined.
let reaction;
// First console.log actually logs the string as expected
console.log(reactionTitle);
await this.page.evaluate(function (reactionTitle, reaction) {
// Second console.log logs undefined in the website's console
console.log(reactionTitle);
document.querySelectorAll('span.embedAuthorName-3mnTWj').forEach(s => {
// Third console.log logs all innerTexts in the website's console
console.log(s.innerText)
if (s.innerText == reactionTitle) {
console.log(s);
reaction = s.offsetParent.offsetParent.querySelector('.container-1ov-mD').querySelector('.reactions-12N0jA').querySelector('div').querySelector('.reaction-1hd86g').querySelector('.reactionInner-15NvIl');
console.log(reaction)
}
});
}, (reactionTitle, reaction));
console.log(reaction)
I put some comments in the code to show where reactionTitle is undefined.
Remove the parentheses from (reactionTitle, reaction) in arguments:
await this.page.evaluate((reactionTitle, reaction) => { ... }, reactionTitle, reaction)
or
await this.page.evaluate(({reactionTitle, reaction}) => { ... }, {reactionTitle, reaction})
page.evaluate executes in page context and is separate from the puppeteer execution environment. I'd suggest taking look at the docs and the architecture. To access the variables from in-page context in puppeteer, you need to return the variables. Note that the variables must be serializable:
const result = await this.page.evaluate(({reactionTitle, reaction}) => {
...
return reaction;
}, {reactionTitle, reaction});
In case the returned value is an in-page object, use evaluateHandle.

Jest: How to test function that returns a function

I have several functions I would like to test with Jest. All functions are functions that return functions.
A simple example:
export function csl(foo) {
return function(bar) {
return(bar)
};
}
now I want to test if the input = the return is. I try it with:
expect(() => csl("foo")).toBe("foo") // = received: [Function anonymous]
expect(csl("foo")).toBe("foo") // = received: undefined
How I can test these functions?
You need to call the returned function
expect(csl("foo")("bar")).toBe("bar")
^^^^^^^

How to test if a wrapper in Jest calls skip and it correctly?

This is a follow up question to this question.
I have a function called assertTruthy for Jest. assertTruthy(msg, fn, args), expects a message, a function and arguments and should pass if the thing that is returned when the function is invoked with the arguments is truthy and fail if its not.
I want to extend it to also support Jest's only and skip.
Here is what I wrote:
assertTruthy.skip = ({
message = '',
fn = undefined,
args,
} = {}) => {
it.skip(message, () => {
expect(fn(args)).toBeTruthy();
});
};
assertTruthy.only = ({
message = '',
fn = undefined,
args,
} = {}) => {
it.only(message, () => {
expect(fn(args)).toBeTruthy();
});
};
How would I test these functions?
Here is what I tried, which works, but I'm not sure if this is correct.
describe('skip()', () => {
test('it skips the function', () => {
it.skip = jest.fn();
assertTruthy.skip({
message: 'something',
fn: () => true,
args: undefined,
});
expect(it.skip).toHaveBeenCalledTimes(1);
});
});
This looks like a fair enough test that your assertTruthy skip and only call Jest's it skip and only methods.
You might want to assert that it also calls them with the arguments you expect using toHaveBeenCalledWith.
Can you please elaborate more, what you want to achieve and I did't get it properly why you want to extend jest skip and only methods to achieve the same thing that you are already testing.
But if you only want to test if a function not been invoked/executed with toHaveBeenCalledTimes() based on the arguments to be truthy/falsy then you are doing it right.

Mocha / Chai expect.to.throw not catching thrown errors

I'm having issues getting Chai's expect.to.throw to work in a test for my node.js app. The test keeps failing on the thrown error, but If I wrap the test case in try and catch and assert on the caught error, it works.
Does expect.to.throw not work like I think it should or something?
it('should throw an error if you try to get an undefined property', function (done) {
var params = { a: 'test', b: 'test', c: 'test' };
var model = new TestModel(MOCK_REQUEST, params);
// neither of these work
expect(model.get('z')).to.throw('Property does not exist in model schema.');
expect(model.get('z')).to.throw(new Error('Property does not exist in model schema.'));
// this works
try {
model.get('z');
}
catch(err) {
expect(err).to.eql(new Error('Property does not exist in model schema.'));
}
done();
});
The failure:
19 passing (25ms)
1 failing
1) Model Base should throw an error if you try to get an undefined property:
Error: Property does not exist in model schema.
You have to pass a function to expect. Like this:
expect(model.get.bind(model, 'z')).to.throw('Property does not exist in model schema.');
expect(model.get.bind(model, 'z')).to.throw(new Error('Property does not exist in model schema.'));
The way you are doing it, you are passing to expect the result of calling model.get('z'). But to test whether something is thrown, you have to pass a function to expect, which expect will call itself. The bind method used above creates a new function which when called will call model.get with this set to the value of model and the first argument set to 'z'.
A good explanation of bind can be found here.
As this answer says, you can also just wrap your code in an anonymous function like this:
expect(function(){
model.get('z');
}).to.throw('Property does not exist in model schema.');
And if you are already using ES6/ES2015 then you can also use an arrow function. It is basically the same as using a normal anonymous function but shorter.
expect(() => model.get('z')).to.throw('Property does not exist in model schema.');
This question has many, many duplicates, including questions not mentioning the Chai assertion library. Here are the basics collected together:
The assertion must call the function, instead of it evaluating immediately.
assert.throws(x.y.z);
// FAIL. x.y.z throws an exception, which immediately exits the
// enclosing block, so assert.throw() not called.
assert.throws(()=>x.y.z);
// assert.throw() is called with a function, which only throws
// when assert.throw executes the function.
assert.throws(function () { x.y.z });
// if you cannot use ES6 at work
function badReference() { x.y.z }; assert.throws(badReference);
// for the verbose
assert.throws(()=>model.get(z));
// the specific example given.
homegrownAssertThrows(model.get, z);
// a style common in Python, but not in JavaScript
You can check for specific errors using any assertion library:
Node
assert.throws(() => x.y.z);
assert.throws(() => x.y.z, ReferenceError);
assert.throws(() => x.y.z, ReferenceError, /is not defined/);
assert.throws(() => x.y.z, /is not defined/);
assert.doesNotThrow(() => 42);
assert.throws(() => x.y.z, Error);
assert.throws(() => model.get.z, /Property does not exist in model schema./)
Should
should.throws(() => x.y.z);
should.throws(() => x.y.z, ReferenceError);
should.throws(() => x.y.z, ReferenceError, /is not defined/);
should.throws(() => x.y.z, /is not defined/);
should.doesNotThrow(() => 42);
should.throws(() => x.y.z, Error);
should.throws(() => model.get.z, /Property does not exist in model schema./)
Chai Expect
expect(() => x.y.z).to.throw();
expect(() => x.y.z).to.throw(ReferenceError);
expect(() => x.y.z).to.throw(ReferenceError, /is not defined/);
expect(() => x.y.z).to.throw(/is not defined/);
expect(() => 42).not.to.throw();
expect(() => x.y.z).to.throw(Error);
expect(() => model.get.z).to.throw(/Property does not exist in model schema./);
You must handle exceptions that 'escape' the test
it('should handle escaped errors', function () {
try {
expect(() => x.y.z).not.to.throw(RangeError);
} catch (err) {
expect(err).to.be.a(ReferenceError);
}
});
This can look confusing at first. Like riding a bike, it just 'clicks' forever once it clicks.
examples from doc... ;)
because you rely on this context:
which is lost when the function is invoked by .throw
there’s no way for it to know what this is supposed to be
you have to use one of these options:
wrap the method or function call inside of another function
bind the context
// wrap the method or function call inside of another function
expect(function () { cat.meow(); }).to.throw(); // Function expression
expect(() => cat.meow()).to.throw(); // ES6 arrow function
// bind the context
expect(cat.meow.bind(cat)).to.throw(); // Bind
One other possible implementation, more cumbersome than the .bind() solution, but one that helps to make the point that expect() requires a function that provides a this context to the covered function, you can use a call(), e.g.,
expect(function() {model.get.call(model, 'z');}).to.throw('...');
I have found a nice way around it:
// The test, BDD style
it ("unsupported site", () => {
The.function(myFunc)
.with.arguments({url:"https://www.ebay.com/"})
.should.throw(/unsupported/);
});
// The function that does the magic: (lang:TypeScript)
export const The = {
'function': (func:Function) => ({
'with': ({
'arguments': function (...args:any) {
return () => func(...args);
}
})
})
};
It's much more readable then my old version:
it ("unsupported site", () => {
const args = {url:"https://www.ebay.com/"}; //Arrange
function check_unsupported_site() { myFunc(args) } //Act
check_unsupported_site.should.throw(/unsupported/) //Assert
});

Categories