gpt4 book ai didi

javascript - 根据测试环境,测试模块会产生不同的输出

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

测试简介

我已经获得了一个可以使用的模块,因此它通过了附加的 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]);
});
})

预期值为:[{"名称": "产品1", "价格": 1.5, "数量": 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 测试
但在测试环境中情况有所不同。另外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:如果您能查看并解释我此时做错了什么,或者可能还有什么问题,我将不胜感激?

最佳答案

我认为是这一行:

let products = Array.from(arguments),

由于这一行,这将产生一个产品数组的数组:

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

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

addProducts: function(recievedProducts) {
let products = recievedProducts,

如果您的 addProducts 可以将对象作为数组处理,您可能需要这样做:

addProducts: function(recievedProducts) {
let products = Array.isArray(recievedProducts) ? receivedProducts : [receivedProducts],

关于javascript - 根据测试环境,测试模块会产生不同的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50666147/

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