Skip to content

Commit

Permalink
feat: add cross-runtime testing
Browse files Browse the repository at this point in the history
  • Loading branch information
frytg committed Nov 27, 2024
1 parent 4644463 commit 165f31e
Show file tree
Hide file tree
Showing 6 changed files with 264 additions and 9 deletions.
85 changes: 80 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ permissions:
contents: read

jobs:
test-deno:
test-on-deno-and-lint:
runs-on: ${{ matrix.os }}
timeout-minutes: 5
strategy:
Expand All @@ -27,7 +27,6 @@ jobs:
- ubuntu-latest
- windows-latest
- macOS-latest

steps:
- name: Set line ending for Windows
if: matrix.os == 'windows-latest'
Expand All @@ -42,14 +41,90 @@ jobs:
deno-version: canary

- name: Install dependencies
run: deno install
run: deno install --vendor

- name: Format & Check
run: deno run check

# - name: Test
# run: deno task test
- name: Test
run: deno test

- name: Publish dry run
run: deno publish --dry-run
if: matrix.os == 'ubuntu-latest'

test-on-bun:
runs-on: ${{ matrix.os }}
timeout-minutes: 5
strategy:
fail-fast: false
matrix:
os:
- ubuntu-latest
- windows-latest
- macOS-latest
steps:
- name: Set line ending for Windows
if: matrix.os == 'windows-latest'
run: git config --global core.autocrlf false

- name: Clone repository
uses: actions/checkout@v4

- name: Setup Bun
uses: oven-sh/setup-bun@v2
with:
bun-version: latest

- name: Bun version
run: bun --version

- name: Install JSR dependencies
run: bunx jsr add @cross/test @std/assert

- name: Install NPM dependencies
run: bun add luxon

- name: Test
run: bun test

test-on-node:
runs-on: ${{ matrix.os }}
timeout-minutes: 5
strategy:
fail-fast: false
matrix:
node-version:
- 20
- 22
- 23
os:
- ubuntu-latest
- windows-latest
- macOS-latest
steps:
- name: Set line ending for Windows
if: matrix.os == 'windows-latest'
run: git config --global core.autocrlf false

- name: Clone repository
uses: actions/checkout@v4

- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}

- name: Install JSR dependencies
run: npx jsr add @cross/test @std/assert

- name: Install NPM dependencies
run: npm install luxon

- name: Node version
run: node --version

- name: Set up package.json
run: 'echo ''{ "type": "module" }'' > package.json'

- name: Test
run: npx --yes tsx --test **/*.test.ts
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.DS_Store
coverage/

# config
keys/*
.env

# Dependency directories
node_modules/

tmp
101 changes: 101 additions & 0 deletions dates/dates.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { test } from '@cross/test'
import { assert, assertEquals } from '@std/assert'
import {
DateTime,
getDateHourMinutes,
getFullRelativeTime,
getISO,
getIso,
getMs,
getMsOffset,
getNow,
getRelative,
getUnix,
getYearMonthDay,
msToUnix,
parseISO,
parseIso,
toLocal,
} from './dates.ts'

test('toLocal() converts date to Amsterdam timezone', () => {
const utcDate = DateTime.fromISO('2024-03-15T12:00:00Z')
const localDate = toLocal(utcDate)
assertEquals(localDate.zoneName, 'Europe/Amsterdam')
})

test('msToUnix() converts milliseconds to unix timestamp', () => {
const ms = 1647345600000
assertEquals(msToUnix(ms), 1647345600)
})

test('getNow() returns current DateTime', () => {
const now = getNow()
assert(now instanceof DateTime)
assert(Math.abs(now.toMillis() - Date.now()) < 1000) // Within 1 second
})

test('getUnix() returns unix timestamp', () => {
const now = DateTime.fromMillis(1647345600000) // March 15, 2024 12:00:00 UTC
assertEquals(getUnix(now), 1647345600)
})

test('getMs() returns millisecond timestamp', () => {
const now = DateTime.fromMillis(1647345600000) // March 15, 2024 12:00:00 UTC
assertEquals(getMs(now), 1647345600000)
})

test('getISO() returns ISO string in UTC', () => {
const date = DateTime.fromISO('2024-03-15T12:00:00+01:00')
assertEquals(getISO(date), '2024-03-15T11:00:00.000Z')
})

test('getIso() is an alias for getISO()', () => {
const date = DateTime.fromISO('2024-03-15T12:00:00+01:00')
assertEquals(getIso(date), getISO(date))
})

test('getMsOffset() calculates offset from previous timestamp', () => {
const now = getMs()
const earlier = now - 1000 // 1 second ago
assert(getMsOffset(earlier) >= 1000)
})

test('getRelative() returns relative time string', () => {
const now = getNow()
const yesterday = now.minus({ days: 1 })
assertEquals(getRelative(yesterday), '1 day ago')
})

test('getYearMonthDay() formats date correctly', () => {
const date = DateTime.fromISO('2024-03-15T12:00:00Z')
assertEquals(getYearMonthDay(date), 20240315)
assertEquals(getYearMonthDay(date, true), '2024-03-15')
})

test('parseISO() parses ISO string correctly', () => {
const isoString = '2024-03-15T12:00:00.000Z'
const parsed = parseISO(isoString)
assert(parsed instanceof DateTime)
assertEquals(parsed.toUTC().toISO(), isoString)
})

test('parseIso() is an alias for parseISO()', () => {
const isoString = '2024-03-15T12:00:00.000Z'
assertEquals(parseIso(isoString).toISO(), parseISO(isoString).toISO())
})

test('getDateHourMinutes() formats date in human readable way', () => {
const date = DateTime.fromISO('2024-03-15T12:00:00Z')
const formatted = getDateHourMinutes(date)
assert(formatted.includes('Mar'))
assert(formatted.includes('2024'))
})

test('getFullRelativeTime() combines date and relative time', () => {
const date = DateTime.fromISO('2024-03-15T12:00:00Z')
const fullRelative = getFullRelativeTime(date)
assert(fullRelative.includes('Mar'))
assert(fullRelative.includes('2024'))
assert(fullRelative.includes('ago'))
})
6 changes: 3 additions & 3 deletions dates/dates.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// load package
import { DateTime } from 'luxon'

const localTimeZone = 'Europe/Amsterdam'
const LOCAL_TIMEZONE = 'Europe/Amsterdam'
const DATE_HOUR_MINUTES_FORMAT = 'ccc, d. LLLL yyyy - h:mma'

/**
* Luxon DateTime
Expand All @@ -20,7 +21,7 @@ export { DateTime }
* toLocal(getNow())
* ```
*/
export const toLocal = (date: DateTime): DateTime => date.setZone(localTimeZone)
export const toLocal = (date: DateTime): DateTime => date.setZone(LOCAL_TIMEZONE)

/**
* Converts ms timestamp to s
Expand Down Expand Up @@ -182,7 +183,6 @@ export const parseIso = parseISO
* getDateHourMinutes(getNow())
* ```
*/
const DATE_HOUR_MINUTES_FORMAT = 'ccc, d. LLLL yyyy - h:mma'
export const getDateHourMinutes = (date: DateTime): string =>
`${date.setLocale('en').toFormat(DATE_HOUR_MINUTES_FORMAT)}`

Expand Down
4 changes: 3 additions & 1 deletion deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
"workspace": ["./check-required-env", "./dates", "./logger"],
"imports": {
"@biomejs/biome": "npm:@biomejs/biome@^1.9.4",
"@types/node": "npm:@types/node@^22.9.0"
"@types/node": "npm:@types/node@^22.9.0",
"@cross/test": "jsr:@cross/[email protected]",
"@std/assert": "jsr:@std/assert@^0.218.2"
}
}
60 changes: 60 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 165f31e

Please sign in to comment.