测试模块根据测试环境导致不同的输出

时间:2018-06-03 11:59:09

标签: javascript unit-testing jest

测试介绍

我已经获得了一个可以使用的模块,因此它通过了附加的Jest测试。目前,我坚持以下测试规则:

describe(`TWO TIMES the SAME product ADDED`, () => {
  const _VATRate = 20;
  const _product = {
    name: 'product1',
    price: 1.50,
    quantity: 1
  };
  let _instance;
  let _cart;

  beforeEach(() => {
    _instance = window[NAMESPACE][MODULE_NAME]();
    _instance.init(_VATRate);

    _cart = _instance.addProducts([_product, _product]);
  });

  test(`cart's products`, () => {
    const result = Object.assign({}, _product, {'quantity': 2});
    expect(_cart.products).toEqual([result]);
  });
})

期望值为: [{"name": "product1", "price": 1.5, "quantity": 2}]


在浏览器中实施模块(正常)



function CartModule() {
  var _Cart = {
    total: {
      beforeVAT: 0,
      afterVAT: 0,
      VAT: 0
    },
    products: [],
    VATRate: null
  };

  var _getCart = function() {
    return {
      total: _Cart.total,
      products: _Cart.products
    };
  };

  var _updateTotalPrice = function() {
    let invoice = _Cart.total;

    invoice.beforeVAT = 0;
    invoice.afterVAT = 0;
    invoice.VAT = 0;
    let roundDecimals = number => +number.toFixed(2);

    _Cart.products.forEach(product => {
      invoice.beforeVAT = roundDecimals(
        product.price * product.quantity + invoice.beforeVAT
      );
    });

    invoice.VAT = roundDecimals(_Cart.total.beforeVAT / 100 * _Cart.VATRate);
    invoice.afterVAT = invoice.VAT + invoice.beforeVAT;
  };

  return {
    init: function(VATRate) {
      return (_Cart.VATRate = VATRate || 0);
    },

    getCart: _getCart,

    addProducts: function(recievedProducts) {
      let products = Array.from(arguments),
        updatedProduct,
        cartProducts = _getCart().products,
        existingProduct;

      products.forEach(product => {
        existingProduct = cartProducts.findIndex(
          existing => existing.name === product.name
        );

        if (existingProduct >= 0) {
          updatedProduct = cartProducts[existingProduct];
          updatedProduct.quantity++;
          cartProducts[existingProduct] = updatedProduct;
        } else {
          updatedProduct = product;
          updatedProduct.quantity = 1;
          cartProducts.push(product);
        }
      });

      // Update Total Price
      _updateTotalPrice();
      return _getCart();
    },

    changeProductQuantity: function(product, newQuantity) {
      let products = _Cart.products,
        productIndex = products.findIndex(
          existing => existing.name === product.name
        );

      products[productIndex].quantity = +newQuantity;
      _updateTotalPrice();

      return _getCart();
    },

    removeProducts: function(product) {
      let products = _Cart.products,
        productIndex = products.findIndex(
          existing => existing.name === product.name
        );

      products.splice(productIndex, 1);
      _updateTotalPrice();

      return _getCart();
    },

    destroy: function() {
      /* Module = null */
    }
  };
}

var shoppingCart = CartModule(),
    _product = {
      name: 'product1',
      price: 1.50,
      quantity: 1
    };


shoppingCart.init(20);
shoppingCart.addProducts(_product, _product)

console.log(shoppingCart.getCart().products)




Jest Testing
但是在测试环境中情况有所不同。此外,sandboxlocal版本的错误输出不同:

本地版

Expected value to equal:
      [{"name": "product1", "price": 1.5, "quantity": 2}]
    Received:
      [{"name": "product1", "price": 1.5, "quantity": 1}, {"0": {"name": "product1", "price": 1.5, "quantity": 1}, "1": {"name": "product1", "price": 1.5, "quantity": 1}, "quantity": 1}]

沙箱版本输出为:

Expected value to equal:
  [{"name": "product1", "price": 1.5, "quantity": 2}]
Received:
  [{"0": {"name": "product1", "price": 1.5, "quantity": 1}, "1": {"name": "product1", "price": 1.5, "quantity": 1}, "quantity": 1}]


测试环境
沙箱工作环境
enter image description here enter image description here

Edit zx9wy9l9qm

P.S :如果您能够查看并解释我此时做错了什么,或者也可能出错,我会非常感激吗?

1 个答案:

答案 0 :(得分:1)

我认为就是这条线:

data class Task(
    @ColumnInfo(name = "completed_flag") var completed: Boolean = false,
    @ColumnInfo(name = "task_description") var description: String?,
    @ColumnInfo(name = "category_id") var categoryId: Long? = null
) : Parcelable

由于这一行,这将生成一系列产品数组:

let products = Array.from(arguments),

你可能想要使用这样的东西:

_cart = _instance.addProducts([_product, _product]);

如果您的addProducts: function(recievedProducts) { let products = recievedProducts, 可以将对象同时作为数组处理,那么您可能希望这样做:

addProducts
相关问题