Javascript 使用 let 时出现问题
Javascript issue when using let
我有以下 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' }));
});
});
});
请注意,在 'When the error is a SyntaxError...' 的描述块中,res
、next
和 clonedRes
前面有 let
。
如果没有这些前面的 let
,我的测试就会失败。我不明白为什么我需要再次为这些添加 let
,而不是为同一块中的 err
和 req
添加。谁能帮我解释一下?
在严格模式下(以及通常在适当的 linted 代码中),变量必须在赋值之前声明。此外,const
和 let
变量必须在一个块中声明 一次 ,不能再声明了。重新声明已经声明的 err
(或任何其他变量)将引发错误,这就是为什么您应该在 describe('errorHandler'
函数中只看到一次 let <varname>
:
const describe = cb => cb();
let something;
describe(() => {
something = 'foo';
});
let something;
describe(() => {
something = 'bar';
});
describe
describe('errorHandler'
内的 describe('errorHandler'
已经具有对 err
.
的范围访问权限
根本不先声明一个变量,以草率的方式分配它会导致它被分配给全局对象,这几乎总是不可取的 。例如:
// Accidentally implicitly referencing window.status, which can only be a string:
status = false;
if (status) {
console.log('status is actually truthy!');
}
也就是说,尽可能缩小变量范围通常是个好主意 - 仅当您需要外部范围内的值时才分配给外部变量。考虑仅在分配给它们的 describe
内部声明变量,这有一个额外的好处,即允许您使用 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
我有以下 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' }));
});
});
});
请注意,在 'When the error is a SyntaxError...' 的描述块中,res
、next
和 clonedRes
前面有 let
。
如果没有这些前面的 let
,我的测试就会失败。我不明白为什么我需要再次为这些添加 let
,而不是为同一块中的 err
和 req
添加。谁能帮我解释一下?
在严格模式下(以及通常在适当的 linted 代码中),变量必须在赋值之前声明。此外,const
和 let
变量必须在一个块中声明 一次 ,不能再声明了。重新声明已经声明的 err
(或任何其他变量)将引发错误,这就是为什么您应该在 describe('errorHandler'
函数中只看到一次 let <varname>
:
const describe = cb => cb();
let something;
describe(() => {
something = 'foo';
});
let something;
describe(() => {
something = 'bar';
});
describe
describe('errorHandler'
内的 describe('errorHandler'
已经具有对 err
.
根本不先声明一个变量,以草率的方式分配它会导致它被分配给全局对象,这几乎总是不可取的
// Accidentally implicitly referencing window.status, which can only be a string:
status = false;
if (status) {
console.log('status is actually truthy!');
}
也就是说,尽可能缩小变量范围通常是个好主意 - 仅当您需要外部范围内的值时才分配给外部变量。考虑仅在分配给它们的 describe
内部声明变量,这有一个额外的好处,即允许您使用 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