Tag: unit testing

对所有testing运行ava test.before()一次

我想用test.before()来引导我的testing。 我试过的设置不起作用: // bootstrap.js const test = require('ava') test.before(t => { // do this exactly once for all tests }) module.exports = { test } // test1.js const { test } = require('../bootstrap') test(t => { … {) AVA将在每个testing文件之前运行before()函数。 我可以before通话before做一个检查,看看是否被调用,但我想找一个更清洁的过程。 我已经尝试使用require参数: "ava": { "require": [ "./test/run.js" ] } 附: // bootstrap,js const test = require('ava') […]

你将如何嘲笑/分支一个快速路线的请求?

例如,如果我想在这个给定的方法中嘲笑一个请求,我该怎么做? app.get('/test', exposeDb, function(req, res) { req.dbService.getUserByID(req.body, function(err, result) { if (result == 5) { console.log(result); res.json({result: result}); } else { console.log(result); res.json({result: result}); } }); }); 这是我目前的testing: describe('Test', function() { beforeEach(function() { var getUserByIDStubE = sinon.stub(dbService, 'getUserByID').callsFake(cb => { cb(undefined, 5); }); }); it('TESTSETSTETS', function(done) { chai.request(server) .get('/test') .send({ req }) .end(function (err, res) […]

如何在使用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将不会运行。 任何帮助表示赞赏。

在Node JS磁带中独立进行testing

我正在尝试在脚本中testing我在Node JS中编写的函数。 我正在使用磁带进行unit testing。 但是我面临着如何使每个testing用例都是独立的问题。 例如。 脚本中有一些全局variables。 这些variables的值在这些方法中被设置。 现在问题是这些variables在不同的unit testing用例之间共享这些值。 有没有办法在运行testing用例之前清除所有全局variables?

NodeJS模仿保存模型似乎不工作

试着unit testing我的小api,我需要testing保存错误和成功(已经注册等)。 目前这里是我的代码: models / auth.js var mongoose = require('mongoose'); var Schema = mongoose.Schema; var AuthSchema = Schema({ id: String, user: String, password: String, origin: String, }); //Export model module.exports = mongoose.model('user', AuthSchema); testing/ api.test.js const chai = require('chai'); const should = chai.should; const expect = chai.expect; const assert = chai.assert; / Node Http Mocks […]

如何testingpassport.js进行身份validation

我想testing我的passport.jsauthentication中间件: passport.authenticate('jwt', { session: false }, (err, user, info) => { if (err) return res.serverError(err) if (!user) return res.forbidden(info) req.user = user next() })(req, res, next) 中间件正在工作。 但是我不知道如何等待testing中的callback。 testing看起来像这样: const token = JwtAuthService.issue({ id: 1, email: 'test@email.com' }) const req = httpMocks.createRequest({ headers: { authorization: `bearer ${token}` } }) const res = { forbidden: sinon.spy() […]

我怎样才能使用JEST间谍来testing一个NodeJs模块/应用程序

我不明白如何侦测一个模块内部的方法/函数是否被触发。 这是我的例子: db.js module.exports.saveUser = (user) => { console.log('Saving the user', user); }; app.js let db = require('./db'); module.exports.handleSignup = (email, password) => { db.saveUser({ email, password }); } app.test.js const db = require('./db'); jest.genMockFromModule('./app'); const app = require('./app'); describe('App: ', () => { it('should call "db.saveUser" with a user object', () => { let […]

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) { […]

Proxyquire:不能存根Cassandra驱动程序

我试图嘲笑Cassandraunit testing用例,我使用npm Cassandra-driver来连接/查询cassandra。 嘲笑存根这是我的代码。 beforeEach(function () { let CassandraClient = sinon.stub(); CassandraClient.prototype.execute = sinon.stub().returns(new Promise((fulfill, reject) => { console.log("Inside execute method"); fulfill(cassndaraExperimentQueryData); })); const CassandraClientWrapper = proxyquire('../../../../api/services/index.js', { 'cassandra-driver': { Client: CassandraClient } }); console.log(typeof CassandraClientWrapper); CassandraClientWrapper.init(); }); 这是我的index.js的外观 const cassandra = require('cassandra-driver'); module.exports = class DBServices { init(){ const contactPoint = process.env['cassndraIP'] || '127.0.0.1'; […]