Nightwatch using expect in url callback, result.value is undefined - nightwatch.js

I'm messing around with Nightwatch and I have a test setup like the following:
module.exports = {
'Load index page with default category' : function(browser)
{
browser
.url(browser.launch_url)
.waitForElementPresent('body', 1000)
.url((result)=>
{
console.log(result.value) // http://example.com/something/
browser.expect.result.value.to.contain("something");
// TypeError: Cannot read property 'to' of undefined
})
.end();
}
}
I'm confused how I'm supposed to use result.value with expect.
result.value logs as expected, but I can't verify it.
I can use browser.assert.urlContains("something"), but I was trying to use expect instead.

You can use chai library directly but PASS will not be in report while FAIL will be shown.
https://github.com/nightwatchjs/nightwatch/issues/601

Related

cypress should be called with value test

Looking for a test like below. Basically I get it to work using cy.get('#myLog').should('be.called') but I'm trying to test the value console log is using when called. In jest it would be toHaveBeenCalledWith() so the equivalent in cypress is what I'm after ?
cy.window().then((win) => {
cy.wrap(cy.spy(win.console, 'log')).as('myLog')
})
cy.get('#myLog')
.should('be.called')
.and('have.value', 'clicked')
cy.spy() and cy.stub() you can use Sinon-chai assertions. So for you instance you may want to use be.calledWith.
cy.window().then((win) => {
cy.wrap(cy.spy(win.console, 'log')).as('myLog')
})
cy.get('#myLog')
.should('be.calledWith', 'clicked')

Combined resolvers always throw "cannot read property 'apply' of undefined" error

I am trying to attach the following guard:
export const isSupplier=(_,__,{me})=>{
me.role==="supplier" ? skip : new ForbiddenError('Not authenticated as supplier.');
}
To a resolver, like this:
addProductNow:combineResolvers(
isSupplier,
async (_,{name,price,description,url,stock},{dataSources,me})=>{
const supplierId=me.id
console.log(supplierId)
const supplier=await dataSources.SupplierAPI.findSupplierById(supplierId)
const newProduct=await dataSources.ProductAPI.addProduct(name,price,description,url,stock,supplierId,{ttlInSeconds:60*20})
return newProduct
}),
Yet it always returns the error "cannot read property 'apply' of undefined". I have tried to log something in the guard , yet it seems like it never gets executed. After removing the guard from the resolver everything works fine and logging 'me' shows the expected value. Am I doing something wrong ? Thanks !
I'm not sure what you're using for your "guards", but I assume it's because you're not doing or returning anything in this function, and it's expecting a function (what it calls "apply" on):
export const isSupplier=(_,__,{me})=>{
me.role==="supplier" ? skip : new ForbiddenError('Not authenticated as supplier.');
}
Did you mean to do this:
export const isSupplier=(_,__,{me})=>{
return me.role==="supplier" ? skip : new ForbiddenError('Not authenticated as supplier.');
}
Follow-up Edit:
From googling some of your variable names, I assume you're using graphql-resolvers.
In the example they provide, they use "arrow functions with implicit return", which is when you
Don't add Braces around the body of your function
Put the whole thing in one expression
The result of the function becomes whatever the expression after the arrow results in:
const isAuthenticated = (root, args, { user }) => user ? skip : new Error('Not authenticated')
This can be converted to using "explicit returns" by adding braces and the return keyword like this:
const isAuthenticated = (root, args, { user }) => {
return user ? skip : new Error('Not authenticated')
}
Your code as documented, however, added the braces but NOT the return and therefore always returns undefined. Looking at the source code of that library, if you don't return EXACTLY skip it resolves to whatever the return value of your function was (in this case undefined).
Can you share more information about your stack trace to show WHAT is trying to call .apply on what?

How to test a function in Angular Jasmine?

I have a function in my service as:
logError(message: string, stack: string) {
console.log('LoggingService: ' + message);
}
and I want to test this function test case I am writing are as:
it('should be created', inject([LoggerService], (loggerService: LoggerService) => {
expect(loggerService).toBeTruthy();
}));
it('log error should be called', inject([LoggerService], (loggerService: LoggerService) => {
spyOn(loggerService, 'logError');
expect(loggerService.logError).toHaveBeenCalled();
expect(loggerService.logError).toHaveBeenCalledTimes(1);
}));
Problem is when I run tests and I see code coverage it shows that logError function is not covered and also the console.log statement is not covered.
But when I change my approach from spyOn to call the actual method it says there are no expectations and I am not sure what will be the right expectaion in this case?
Any help on recommended approach is appreciated.
spyOn installs a spy onto a method of an existing object but it doesn't invoke that method. Therefore, after installing the spy on a method, you need to call the method under test, which is supposed to invoke the spied/mocked method.
it('log error should be called', inject([LoggerService], (loggerService: LoggerService) => {
spyOn(console, 'log');
const error = 'abc';
loggerService.logError(...);
expect(console.log).toHaveBeenCalledTimes(1);
expect(console.log)toHaveBeenCalledWith(error);
}));

How can I add shared variable to chakram?

I'am using chakram + mocha.
How can I use shared variables for all test?
For example, I would like to use variable API_PATH="http://example.com/v1/" in tests. And this variable could be changed during running test. So, my test looks like this.
var response = chakram.get(API_PATH + "products");
expect(response).to.have.status(200);
As example, protractor has conf.js with parameter baseUrl. Running test looks like protractor conf.js --baseUrl http://example.com/
what have you tried so far? Have you tried using beforeEach to reinitialize the object that you are using? You could just make the the shared variables declared outside of your tests.
EDIT: Adding details from what Jerry said:
If you want all variable to be reused within the same test, you must make them global variables. See example below
///include dependencies
var assert = require('assert'),
chai = require('chai'),
expect = chai.expect,
chakram = require('chakram');
//INIT GLOBAL VARAIBLES FOR within the same test
var testObj,
dummyData = {
user: 'John Kim',
lastSeenOnline: 'Wed August 11 12:05 2017'
};
describe ('#User', function () {
beforeEach(function () {
//init what the object contains
testObj = new DataStore(data, ContainerStore);
});
it ('#Should return the name of the user', function () {
assert.equal(testObj.get('user'), dummyData.user);
});
it("should offer simple HTTP request capabilities", function () {
return chakram.get("http://httpbin.org/get");
});
});
Note: I work with react but this is an example. We assume that the ContainerStore contains a method that has a method for get() which just gets the value of our JSON object. You can use testObj many time in different describe blocks since it is declared outside of your tests. But you should remember to always reinitialize your tesObj in a beforeEach(); otherwise, you risk populating your individual tests. There are cases were you do not have to initialize the beforeEach() and it is optional.
For Example in config.js
module.exports = {
"baseUrl": "http://example.com/",
"googleUrl": "http://www.google.com.tr/"
};
And use in javascript code:
let config = require('/config');
describle("test describle", () => {
it("test", () => {
chakram.get(config.baseUrl + "products"); //for example use
})
})

Sinon useFakeTimers() creates a timeout in before/afterEach

I'm using Sinon with Mocha to test some expiration date values. I used the same code a few months ago and it worked fine, but somewhere between v1.12.x and v1.17.x, something has changed and I can't seem to find the right path.
let sinon = require('sinon');
describe('USER & AUTHENTICATION ENDPOINTS', function(done) {
beforeEach(function() {
this.clock = sinon.useFakeTimers(new Date().getTime());
return fixtures.load(data);
});
afterEach(function() {
this.clock.restore();
return fixtures.clear(data);
});
context('POST /users', function() { ... }
});
I've tried with and without the new Date().getTime() argument.
I've tried passing in and explicitly calling done().
I've tried removing my fixture load/clear processes.
The end result is always the same:
Error: timeout of 5000ms exceeded. Ensure the done() callback is being called in this test.
Has something changed that I just haven't noticed in the documentation? Do I have some kind of error in there that I can't see?
Any thoughts would be appreciated.
UPDATE
So a little more info here. This clearly has something to do with my code, but I'm at a loss.
If I comment every actual test, the tests run and give me a green "0 passing".
If I run an actual test, even one that just this:
context('POST /users', function() {
it('should create a new user', function(done) {
done();
})
});
I'm right back to the timeout. What am I missing?
Mystery solved. It appears to be a conflict between Sinon and versions of Knex > 0.7.6.
Seems to be because pool2 relies on behavior of setTimeout. Using sinon.useFakeTimers(...) replaces several methods including setTimeout with synchronous versions which breaks it. Can fix by replacing with: clock = sinon.useFakeTimers(Number(date), 'Date');
My original code was written in a world where Knex v0.7.6 was the latest version. Now that it's not everything failed even though the code itself was the same. I used the fix mentioned and things look to be fine.
You are passing done to your describe callback in line 2:
describe('USER & AUTHENTICATION ENDPOINTS', function(done) {
Mocha expects you to invoke it... To get rid of the timeout error, just remove the done parameter from the callback.

Resources