Tag: 摩卡

摩卡柴testing受保护的路线

我有一个Express服务器,我使用passport-local进行身份validation。 我有以下受保护的路线: app.post("/api/test", connect.ensureLoggedIn("/"), (req, res) => { let test = new Test(req.body); test .save() .then(data => { return res.json(data); }) .catch(e => { return res.status(HTTP_RESPONDE_BAD_REQUEST).send(e); }); }); 我想知道如何testing上面提到的路线,确保用户login。 这是我目前的testing(它没有通过,因为我无法发送authentication: it("Testing protected route", done => { chai .request(server) .post("/api/test") .send(test) .end((err, res) => { expect(res.status).to.equal(200); done(); }); }); 我已经尝试了以下,但是当我运行testing他们redirect到login页面。 it("Testing protected route", done => { […]

如何在使用npm run运行端到端testing之前启动节点服务器?

我正在写一个快速站点的端到端testing,我想在package.js中添加一个“testing”命令 这个命令需要: 运行eslint 编译打字稿 启动节点服务器 对该服务器运行unit testing并显示输出。 一旦完成testing,closures服务器。 我知道如何单独执行所有这些命令,但不是一次全部执行。 我现在拥有的是: npm run compile && npm run build && node ./dist/server.js –db=test && npm run test 它的工作原理是:“ && npm run test ”,因为节点服务器正在运行,它不会继续到下一个命令,如果它closures,那么testing将不会运行。 任何帮助表示赞赏。

节点/ vue.jsunit testing的承诺,如何正确写入?

基本上,我试图testing下面的vex / action: export default { login (context, payload) { return vueAuthInstance.login(payload.user, payload.requestOptions) .then((response) => { if (JSON.stringify(response.data) !== '{}') { return true } else { return false } }) } } 我有一个依赖vueAuthInstance我需要存根/模拟…它被调用,它有一个login函数,有两个参数,它返回一个对象(响应)与数据属性 vueAuthInstance = {login: sinon.stub().withArgs({email: 'email', password: 'password'}, 'options').return({data: 'user_data_with_token'}) } 我的login函数实际上就像调用vueAuthInstance存根,所以我应该可以写 actions.login(context, payload).then((response) => { expect(response).to.eql(true) }) 但我被locking在一个正确的testing describe('login', () => { […]

“npm运行覆盖”不与摩卡一起运行

我正尝试在express.js应用程序上使用摩卡来运行伊斯坦布尔 每次运行cmd npm run coverage时,我都会在cmd中得到这个错误: Unable to resolve file [node_modules/mocha/bin/_mocha] Try "istanbul help" for usage npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! dabl-demo@1.0.0 coverage: `istanbul cover node_modules/mocha/bin/_mocha` npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the dabl-demo@1.0.0 coverage script. npm ERR! This is probably not a problem with npm. […]

与asyc回拨摩卡不执行我之前,它阻止

我在testing套件中有摩卡testing。 我注意到所有的testing,即使他们应该失败通过。 我把一些console.log语句,并实现摩卡没有进入之前或它阻止 当我运行下面,1,3和4打印到控制台,与testing通过________________________________________ process.env.NODE_ENV = 'development'; if (!process.env.PWD) { process.env.PWD = process.cwd(); } const base = process.env.PWD; const config = require(base + '/config'); const logger = require('mocha-logger'); const mongoose = require('mongoose'); const Org = require(base + '/models/org'); const orgs = require(base + '/controllers/org'); const should = require('chai').should(); const testUtils = require(base + '/test/utils'); let […]

为摩卡testing设置数据库连接的最佳实践和最有效的方法

我有一套testing,我在我的代码上运行。 在testing运行之前,我想打开一个到testing数据库的数据库连接,并确保数据库是空的。 这个连接将保持对所有testing打开。 然后,当所有testing完成后,closures数据库连接并清空数据库。 我目前的解决scheme涉及打开每个文件的连接,这将导致大量的连接整体。 这将是理想的打开连接一次 – >运行testing – >清除数据库 – >closures连接一次。 这是我的一个摩卡testing文件的代码: import {assert} from 'chai'; import mongoose from 'mongoose'; import User from '../../../server/models/user.js'; import 'dotenv/config'; mongoose.connect(process.env.DB_TEST).then(db => { describe('User Model', function() { it('Save', function(done) { var john = new User({ name: { first: 'John', last: 'smith' }, email: 'john.smith@gmail.com', type: 'student' }); john.save(done); […]

configuration模拟cli

我试图使用模拟cli来存储cli应用程序的摩卡testingprocess.arv。 我想testing一个消息是console.logged当一个不正确的参数(“imit”)被传递给process.argv(由命令定义)。 我正在尝试修改文档中的示例,但是我不认为我已经正确设置了所有内容。 它通过时,我注释掉“ stdin: require('../mocks/fakeInputStream'), // Hook up a fake input stream ”虽然我知道它不能正常工作 它与TypeError: sourceStream.on is not a function失败TypeError: sourceStream.on is not a function运行时TypeError: sourceStream.on is not a function ,如下所述 有人可以看到我失踪? /index.js var commands = ['init']; function getGitHeadArgs() { return process.argv.slice(2, process.argv.length); } if (getGitHeadArgs().length) { if (!commands.includes(getGitHeadArgs()[0])) { console.log("Silly Githead! That's not a […]

节点JS摩卡testing用例没有运行

我正试图build立一个testing用例的testing套件。 由于某种原因,我们没有把我们的射击钩住,所以我使得testing用例尽可能简单。 除了前面描述的之外,还没有任何一个debug.log打印出来。 这几乎就像代码没有看到我的摩卡testing。 我有摩卡安装本地作为开发依赖。 即使我的代码中没有那么多的testing用例,它也会返回一些testing用例。 我已经尝试卸载并重新安装摩卡。 terminal返回** / test / runner.jsinputOrg Runnerconfiguration设置数据库信息:mongodb:// xxxxx:xxxxxxxx @ localhost / icpDB 657个testing。 0失败。 268毫秒 进程使用退出码0结束 const mocha = require('mocha'); const chai = require('chai'); const debug = require('debug'); debug.log('Entered Org Runner'); describe('Org api test suite', function () { before(function (done) { debug.log('Entered Org Before'); done(); }); after(function (done) { […]

摩卡testing与inheritance的JavaScript

我是Mocha和Node的新手,但是尝试在一些JavaScript类上写一些Mochatesting。 我有以下class级: function FormField() { } FormField.prototype.sanitizeFieldValue = function(value) { if (value == null || value.replace == null) { return null; } return value .replace(/&nbsp;/g, " ") .replace(/&/g, '&amp;') .replace(/\\/g, '\\\\') .replace(/'/g, "\\'") .replace(/</g, '&lt;') .replace(/>/g, '&gt;') .replace(/"/g, '&quot;') .replace(/[\n\r]+/g, " "); }; module.exports = FormField; 当我在这个文件上运行我的摩卡testing时,一切正常,testing通过。 var expect = require("chai").expect; var formfield = require("FormField"); […]

加快meteortesting刷新

在testingMeteor应用程序时,我注意到Meteor做了很多重复的任务,比如 下载缺less的软件包(几秒钟) 在“下载缺失的软件包”之前,尤其是在unit testing或其他特定于应用程序的代码(即没有新的导入)内进行更改时,检查更新的package.json和.meteor/versions会更有效率。 build立web.cordova(我不使用) 我非常肯定,指定testing的目标是可能的,所以这个项目只是为web.browser构build,例如。 如果有错误,一切都build立和执行三次(失败三次)。 当testing失败时,为什么必须使用完全相同的代码再次尝试? 有没有什么用例,这是有道理的? 现在,每当testing模块发生变化时,由于所有这些任务,testing再次运行几秒钟。 有没有什么办法来优化,使其更高效和更快?