我想对下面的ES6类进行单元测试:
// service.js
const InternalService = require('internal-service');
class Service {
constructor(args) {
this.internalService = new InternalService(args);
}
getData(args) {
let events = this.internalService.getEvents(args);
let data = getDataFromEvents(events);
return data;
}
}
function getDataFromEvents(events) {...}
module.exports = Service;
Run Code Online (Sandbox Code Playgroud)
我如何嘲笑与Sinon.JS构造,以模拟getEvents的internalService测试getData?
我查看了使用Sinon的Javascript:Mocking Constructor,但无法提取解决方案.
// test.js
const chai = require('chai');
const sinon = require('sinon');
const should = chai.should();
let Service = require('service');
describe('Service', function() {
it('getData', function() {
// throws: TypeError: Attempted to wrap undefined property …Run Code Online (Sandbox Code Playgroud) 什么是检查JavaScript对象是Map还是Set的替代/更好方法:
Object.getPrototypeOf(map) === Map.prototype
Object.getPrototypeOf(set) === Set.prototype
Run Code Online (Sandbox Code Playgroud) 我有一个带有以下控制器的AngularJS应用程序.它可以在常规JSON资源和手动更新请求上使用GET,但我无法使用Server-Sent Events.我面临的问题是,在收到SSE事件并设置/更新openListingsReport变量后,我的视图没有得到更新.我显然错过了一个非常基本的概念.请帮我解决这个问题.
var rpCtrl = angular.module('rpCtrl', ['rpSvc']);
rpCtrl.controller('rpOpenListingsCtrl', ['$scope', 'rpOpenListingsSvc',
function ($scope, rpOpenListingsSvc) {
$scope.updating = false;
if (typeof(EventSource) !== "undefined") {
// Yes! Server-sent events support!
var source = new EventSource('/listings/events');
source.onmessage = function (event) {
$scope.openListingsReport = event.data;
$scope.$apply();
console.log($scope.openListingsReport);
};
}
} else {
// Sorry! No server-sent events support..
alert('SSE not supported by browser.');
}
$scope.update = function () {
$scope.updateTime = Date.now();
$scope.updating = true;
rpOpenListingsSvc.update();
}
$scope.reset = function () {
$scope.updating = false; …Run Code Online (Sandbox Code Playgroud) 我有深层嵌套对象的数组.我想编写一个函数来从这些数组中提取任意子对象.在某些情况下,嵌套属性的值是值和对象,在其他情况下,它们是数组.
数组的示例如下:
[{parent: {level1: {level2: 'data'}}}]
[{parent: {level1: [{level2: {...}}, {level2: {...}}, {level2: {...}}]}}]
[{parent: {level1: [{level2: {level3: 'data'}}, {level2: {..}}, {level2: {..}}]}}]
Run Code Online (Sandbox Code Playgroud)
在这样的数组上调用提取函数应该会产生一个我们感兴趣的对象数组.
调用上面示例数组的函数及其结果的示例:
extractChildren(source, 'level2') = [{level2: 'data'}]
extractChildren(source, 'level2') = [{level2: {...}, level2: {...}, level2: {...}]
extractChildren(source, 'level3') = [{level3: 'data'}]
Run Code Online (Sandbox Code Playgroud)
是否有一种简洁的方法来实现这一点,lodash或者我应该使用常规JavaScript来迭代属性?
PS认为它等同于XPath select all nodes with the name "nodename"
我有以下Mongoose模型:
var mongoose = require('mongoose');
var userSchema = mongoose.Schema({
facebook: {
name: String,
email: String,
customerId: String
}
});
var User = mongoose.model('User', userSchema);
Run Code Online (Sandbox Code Playgroud)
当我使用更新本文档的一部分时 findByIdAndUpdate
User.findByIdAndUpdate(id, {
$set: {
facebook: {
name: name
}
}
});
Run Code Online (Sandbox Code Playgroud)
name得到更新,同时email并customerId得到去除(未设置?).
我没有找到这个记录.
有没有办法只更新特定的文档属性findByIdAndUpdate?
我在Windows上使用节点v0.11.14-nightly-20140819-pre并带有harmony标志.
我有JavaScript对象,其原型中定义了两个方法:
function User (args) {
this.service= new Service(args);
}
User.prototype.method2 = function (response) {
console.log(this); // <= UNDEFINED!!!!
};
User.prototype.method1 = function () {
.............
this.service.serviceMethod(args)
.then(this.method2)
.catch(onRejected);
};
function onRejected(val) {
console.log(val);
}
Run Code Online (Sandbox Code Playgroud)
serviceMethod的Service对象返回一个承诺.
当我使用User如下对象:
let user = new User(args);
user.method1();
Run Code Online (Sandbox Code Playgroud)
this在method2对象的User结束undefined时,由被称为then一旦承诺得到满足.
我尝试使用ES6和Bluebird承诺实现.
为什么this最终会undefined出现这种情况?
我有一个带有以下简单配置文件的Angular应用程序config.js:
export default function(app) {
app.constant('config', {apiUrl: 'https://localhost:8080'});
};
Run Code Online (Sandbox Code Playgroud)
由Webpack入口点导入app.js:
import config from './config';
config(app);
Run Code Online (Sandbox Code Playgroud)
apiUrl当我进行生产构建时,我想要有所不同.
在Webpack中最简单的方法是什么?
我如何修复下面的代码,以便能够使用调用类方法call.
班级定义:
class User {
constructor(..) {...}
async method(start, end) {}
}
Run Code Online (Sandbox Code Playgroud)
尝试将类方法作为函数参数传递:
const User = require('./user');
async function getData(req, res) {
// User.method is undefined, since User refers to User constructor
await get(req, res, User.method);
}
async function get(req, res, f) {
let start = ...;
let end = ...;
let params = ...;
let user = new User(params);
// f is undefined here
let stream = await f.call(user, start, end);
}
Run Code Online (Sandbox Code Playgroud) 我们在 New Relic 中观察到的情况如下。蓝色是非堆内存,橙色 - 使用 V8 堆,红色 - V8 堆空闲。
如您所见,非堆内存稳步增长,直到应用程序因内存不足而崩溃。
Chrome Node.js DevTools 似乎仅对 V8 堆内存有帮助,这似乎不会导致这里的问题。
什么是 Node.js 非堆内存?包含栈吗?什么可能导致非堆内存泄漏?我们如何诊断它们?
javascript ×7
node.js ×5
angularjs ×2
ecmascript-6 ×2
bluebird ×1
lodash ×1
mocking ×1
mongodb ×1
mongoose ×1
promise ×1
readline ×1
sinon ×1
stream ×1
unit-testing ×1
webpack ×1