gpt4 book ai didi

使用let时的Javascript问题

转载 作者:行者123 更新时间:2023-12-01 01:13:26 25 4
gpt4 key购买 nike

我有以下 js 用于对错误处理程序进行单元测试:

import assert from 'assert';
import deepClone from 'lodash.clonedeep';
import deepEqual from 'lodash.isequal';
import { spy } from 'sinon';
import errorHandler from './index';

function getValidError(constructor = SyntaxError) {
let error = new constructor();
error.status = 400;
error.body = {};
error.type = 'entity.parse.failed';
return error;
}

describe('errorHandler', function() {
let err;
let req;
let res;
let next;
let clonedRes;
describe('When the error is not an instance of SyntaxError', function() {
err = getValidError(Error);
req = {};
res = {};
next = spy();
clonedRes = deepClone(res);
errorHandler(err, req, res, next);

it('should not modify res', function() {
assert(deepEqual(res, clonedRes));
});

it('should call next()', function() {
assert(next.calledOnce);
});
});

...(#other test cases all similar to the first)

describe('When the error is a SyntaxError, with a 400 status, has a `body` property set, and has type `entity.parse.failed`', function() {
err = getValidError();
req = {};
let res = {
status: spy(),
set: spy(),
json: spy()
};
let next = spy();
errorHandler(err, req, res, next);

it('should set res with a 400 status code', function() {
assert(res.status.calledOnce);
assert(res.status.calledWithExactly(400));
});

it('should set res with an application/json content-type header', function() {
assert(res.set.calledOnce);
assert(res.set.calledWithExactly('Content-Type', 'application/json'));
});

it('should set res.json with error code', function() {
assert(res.json.calledOnce);
assert(res.json.calledWithExactly({ message: 'Payload should be in JSON format' }));
});
});
});

请注意,我在 ' 的描述 block 中的 resnextclonedRes 前面有 let当错误是 SyntaxError...'.

如果前面没有 let ,我的测试就会失败。我不明白为什么我需要再次为这些添加 let ,而不是为同一 block 中的 errreq 。谁能帮我解释一下吗?

最佳答案

在严格模式下(通常在正确的 linted 代码中),必须在分配变量之前对其进行声明。另外,constlet变量必须在 block 中声明一次,不能再声明更多。重新声明err (或任何其他变量)已经声明的将引发错误,这就是为什么您应该看到 let <varname>仅在您的 describe('errorHandler' 中出现过一次功能:

const describe = cb => cb();

let something;
describe(() => {
something = 'foo';
});
let something;
describe(() => {
something = 'bar';
});

进一步describe s 内部 describe('errorHandler'已经具有对 err 的访问权限.

根本不先声明变量,以草率的方式分配给它会导致它被分配给全局对象,这几乎总是不可取的,并且 can introduce bugs and errors 。例如:

// Accidentally implicitly referencing window.status, which can only be a string:

status = false;
if (status) {
console.log('status is actually truthy!');
}

也就是说,将变量的作用域尽可能缩小通常是一个好主意 - 仅当您需要外部作用域中的值时才分配给外部变量。考虑仅在 describe 内部声明变量分配给它们的 s,它还有一个额外的好处,允许您使用 const而不是let :

describe('When the error is not an instance of SyntaxError', function() {
const err = getValidError(Error);
const req = {};
const res = {};
const next = spy();
const clonedRes = deepClone(res);
errorHandler(err, req, res, next);
// etc
});
// etc
describe('When the error is a SyntaxError, with a 400 status, has a `body` property set, and has type `entity.parse.failed`', function() {
const err = getValidError();
const req = {};
const res = {
status: spy(),
set: spy(),
json: spy()
};
const next = spy();
// etc

关于使用let时的Javascript问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54975647/

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