gpt4 book ai didi

node.js - 在nodejs类中调用ramda compose

转载 作者:太空宇宙 更新时间:2023-11-03 23:24:24 24 4
gpt4 key购买 nike

我在我试图测试的 node js 类中有以下方法 skipLoggingThisRequest 。该方法应该根据请求中的路径返回 true 或 false,并使用 ramda compose 获取该值。但是在我的测试中,无论我在请求对象中设置什么路径,我的 skipLoggingThisRequest 始终返回 true。

我在这里缺少什么?

我的类(class):

import { compose, filter, join, toPairs, map, prop, flip, contains, test, append } from 'ramda'
import { create, env } from 'sanctuary'
import { isEmpty, flattenDeep } from 'lodash'
import chalk from 'chalk'
import log from 'menna'

class MyClass {

constructor (headerList) {
this.headerWhiteList = flattenDeep(append(headerList, []));
}

static getBody (req) {
return (!isEmpty(req.body) ? JSON.stringify(req.body) : '');
}

static S () {
return create({ checkTypes: false, env });
}

static isInList () {
return flip(contains);
}

static isInWhitelist () {
return compose(this.isInList(this.headerWhiteList), this.S.maybeToNullable, this.S.head);
}

static parseHeaders () {
return (req) => compose(join(','), map(join(':')), filter(this.isInWhitelist), toPairs, prop('headers'));
}

skipLoggingThisRequest () {
return (req) => compose(test(/^.*(swagger|docs|health).*$/), prop('path'))
}

logger (req, res, next) {
if (this.skipLoggingThisRequest(req)) {
console.log('Skipping')
return next();
}

const primaryText = chalk.inverse(`${req.ip} ${req.method} ${req.originalUrl}`);
const secondaryText = chalk.gray(`${this.parseHeaders(req)} ${this.getBody(req)}`);
log.info(`${primaryText} ${secondaryText}`);

return next();
}
}

export default MyClass

我的测试:

import sinon from 'sinon';
import MyClass from '../lib/MyClass';

describe('MyClass', () => {
const headerList = ['request-header-1', 'request-header-2'];

const request = {
'headers': {
'request-header-1': 'yabadaba',
'request-header-2': 'dooooooo'
},
'ip': 'shalalam',
'method': 'GET',
'originalUrl': 'http://myOriginalUrl.com',
'body': ''
};
const response = {};

const nextStub = sinon.stub();

describe('Logs request', () => {
const myInstance = new MyClass(headerList);
const skipLogSpy = sinon.spy(myInstance, 'skipLoggingThisRequest');
request.path = '/my/special/path';
myInstance.logger(request, response, nextStub);
sinon.assert.called(nextStub);
});
});

最佳答案

this.skipLoggingThisRequest(req) 返回一个函数 ((req) => compose(test(/^.*(swagger|docs|health).*$/), prop ('路径')))。

它不返回 bool 值。但是,由于函数是真实的,因此您的 if 语句始终会执行。

您最可能想做的是this.skipLoggingThisRequest()(req)。您获取该函数,然后对其应用请求。

演示正在发生的事情:

const testFunction = () => (test) => test === "Hello!";
console.log(testFunction);
console.log(testFunction());
console.log(testFunction()("Hello!"));
console.log(testFunction()("Goodbye!"));

if (testFunction) {
console.log("testFunction is truthy.");
}

if (testFunction()) {
console.log("testFunction() is truthy.");
}

if (testFunction()("Hello!")) {
console.log('testFunction()("Hello!") is truthy.');
}

if (!testFunction()("Goodbye!")) {
console.log('testFunction()("Goodbye!") is falsey.');
}

关于node.js - 在nodejs类中调用ramda compose,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46077080/

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