java中的jset框架,使用Jest测试框架在不导出的情况下测试JavaScript

我正在将旧的JS代码库移动到现代JS . 第一步是向当前代码库添加一些测试 . 当前的代码库基本上是一堆单独的JS文件,每个文件都包含在IIFE中 .

这本身就是测试的一个问题,因为除非某些东西暴露在全局对象中,否则你无法进入IIFE . 我重构的一些代码是带有属性的简单JS对象,它附加到全局对象上的命名空间(下面的 namespace 只是一个占位符名称),例如:

var namespace = window.namespace || {};

var paymentsHandlerUtils = {

getNewValue: function(selectedAmount) {

'use strict';

return selectedAmount < 1 || isNaN(selectedAmount)

? ''

: '$' + selectedAmount;

},

getSelectedAmount: function(value) {

'use strict';

return value % 1 === 0 ? parseInt(value) : parseFloat(value).toFixed(2);

}

};

namespace.paymentsHandlerUtils = paymentsHandlerUtils;

我的问题是,您将如何使用Jest进行测试?我已经尝试过如下要求:

const paymentsHandlerUtils = require('../js/components/payments/payments-handler-utils.js');

这会运行,但 paymentsHandlerUtils 对象只是一个空的 {} . 不足为奇,因为只需执行JS就无法返回任何内容 . 但是, window.namespace 也未定义 . 似乎代码没有在jsDOM的上下文中执行,因此不会创建全局 .

有没有办法让这个工作,或者这不是Jest的用例?提前致谢 .

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用NOAA日出日落算法计算日出和日落时间的示例代码: ```python import datetime import math def calculate_julian_day(year, month, day): if month <= 2: year -= 1 month += 12 A = math.floor(year / 100) B = 2 - A + math.floor(A / 4) julian_day = math.floor(365.25 * (year + 4716)) + math.floor(30.6001 * (month + 1)) + day + B - 1524.5 return julian_day def calculate_sunrise_sunset(latitude, longitude, date): julian_day = calculate_julian_day(date.year, date.month, date.day) n = julian_day - 2451545.0 + 0.0008 Jc = n / 36525.0 Jd = 2451545.0 + 0.0009 + Jc M = (357.5291 + 0.98560028 * (Jd - 2451545.0)) % 360 L = (280.46646 + 0.98564736 * (Jd - 2451545.0)) % 360 ec = 0.016708634 - 0.000042037 * Jc C = (1.914602 - 0.004817 * Jc - 0.000014 * Jc**2) * math.sin(math.radians(M)) + (0.019993 - 0.000101 * Jc) * math.sin(math.radians(2 * M)) + 0.000289 * math.sin(math.radians(3 * M)) true_longitude = L + C omega = 125.04 - 1934.136 * Jc lambda_sun = true_longitude - 0.00569 - 0.00478 * math.sin(math.radians(omega)) epsilon = 23.439291 - 0.0130042 * Jc sin_alpha = math.cos(math.radians(epsilon)) * math.sin(math.radians(lambda_sun)) cos_alpha = math.sqrt(1 - sin_alpha**2) alpha = math.degrees(math.atan2(sin_alpha, cos_alpha)) hour_angle = math.degrees(math.acos((math.sin(math.radians(-0.83)) - math.sin(math.radians(latitude)) * math.sin(math.radians(alpha))) / (math.cos(math.radians(latitude)) * math.cos(math.radians(alpha))))) Jtransit = 2451545.0 + 0.0009 + ((hour_angle + longitude) / 360.0) + n delta_Jtransit = Jtransit - math.floor(Jtransit) Jset = Jtransit + (0.0053 * math.sin(math.radians(omega))) - (0.0069 * math.sin(2 * math.radians(lambda_sun))) delta_Jset = Jset - math.floor(Jset) Jrise = Jtransit - delta_Jtransit Jrise_next = Jrise + 1.0 sunrise_time = datetime.datetime.utcfromtimestamp((Jrise - 2451545.0) * 86400.0) sunset_time = datetime.datetime.utcfromtimestamp((Jset - 2451545.0) * 86400.0) return sunrise_time, sunset_time # 示例使用 latitude = 37.7749 # 纬度 longitude = -122.4194 # 经度 date = datetime.datetime(2022, 1, 1) # 日期 sunrise, sunset = calculate_sunrise_sunset(latitude, longitude, date) print("日出时间:", sunrise) print("日落时间:", sunset) ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值