gpt4 book ai didi

javascript - Nightmare.js 和代码覆盖率

转载 作者:行者123 更新时间:2023-11-30 05:30:51 25 4
gpt4 key购买 nike

简短版:

我无法看到我使用 nightmare.js 和 mocha 编写的测试的代码覆盖率。到目前为止,我已经尝试过使用 istanbul 和 _mocha,但没有成功。

大版本:

我有一个小项目:

/public/index.html

<html lang="en">
<head>
<meta charset="UTF-8">
<title>My Website</title>
<script src="./js/hello.js"></script>
</head>
<body>
<h1>My Website</h1>
</body>
</html>

/public/js/hello.js

window.hello = function hello(){
return 'world';
};

该站点使用 express 和 forever 运行。

当我尝试使用 nightmare.js 对其进行测试时。

/测试/测试.js

var path = require('path');
var Nightmare = require('nightmare');
var should = require('should');

/*global describe */
/*global it */

describe('Simple demo', function () {
this.timeout(15000);
var url = 'http://localhost:9000';

it('check hello world result', function (done) {
new Nightmare()
.goto(url)
.evaluate(function () {
/*global hello */
return hello();
}, function (value) {
var expected = "world";
if (value === null) {
false.should.equal(true);
return done();
}
value.should.equal(expected);
return done();
})
.run();
});

it('should get the index title', function (done) {
var expected = 'My Website';
new Nightmare()
.goto(url)
.title(function (title) {
title.should.equal(expected);
done();
})
.run();
});
});

测试通过

$ mocha


Simple demo
✓ check hello world result (2089ms)
title = Alexandria
✓ should get the index title (1947ms)


2 passing (4s)

但是,我无法从我的测试中获取代码覆盖率报告。

我已经尝试过一些命令,例如:

$ istanbul cover _mocha -- test/test.js -u exports -R spec
No coverage information was collected, exit without writing coverage information

$ istanbul cover --hook-run-in-context _mocha -- -R spec
No coverage information was collected, exit without writing coverage information

那么,有人能够创建 nightmare.js 测试的代码覆盖率报告吗?如果不是,使用其他工具是否有类似的东西?

最佳答案

我在我的项目中遇到了同样的问题。我找不到任何可以简单地解决此问题的库或配置,但通过一些实现和 Grunt 配置,您可以从 Grunt 流程中获得代码覆盖率。

我在我的项目中使用的依赖:

"chai": "^3.5.0",
"grunt": "^0.4.5",
"grunt-contrib-clean": "^0.7.0",
"grunt-contrib-copy": "^0.8.2",
"grunt-express-server": "^0.5.3",
"grunt-istanbul": "^0.7.1",
"grunt-mocha-test": "^0.12.7",
"istanbul": "^0.4.4",
"nightmare": "^2.2.0"

我的项目结构:

public/     -- public folder for index.html
src/ -- source folder for hello.js
test/ -- mocha tests implementation
server/ -- express implementation for server.js
coverage/ -- HTML report from code coverage
report/ -- HTML report from mocha tests
dist/ -- folder which is used by express server to get content, generated by Grunt

您必须从 Grunt 运行的步骤:

grunt.registerTask('test_hello', [
'clean', // clean dist/ folder
'copy:public', // copy public files from public/ (ex. index.html) to dist/
'instrument', // generate instruments (ex. hello.js) for code coverage from src/ by istanbul
'express', // run express server from dist/ folder
'mochaTest', // run mocha tests with nightmare and generate HTML report to report/ folder
'report_coverage' // generate HTML report from code coverage to coverage/ folder
]);

Grunt 配置:

module.exports = function (grunt) {

grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-express-server');
grunt.loadNpmTasks('grunt-istanbul');
grunt.loadNpmTasks('grunt-mocha-test');

grunt.initConfig({
clean: {
dist: ['dist/', 'report/', 'coverage/']
},
copy: {
public: {
expand: true,
cwd: 'public/',
src: ['**'],
dest: 'dist/'
}
},
instrument: {
files: ['**/*.js'],
options: {
cwd: 'src/',
lazy: true,
basePath: 'dist/'
}
},
express: {
dev: {
options: {
port: 9000,
script: 'server/server.js',
background: true
}
}
},
mochaTest: {
hello: {
options: {
timeout: 10000,
captureFile: 'report/results.txt', // Optionally capture the reporter output to a file
quiet: false, // Optionally suppress output to standard out (defaults to false)
clearRequireCache: false // Optionally clear the require cache before running tests (defaults to false)
},
src: ['test/*.js']
},
}
});

grunt.registerTask('report_coverage', function () {
var coverage = require('./test/utils/coverage');
coverage.generateReport();
});

grunt.registerTask('test_hello', [
'clean', // clean dist/ folder
'copy:public', // copy public files from public/ (ex. index.html) to dist/
'instrument', // generate instruments (ex. hello.js) for code coverage from src/ by istanbul
'express', // run express server from dist/ folder
'mochaTest:hello', // run mocha tests with nightmare and generate HTML report to report/ folder
'report_coverage' // generate HTML report from code coverage to coverage/ folder
]);

}

我还创建了一个类,它允许我从每个 Nightmare 实例中收集代码覆盖率:

var istanbul = require('istanbul');
var reporter = new istanbul.Reporter(),
sync = true;

var Coverage = function () {

this.loadCodeCoverage = function (dom, done) {
dom
.evaluate(function () {
return window.__coverage__; // this variable store all information about code coverage
})
.then(function (coverageDate) {
if (coverageDate) {
this.getCollector().add(coverageDate);
}
done();
}.bind(this))
.catch(function (error) {
done(error);
});
}

// load page by nightmare
this.getCollector = function () {
if (!this.collector) {
this.collector = new istanbul.Collector();
}
return this.collector;
}

this.generateReport = function () {
reporter.add('text');
reporter.addAll(['lcov', 'clover']);
reporter.write(this.collector, sync, function () {
console.log('All reports generated');
});
}

}

module.exports = new Coverage();

对于上面的配置文件test/test.js应该有下面的结构:

var should = require('should');
var coverage = require('./utils/coverage');

/*global describe */
/*global it */

describe('Simple demo', function () {
this.timeout(15000);
var url = 'http://localhost:9000';

before(function (done) {
global.dom = new Nightmare()
.goto(url)
.evaluate(function () {
return 'test';
})
.then(function(result) {
done();
})
.catch(function(error) {
done(error);
});

});

after(function (done) {
coverage.loadCodeCoverage(dom, done);
});

it('check hello world result', function (done) {
dom.evaluate(function () {
/*global hello */
return hello();
})
.then(function(value) {
var expected = "world";
if (value === null) {
false.should.equal(true);
return done();
}
value.should.equal(expected);
return done();
})
.catch(function(error) {
done(error);
});
});

it('should get the index title', function (done) {
var expected = 'My Website';
dom.title(function (title) {
title.should.equal(expected);
done();
});
});

});

如果一切正常,您应该在控制台上获取信息,并且应该在文件夹覆盖率中生成 HTML 格式的代码覆盖率报告/

Running "report_coverage" task
------------------------------------|----------|----------|----------|----------|----------------|
File | % Stmts | % Branch | % Funcs | % Lines |Uncovered Lines |
------------------------------------|----------|----------|----------|----------|----------------|
js/ | 100 | 100 | 100 | 100 | |
hello.js | 100 | 100 | 100 | 100 | |
------------------------------------|----------|----------|----------|----------|----------------|
All files | 100 | 100 | 100 | 100 | |
------------------------------------|----------|----------|----------|----------|----------------|

我仍然遇到的问题是,对于每个测试描述,我都必须添加方法beforeafter。将这些实现仅放在一个地方可能会很好,例如在 Grunt 配置中的某个地方,我在做新的测试描述时不需要记住它们。

如果有人为 beforeafter 方法找到更通用的解决方案,我们将不胜感激。

关于javascript - Nightmare.js 和代码覆盖率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27156047/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com