I am using jasmine-node framework for my API automation. I am able to run REST services and able to get the result using node-fetch or http. Without reporter when I run with the below command I was able to get the results in console
jasmine spec/xxx.spec.js
After that I added a reporter(pretty html reporter) for reporting. Now when I run those commands, I get the below error.
No specs found
Below is my code for reporter.js
var Jasmine = require('jasmine');
var HtmlReporter = require('jasmine-pretty-html-reporter').Reporter;
var path=require('path');
var jasmine = new Jasmine();
jasmine.loadConfigFile('./spec/support/jasmine.json');
// options object
jasmine.addReporter(new HtmlReporter({
path: path.join('./spec/helpers','results')
}));
jasmine.execute();
Below is my code for jasmine.json
{
"spec_dir": "spec",
"spec_files": [
"**/*[sS]pec.js"
],
"helpers": [
"helpers/**/*.js"
],
"stopSpecOnExpectationFailure": false,
"random": true
}
Please help me with this. Also please let me know if there is any better reporter than this.Thanks in advance
Related
I'm using Sentry for error reporting on the React app that I created.
The problem with it is that I don't have an idea how to debug certain issues because I don't know what's the exact file the error occurred in:
I'm using Laravel mix for compiling. The webpack.mix.js looks like this:
mix
.react("resources/js/checkout/CheckoutRoot.js", "public/js")
.version();
I tried using sourceMaps() like so:
const productionSourceMaps = true;
mix
.react("resources/js/checkout/CheckoutRoot.js", "public/js")
.react("resources/js/checkout/DonationRoot.js", "public/js")
.version()
.sourceMaps(productionSourceMaps, "source-map")
But it doesn't seem to work. It appended this right below the file when viewing in Chrome dev tools:
//# sourceMappingURL=27.js.map?id=c4f9bf41f206bfad8600
But when I pretty print it still results in the same gibberish:
I'm expecting to see it point out to the component file I'm working on locally. Is that possible?
Update
I tried installing Sentry's webpack plugin:
const SentryWebpackPlugin = require("#sentry/webpack-plugin");
let config = {
output: {
publicPath: "/",
chunkFilename: "js/chunks/[name].js?id=[chunkhash]",
},
plugins: [
new SentryWebpackPlugin({
// sentry-cli configuration
authToken: "MY_AUTH_TOKEN",
org: "MY_ORG",
project: "MY_PROJECT",
release: "MY_RELEASE",
include: ".",
ignore: ["node_modules", "webpack.config.js"],
}),
],
};
Used the same release when initializing Sentry on my source file:
Sentry.init({
dsn: "MY_DSN",
release: "testing",
});
Put some failing code:
useEffect(() => {
console.bog("MY_RELEASE");
}, []);
Then compiled like usual:
npm run production
I triggered the error on the browser and I got the expected file in there (MobilePayment.js):
But from Sentry, all I get is this:
I would expect to find MobilePayment.js in there but there's none.
When compiling, I got this:
So I assume it uploaded the sources to Sentry.
I even tried the same thing using Sentry-cli:
sentry-cli releases files release upload-sourcemaps --ext js --ext map /path/to/public/js
And it pretty much did the same thing:
I then triggered the same error. But I still got the same output from Sentry dashboard. Please help.
I've run into this before.
IIRC the trick was finding the correct devtool WebPack option.
I can't remember exactly, but I think I used eval-cheap-module-source-map or eval-source-map.
I'm currently developing an Nodejs application and carrying out some unit tests (I'm using Mocha, Chai and Sinon).
I ran into a little ESlint error when I exported and tested an internal function.
function _buildPayload(){
//....
}
module.exports = { _buildPayload };
Then in my test script
const {_buildPayload} = requires('./someModule')
describe('Test',function(){
it('Should work',function(){
let expected = _buildPayload();
})
})
When I write the let expected = _buildPayload(); ESlint returns the following error:
error Shouldn't be accessing private attribute '_buildPayLoad'
My question is should I change the name of my function to not represent and internal even though it is?
#philipisapain makes a good point that testing internal methods may not be necessary. If you do need to do it, you have a couple options:
Disable the rule by placing /* eslint-disable rule-name */ at the top of any test scripts that call private methods.
Disable the rule in all test scripts using a glob config in your .eslintrc, provided you're using at least ESLint v4.1.0:
"overrides": [{
"files": ["*.test.js"],
"rules": [{
"rule-name": "off"
}]
}]
My log4js.js file code
'use strict';
var log4js = require('log4js');
var log4jsGen = {
getLogger: function getLogger() {
log4js.loadAppender('file');
log4js.addAppender(log4js.appenders.file('./ApplicationLogs.log'), 'logs');
var logger = log4js.getLogger('logs');
return logger;
}
};
module.exports = log4jsGen;
My conf.js file(specific to appender section only)
"appenders": [{
"type": "log4js-protractor-appender",
"append": 'false',
"maxLogSize": 20480,
"backups": 3,
"category": "relative-logger"
}],
Problem:
1) IS there a way that the logs will get overwritten in each run.
2) Why log4js-protractor-appender is not working, instead log4js is working, the merit of the previous is that it resolves the promises which is passed as an argument.
Thats a great question. Yes log4js-protractor-appender is awesome. It is built specially for Protractor based environments and it places all logger command in Protractor Control flow and resolves Protractor promises before logging.
You were using it incorrectly. The appender options are not part of Protractor config options but can be integrated. The approach you have is a little old one and I have updated by blog post
These are the steps as an answer to your question-2
Step 1: Install log4js npm module
Step 2: Install log4js-protractor-appender module
Step 3: Add the logger object creation logic in protractor beforeLaunch() and assign it onto browser protractor global object
'use strict';
var log4js = require('log4js');
beforeLaunch:function(){
if (fs.existsSync('./logs/ExecutionLog.log')) {
fs.unlink('./logs/ExecutionLog.log')
}
log4js.configure({
appenders: [
{ type: 'log4js-protractor-appender', category: 'protractorLog4js' },
{
type: "file",
filename: './logs/ExecutionLog.log',
category: 'protractorLog4js'
}
]
});
},
onPrepare: function() {
browser.logger = log4js.getLogger('protractorLog4js');
},
Step 4: Use logger object in your tests by accessing through browser.logger
describe('sample test', function(){
it('Sample Check', function(){
browser.get("http://www.protractortest.org/#/");
browser.logger.info("Testing Log4js");
browser.sleep(5000);
browser.logger.info('Displayed text is:', browser.getCurrentUrl());
var elm = element(by.css('.lead'))
browser.logger.info('Displayed text is:', elm.getText());
});
});
But one thing to note is - This appender is just an console appender and will not be able to write to file. The file will still contain unresolved promises
Sample Output:
[21:54:23] I/local - Starting selenium standalone server...
[21:54:23] I/launcher - Running 1 instances of WebDriver
[21:54:25] I/local - Selenium standalone server started at http://192.168.1.5:60454/wd/hub
Started
[2017-02-03 21:54:30.905] [INFO] protractorLog4js - Testing Log4js
[2017-02-03 21:54:35.991] [INFO] protractorLog4js - Displayed text is: http://www.protractortest.org/#/
[2017-02-03 21:54:36.143] [INFO] protractorLog4js - Displayed text is: Protractor is an end-to-end test framework for Angular and AngularJS applications. Protractor runs tests against your application running in a real browser, interacting with it as a user would.
.
Answer to your Question 1: How to overwrite logs each run. I added a simple logic in beforeLaunch() to delete old logs if they exist and its part of the code snippet I pasted above
I have check this issue with and followed the steps mentioned in Answer 1 and it works for me.
Earlier I was getting log output in Console only but now I am getting log in console and file also.
I corrected the file path passing and Set type: "file" in log4js configure in conf file.
Log4js in Conf file
Log appender in file
Please let me know if you face any issue again.
Thanks
I'm using requirejs for a not super complicated project- problem is that I have some utility methods that log information to console and it's brought to my attention a question I've had for a while but never asked:
Say you have karma running unit tests on roughly a few billion files and one of them is logging to the console...
Without using a stack trace, how can you determine the name / location of that ONE file?
or
What would be the easiest way to filter / refine / define karma's output (other than adjusting logLevel)?
I've looked into different reporters, and will be trying to write one for karma here soon, I'm just trying to make sure I know what's available (if applicable).
karma.conf.js:
module.exports = function (config) {
config.set ({
basePath : '../',
frameworks : ['mocha', 'requirejs', 'chai'],
files : [
{pattern: 'tests/_*.js', included: false},
'tests/test-main.js'
],
reporters : ['dots', 'growl'],
port : 9876,
logLevel : config.LOG_DEBUG,
//autoWatch : true,
autoWatch : false,
plugins : [
'karma-requirejs',
'karma-mocha',
'karma-chai',
],
singleRun : false
});
};
I see that you are using mocha: I would suggest to have more a "test-title" approach rather than a "filename" one.
What about a solution like the following - it could be implemented in karma as well with a custom reporter:
afterEach(function(){
// use here a global variable or
// appending it as property on the runner
if(variableToLookAt){
console.log(this.currentTest.fullTitle() + ': '+your_message_here);
}
});
The snippet above can be insert inside a describe suite block, or better in case of nested suites, in every block that contains a test (it).
I am using Jasmine (BDD Testing Framework for JavaScript) in my firefox add-on to test the functionality of my code.
The problem is that jasmine is outputing the test results to an HTML file,what I need is to Firebug Console or other solution to output the results.
Have you tried the ConsoleReporter?
jasmine.getEnv().addReporter(new jasmine.ConsoleReporter(console.log));
According to the code Jasmine has the ConsoleReporter class that executes a print function (in this case console.log) that should do what you need.
If all else fails you could just use this as a starting point to implement your own console.log reporter.
UPDATE
In newer versions of jasmine, ConsoleReporter was removed. You can either use the built-in jsApiReporter, or write your own (console) reporter, as shown in the following link: https://jasmine.github.io/tutorials/custom_reporter
In newest version of Jasmine (2.0) if you want to get test output to console you need to add following lines.
var ConsoleReporter = jasmineRequire.ConsoleReporter();
var options = {
timer: new jasmine.Timer,
print: function () {
console.log.apply(console,arguments)
}};
consoleReporter = new ConsoleReporter(options); // initialize ConsoleReporter
jasmine.getEnv().addReporter(consoleReporter); //add reporter to execution environment
Output to html is included by default however so if you don't want html output at all you have to edit your boot.js file and remove relevant lines from there. If you want to customize how output is displayed in console edit file console.js.
Source
jasmineRequire.ConsoleReporter did not exist in 2.3.0 so I used the following code:
//create a console.log reporter
var MyReporter = function(){jasmineRequire.JsApiReporter.apply(this,arguments);};
MyReporter.prototype = jasmineRequire.JsApiReporter.prototype;
MyReporter.prototype.constructor = MyReporter;
MyReporter.prototype.specDone=function(o){
o=o||{};
if(o.status!=="passed"){
console.warn("Failed:" + o.fullName + o.failedExpectations[0].message);
}
};
var env = jasmine.getEnv();
env.addReporter(new MyReporter());
For the sake of completeness here's the full configuration:
First of all run the npm install command:
npm install jasmine-console-reporter --save-dev
Then check your Jasmine configuration to make sure you got the helpers setting there:
spec/support/jasmine.json
{
"spec_dir": "spec",
"spec_files": [
"**/*[sS]pec.js"
],
"helpers": [
"helpers/**/*.js"
],
"stopSpecOnExpectationFailure": false,
"random": false
}
Since helpers are executed before specs the only thing you have to do is to create a console reporter helper.
spec/helpers/reporter/consoleReporter.js
const JasmineConsoleReporter = require('jasmine-console-reporter');
let consoleReporter = new JasmineConsoleReporter({
colors: 1, // (0|false)|(1|true)|2
cleanStack: 1, // (0|false)|(1|true)|2|3
verbosity: 4, // (0|false)|1|2|(3|true)|4
listStyle: 'indent', // "flat"|"indent"
activity: false
});
jasmine.getEnv().addReporter(consoleReporter);
jasmine-console-reporter on npmjs.com
Jasmine custom reporter docs
Jasmine configuration reference