首先,我是新手es6和jest.
我有一个Logger实例化的类winston,我想测试它.
这是我的代码:
const winston = require('winston');
const fs = require('fs');
const path = require('path');
const config = require('../config.json');
class Logger {
constructor() {
Logger.createLogDir(Logger.logDir);
this.logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
transports: [
new (winston.transports.Console)({
format: winston.format.combine(
winston.format.colorize({ all: true }),
winston.format.simple(),
),
}),
new (winston.transports.File)({
filename: path.join(Logger.logDir, '/error.log'),
level: 'error',
}),
new (winston.transports.File)({
filename: path.join(Logger.logDir, '/info.log'),
level: 'info',
}),
new (winston.transports.File)({
filename: path.join(Logger.logDir, '/combined.log'),
}),
],
});
}
static get …Run Code Online (Sandbox Code Playgroud) 我正面临一个我无法解决的问题。我想在我的类文件之间共享一些接口和类型。
我的回购是这样的:
test
-> dist/
-> src/
-> index.ts
-> .babelrc
-> .eslintrc.js
-> index.d.ts
-> package.json
-> tsconfig.json
Run Code Online (Sandbox Code Playgroud)
在index.d.ts:
declare module test { // I've try with 'declare namespace test' but the result is the same
export interface LabeledValue {
label: string;
size: number;
}
}
Run Code Online (Sandbox Code Playgroud)
如果我输入LabeledValue在index.ts我有一个错误,我不明白其中的道理:
import { LabeledValue} from 'test'; // TS2307: Cannot find module 'test'.
源代码/索引.ts
import {LabeledValue} from 'test';
function printLabel(labeledObj: LabeledValue) {
console.log(labeledObj.label);
}
let myObj = {size: …Run Code Online (Sandbox Code Playgroud) 我在Ubuntu上使用bash,我在一个文件夹中有一些文件,有些文件名称中有空格,其他非文件.
我想要一个带文件名的数组.示例:[foo.txt,我是file.txt,bar.jpg等]
我的代码:
for x in "$(ls -1 test/)"; do
fileList+=($x)
done
Run Code Online (Sandbox Code Playgroud)
我得到:[foo.txt,我,上午,a,file.txt,bar.jpg等]
如果我把fileList+=("$x")我得到一个行数组[foo.txt我是file.txt bar.jpg等].
我该怎么做才能得到我想要的东西?
谢谢.
我遇到了一个问题,需要你的帮助。我需要在名为 rudder 的软件中使用自动注册服务器的 API。为了达到我的目标,我必须使用 2 个 http 请求:
我的第一个请求得到了完美处理,但我不知道如何实现第二个请求。
这是我的代码(/src/controllers/registration.js):
async function index(req, res) {
// Check parameter
if (!req.body.hostname) {
return res.status(422).json({ result: 'error', message: 'Missing hostname parameter' });
}
try {
const pendingNodes = await axios.get(`${config.rudderURL}/rudder/api/latest/nodes/pending?include=minimal`, { headers });
Object.keys(pendingNodes.data.data.nodes).forEach((key) => {
// Search in all pending machine if our node is present
const node = pendingNodes.data.data.nodes[key];
if (req.body.hostname === node.hostname) {
// Registration
const response = async axios.get(`${config.rudderURL}/rudder/api/nodes/pending/${node.id}`, { headers });
// console.log(response);
return …Run Code Online (Sandbox Code Playgroud) node.js ×2
arrays ×1
async-await ×1
axios ×1
bash ×1
eslint ×1
fs ×1
javascript ×1
jestjs ×1
loops ×1
space ×1
typescript ×1
unit-testing ×1
winston ×1