chai-luxon

一个 Chai 插件,它添加了由 Luxon 提供支持的日期和格式化日期字符串匹配器

NPM Version NPM License NPM Downloads
NPM

使用

另请参阅 测试

浏览器端

在 chai 和 luxon 之后包含 chai luxon

<script src="luxon.js"></script>
<script src="chai.js"></script>
<script src="chai-luxon.js"></script>

服务器端

让 chai 使用 chai-luxon

var chai = require('chai');
chai.use(require('chai-luxon'));

断言

将任何与 Luxon 兼容的日期/字符串/任何内容与另一个进行比较,可以选择粒度。

使用粒度时,请使用以下之一:year, month, week, day, hour, minute, second. 使用 tdd 样式断言时,如果您不使用列出的粒度之一,该参数将被解释为自定义错误消息。

sameDateTime

var dateString = '2020-04-21',
  date = new Date(2020, 3, 21),
  milliseconds = 1461222000000, // assumes system has PDT timezone
  obj = { year: 2020, month: 3, day: 21 },
  luxonDateTime = DateTime.fromISO('2020-04-21'),
  oneDayLater = DateTime.fromISO('2020-04-22');

// using should-style assertions
dateString.should.be.sameDateTime(date);
dateString.should.be.sameDateTime(oneDayLater, 'month');

// using expect-style assertions
expect(milliseconds).to.be.sameDateTime(obj);
expect(dateString).to.be.sameDateTime(oneDayLater, 'month');

// using tdd assertions
assert.sameDateTime(luxonDateTime, luxonDateTime);
assert.sameDateTime(luxonDateTime, oneDayLater, 'month');
assert.sameDateTime(luxonDateTime, oneDayLater, 'month', 'custom error message');
assert.sameDateTime(luxonDateTime, oneDayLater, 'custom error message'); // fails

beforeDateTime

var dateString = '2020-04-21',
  oneDayLater = '2020-04-22';

// using should-style assertions
dateString.should.be.beforeDateTime(oneDayLater);
dateString.should.be.beforeDateTime(oneDayLater, 'month'); // fails

// using expect-style assertions
expect(dateString).to.be.beforeDateTime(oneDayLater);
expect(dateString).to.be.beforeDateTime(oneDayLater, 'month'); // fails

// using tdd assertions
assert.beforeDateTime(luxonDateTime, oneDayLater);
assert.beforeDateTime(luxonDateTime, oneDayLater, 'month'); // fails
assert.beforeDateTime(luxonDateTime, oneDayLater, 'month', 'custom error message'); // fails
assert.beforeDateTime(luxonDateTime, oneDayLater, 'custom error message');

afterDateTime

var dateString = '2020-04-21',
  oneDayLater = '2020-04-22';

// using should-style assertions
oneDayLater.should.be.afterDateTime(luxonDateTime);
oneDayLater.should.be.afterDateTime(luxonDateTime, 'month'); // fails

// using expect-style assertions
expect(oneDayLater).to.be.afterDateTime(luxonDateTime);
expect(oneDayLater).to.be.afterDateTime(luxonDateTime, 'month'); // fails

// using tdd assertions
assert.afterDateTime(oneDayLater, luxonDateTime);
assert.afterDateTime(oneDayLater, luxonDateTime, 'month'); // fails
assert.afterDateTime(oneDayLater, luxonDateTime, 'month', 'custom error message'); // fails
assert.afterDateTime(oneDayLater, luxonDateTime, 'custom error message');

仅日期部分

该库包括用于仅比较 DateTime 值的日期部分的便捷方法。这些便捷方法是使用上述匹配器的粒度为“day”的别名。所有相同的日期格式(对象、JS Date、字符串等)都以相同的方式支持。

sameDate, beforeDate, afterdate

const date = DateTime.fromISO('2020-04-21T12:00:00Z');
const oneHourLater = date.plus({ hour: 1 });
const oneHourEarlier = date.minus({ hour: 1 });
const oneDayLater = date.plus({ day: 1 });
const oneDayEarlier = date.minus({ day: 1 });

// using should-style assertions
date.should.be.sameDate(oneHourLater);
date.should.be.beforeDate(oneHourLater); // fails
date.should.be.beforeDate(oneDayLater);
date.should.be.afterDate(oneHourEarlier); // fails
date.should.be.afterDate(oneDayLater);

// using expect-style assertions
expect(date).to.be.sameDate(oneHourLater);
expect(date).to.be.beforeDate(oneHourLater); // fails
expect(date).to.be.beforeDate(oneDayLater);
expect(date).to.be.afterDate(oneHourEarlier); // fails
expect(date).to.be.afterDate(oneDayLater);

// using tdd assertions
assert.sameDate(date, oneHourLater);
assert.beforeDate(date, oneDayLater);
assert.beforeDate(date, oneHourLater); // fails
assert.afterDate(oneDayLater, date);
assert.afterDate(oneDayLater, oneHourLater); // fails

局限性

字符串仅限于 ISO-8601 字符串。其他字符串日期/时间格式不能保证工作(并且可能不会)。

致谢

感谢 picardy 提供 chai-moment,这是该项目的基石。