Setting up html report with Jasmine

Viewed 242

I'm new to BDD and Jasmine, and I'm not able to configure https://www.npmjs.com/package/jasmine-pretty-html-reporter to get html of test pass rate.

There it mentions a basic setup:

var Jasmine = require('jasmine');
var HtmlReporter = require('jasmine-pretty-html-reporter').Reporter;
var jasmine = new Jasmine();

jasmine.loadConfigFile('./spec/support/jasmine.json');

// options object
jasmine.addReporter(new HtmlReporter({
  path: path.join(__dirname,'results')
}));

jasmine.execute();

However, I'm not sure where should I perform those configuration (in which file). Could anyone help me with this, please?

Thanks in advance!

1 Answers

The setup provided by this package is used jasmine as a library. See Using the library.

This means we can execute test cases programmatically. We execute this module via node, e.g.

Folder structure:

⚡  tree -L 2 -I 'node_modules'
.
├── README.md
├── jasmine.json
├── package-lock.json
├── package.json
├── src
│   ├── helpers
│   └── stackoverflow
├── test-reporter
│   └── report.html
└── tsconfig.json

4 directories, 6 files

src/stackoverflow/70338811/as-a-library.js:

var Jasmine = require('jasmine');
var path = require('path');
var HtmlReporter = require('jasmine-pretty-html-reporter').Reporter;
var jasmine = new Jasmine();

jasmine.loadConfigFile(path.resolve(__dirname, '../../../jasmine.json'));

jasmine.addReporter(
  new HtmlReporter({
    path: path.join(__dirname, '../../../test-reporter'),
  }),
);

jasmine.execute();
npx ts-node <Absolute_path>/src/stackoverflow/70338811/as-a-library.js

If you want to use jasmine as a CLI and run the test cases via npm script. You should add this custom reporter to the Jasmine environment. See Reporters. We need to create a helper file in src/helpers folder to add custom reporter, e.g. pretty-html-reporter.js

var HtmlReporter = require('jasmine-pretty-html-reporter').Reporter;
var path = require('path');

jasmine.getEnv().addReporter(
  new HtmlReporter({
    path: path.join(__dirname, '../../test-reporter'),
  }),
);

npm script:

"test": "ts-node node_modules/jasmine/bin/jasmine --config=./jasmine.json",

Now, let's run test cases of a specific file.

 ⚡  npm t /Users/dulin/workspace/github.com/mrdulin/jasmine-examples/src/stackoverflow/69830430/index.test.ts
> jasmine-examples@ test /Users/dulin/workspace/github.com/mrdulin/jasmine-examples
> ts-node node_modules/jasmine/bin/jasmine --config=./jasmine.json "/Users/dulin/workspace/github.com/mrdulin/jasmine-examples/src/stackoverflow/69830430/index.test.ts"

Randomized with seed 02843
Started
.


1 spec, 0 failures
Finished in 0.014 seconds
Randomized with seed 02843 (jasmine --random=true --seed=02843)

The test reporter is generated in the test-reporter folder.

enter image description here

source code: https://github.com/mrdulin/jasmine-examples

Related