Tag: 摩卡

Node.js发布请求testingsupertest,express,mocha返回undefined

我曾在网站上看过类似的问题,但我不明白它们如何适用于我的问题: const request = require('supertest'); var express = require('express'); const mocha = require('mocha'); var bodyParser = require('body-parser') var app = express() app.use(bodyParser()); // parse application/x-www-form-urlencoded app.use(bodyParser.urlencoded({ extended: false })) // parse application/json app.use(bodyParser.json()) describe("Post valid ID", function(){ it("returns expected data", function(done){ var goodID = ({id: '100', name: 'Edith'}); request("http://localhost:8000") .post("/users") .set('Accept', 'application/json') .type("json") .expect('Content-Type', 'application/json; […]

摩卡和相对path

我的代码是基于组件的,testing直接驻留在组件文件夹中。 一些组件依赖文件(或其他资源),如ConfigurationManager组件: class ConfigurationManager { constructor(protected configDir = 'config') { } addResource(file) { let fileUri = path.join(path.dirname(require.main.filename), this.configDir, file); let configuration = require(fileUri); // … } } config目录位于项目的根目录下,因此fileUri在这里是有意义的。 但是当运行testing时( mocha -r ts-node/register src/Components/**/Tests/*Test.ts )会失败,因为根目录不再是项目根目录,而是在node_modules的mocha目录。 如何解决这个问题?

使用Mocha生成节点应用程序testing和LCOV报告

我的团队最近开始研究一个完全由ES6编写的先前存在的Node应用程序(在我们开始工作之前,它已经通过了几个不同的开发者)。 该应用程序使用摩卡和Nyan作为摩卡记者进行了最低程度的unit testing。 package.json文件已经包含以下命令来运行testing: "test": "node ./node_modules/mocha/bin/mocha –compilers js:babel-core/register ./test/**/*.spec.js" 作为我们部署周期的一部分,我们使用Travis和Codacy的CI / CDpipe道。 因此,为了启用Codacy的“代码覆盖”部分,我需要对应用程序库和package.json文件进行一些更改。 基本的想法是让摩卡生成一个.lcov文件,然后通过Travis构build脚本传送到Codacy。 做了一些研究后,我把以下内容成功地生成了lcov文件: "build-test": "node ./node_modules/mocha/bin/mocha -R mocha-lcov-reporter > coverage/coverage.lcov –compilers js:babel-core/register ./test/**/*.spec.js –reporter nyan" 问题是,我得到了.lcov文件的Codacyparsing错误。 我查看了其他项目的lcov文件,似乎还有额外的数据在我生成的。 这里的参考是生成的lcov文件: 0 0 0 [4A[5C- [5C- [5C- [5C- [4A[6C_,——, [6C_| /\_/\ [6C^|__( – .-) [6C "" "" [4ACONSTRUCTOR 1 0 0 [4A[5C-_ [5C-_ [5C-_ [5C-_ […]

当使用摩卡进行testing时,Node.js执行CronJob

所以,我有一些js代码,这是一个slackbot,应该简单地监听和parsing提供的date,然后启动一个CronJob按照提供的cron或date格式运行某个函数。 像这样的东西。 var CronJob = require ('cron').CronJob; … robot.respond(date, function (msg)) { if(!isValidDate(date)) msg.reply("not a valid date); var interval = isCronDate(date) ? date : new Date(date); msg.reply("Job about to be scheduled.") var schedule = new CronJob(interval, processDataOnDate(), function() { msg.reply("hello") }, true); } 我有一个testing这个代码的咖啡文件,我期望得到一些反馈,但是我不希望根据我在testing代码中提供的date来执行cron作业。 但是,这是。 这是正常的吗? 由于这是一个unit testing,摩卡强迫代码完成执行,还是我做错了什么? 我正在运行这个来执行我的unit testing。 mocha –compilers coffee:coffee-script/register 为了得到更多的信息,我把这个作为一个slackbot运行,所有这些都是以“say”和“reply”的forms完成的。 我的一个testing看起来像这样。 […]

在浏览器中使用Webpack和mocha-loader从React应用程序运行Mocha规范

我有一个应用程序,我想在浏览器中运行它的mocha规格(unit testing)。 我发现这个SOpost,并试图将相同的想法应用到我的项目。 我想出了下面的webpackconfiguration文件: webpack.config.test.js const nodeExternals = require('webpack-node-externals'); const path = require('path'); const host = 'localhost'; const port = '8084'; module.exports = { target: 'web', externals: [nodeExternals()], entry: './specs/index.js', output: { filename: 'debug.bundle.js', path: path.join(__dirname, 'tests'), publicPath: `http://${host}:${port}/tests`, }, devServer: { host, port, }, module: { rules: [ { test: /\.js$/, loaders: ['react-hot-loader', 'babel-loader'], […]

如何使用摩卡在reactjs中testing这个asynchronous方法调用

// Balance.jsx … updateToken () { const parseResponse = (response) => { if (response.ok) { return response.json() } else { throw new Error('Could not retrieve access token.') } } const update = (data) => { if (data.token) { this.data.accessTokenData = data } else { throw new Error('Invalid response from token api') } } if (this.props.balanceEndpoint […]

“node –debug”和“node –debug-brk”是无效的

我已经更新了节点(v8.1.2)。 当我想使用NTVS在visualjs中debugging我之前的testing项目(在visual studio 2017中)时,我得到了以下错误: StandardError:(node:5292)[DEP0062] DeprecationWarning: node –debug和node –debug-brk无效。 请使用node –inspect或node –inspect-brk来代替。 问题很清楚。 该工具尝试运行node –debug – debugging以启动项目。 问题是,如何在Visual Studio 2017 node –inspect node –debug更改为node –inspect来运行testing? 另外,我应该提到,我不能通过在脚本参数中添加–inspect来解决问题。

单独运行摩卡testing

我的test/项目充满了摩卡testing: test/ ├── a.coffee └── b.coffee 说,一个a.coffee是 console.log 'Executing A test!' global.foo = 'FOO' 和b.coffee是 console.log 'Executing B test!' console.log 'Expecting foo not defined:', assert.equal(foo, undefined) 在执行摩卡时: $ mocha –compilers coffee:coffee-script test/* Executing A test! Executing B test! Expecting foo not defined: false 它看起来像testing共享相同的全球对象(我想避免)… 有没有办法单独执行每个testing? 谢谢。

当摩卡testing失败时,它将一个对象logging到控制台

当我所有的testing通过,一切都按预期工作。 然而,当一个testing失败并且任何记者处于活跃状态时,赛跑者都会打印出一个代表追踪痕迹的对象,而不是我在屏幕截图中看到的格式不正确的错误消息 > mocha –reporter spec –ui bdd –ignore-leaks Site Routes GET /sites ✓ should return a 200 OK ✓ should be a JSON response ◦ should be an array: { "date": "Fri Apr 19 2013 16:09:20 GMT-0400 (EDT)", "process": { "pid": 34270, "uid": 1032026306, "gid": 116266774, "cwd": "/Users/squinn/Projects/armada", "execPath": "/usr/local/Cellar/node/0.10.4/bin/node", "version": "v0.10.4", "argv": [ […]

用摩卡testing二进制file upload

我目前正在研究一个带有nodejs的小API,并且要求通过接收一个二进制string来完成file upload。 我不知道该怎么做,是testing它与摩卡,所以我一直在做一些search,发现堆栈溢出unit testingfile upload与摩卡 ,它是一个很好的开始,但它不会工作,因为它发送一个多部分的forms,什么我要求客户端发送的api文件就是一个stream。 inheritance人我的控制器: exports.uploadVideo = function(req, res, next) { var video = "public/video/" + req.params.videoId + ".mp4", util = require('util'), exec = require('child_process').exec; var newFile = fs.createWriteStream("./uploads/" + video); req.pipe(newFile); req.on('end', function () { var cmd = 'qtfaststart ' + './uploads/' + video; var qtfaststart = exec(cmd, function(error, stdout, stderr){ if […]