使用let时出现Javascript问题

问题描述 投票:0回答:1

我有以下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' }));
    });
  });
});

请注意,我在描述块中的letresnext前面有clonedRes'当错误是一个SyntaxError ...'时。

如果没有let,我会在测试中失败。我不明白为什么我需要再次添加let,但不是同一块中的errreq。任何人都可以帮我解释一下吗?

javascript unit-testing let
1个回答
2
投票

在严格模式下(一般来说是体面的代码),必须在赋值变量之前声明变量。此外,constlet变量必须在一个块中声明一次,而不是更多。重新声明已经声明的err(或任何其他变量)将引发错误,这就是为什么你应该在let <varname>函数中只看到describe('errorHandler'一次:

const describe = cb => cb();

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

describe内部的describe('errorHandler's已经进入了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!');
}

也就是说,保持变量范围尽可能狭窄通常是一个好主意 - 只有在需要外部范围内的值时才分配给外部变量。考虑仅在分配给它们的describes中声明变量,这还有额外的好处,允许你使用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
© www.soinside.com 2019 - 2024. All rights reserved.