Skip to content

Commit

Permalink
test: add tests and integrate with CI
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Jan 9, 2025
1 parent e2646c5 commit 114f3ea
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 2 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/test-and-release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Test and Release

on: push

env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
JSR_TOKEN: ${{ secrets.JSR_TOKEN }}

permissions:
contents: write
issues: write
id-token: write

jobs:
tests:
runs-on: ubuntu-24.04

steps:
- uses: actions/checkout@v4

- uses: actions/setup-node@v4
with:
node-version: "22.x"
cache: "npm"

- name: Install dependencies
run: npm ci --no-audit

- name: Test
run: npm test

- name: Semantic release
continue-on-error: true
run: |
npm install -D @sebbo2002/semantic-release-jsr
npx semantic-release
15 changes: 13 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"type": "module",
"main": "waitForIt.ts",
"scripts": {
"test": "npm test"
"test": "node --test --experimental-transform-types ./*.test.ts"
},
"dependencies": {
"backoff": "2.5.0"
Expand All @@ -25,5 +25,16 @@
"@types/backoff": "2.5.5",
"prettier": "3.4.2"
},
"prettier": "@bifravst/prettier-config"
"prettier": "@bifravst/prettier-config",
"release": {
"branches": [
"main"
],
"remoteTags": true,
"plugins": [
"@semantic-release/commit-analyzer",
"@semantic-release/release-notes-generator",
"@sebbo2002/semantic-release-jsr"
]
}
}
47 changes: 47 additions & 0 deletions waitForIt.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import assert from 'node:assert'
import { describe, it, mock } from 'node:test'
import { waitForIt } from './waitForIt.ts'

describe('waitForIt()', () => {
it('returns the result of a successful promise', async () => {
const expectedResult = 'success'
const succeeding = mock.fn(() => Promise.resolve(expectedResult))

const result = await waitForIt(succeeding)

assert.strictEqual(result, expectedResult)
assert.equal(succeeding.mock.calls.length, 1)
})

it('returns the error of a failing promise', async () => {
const expectedTries = 2
const expectedError = new Error('failure')
const failing = mock.fn(() => Promise.reject(expectedError))

try {
await waitForIt(failing, expectedTries)
assert.fail('Expected waitForIt to throw an error')
} catch (error) {
assert.strictEqual(error, expectedError)
}
assert.equal(failing.mock.calls.length, expectedTries)
})

it('returns the result after failing twice and succeeding on the third try', async () => {
const expectedResult = 'success'
const expectedTries = 3
let callCount = 0
const fn = mock.fn(() => {
callCount++
if (callCount < expectedTries) {
return Promise.reject(new Error('failure'))
}
return Promise.resolve(expectedResult)
})

const result = await waitForIt(fn, expectedTries)

assert.strictEqual(result, expectedResult)
assert.equal(fn.mock.calls.length, expectedTries)
})
})

0 comments on commit 114f3ea

Please sign in to comment.