Skip to content

Commit

Permalink
add workflow for unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kasumi-1 committed Dec 7, 2023
1 parent a7cb954 commit 34d82d1
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
44 changes: 44 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Tests CI

on: [push, pull_request]

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Test using Node.js
uses: actions/setup-node@v1
with:
node-version: '18'
- run: npm install
- run: npm run test:ci

- name: Tests ✅
if: ${{ success() }}
run: |
curl --request POST \
--url https://api.github.com/repos/${{ github.repository }}/statuses/${{ github.sha }} \
--header 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' \
--header 'content-type: application/json' \
--data '{
"context": "tests",
"state": "success",
"description": "Tests passed",
"target_url": "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
}'
- name: Tests 🚨
if: ${{ failure() }}
run: |
curl --request POST \
--url https://api.github.com/repos/${{ github.repository }}/statuses/${{ github.sha }} \
--header 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' \
--header 'content-type: application/json' \
--data '{
"context": "tests",
"state": "failure",
"description": "Tests failed",
"target_url": "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
}'
36 changes: 36 additions & 0 deletions github-actions-reporter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class GithubActionsReporter {
constructor(globalConfig, options) {
this._globalConfig = globalConfig
this._options = options
}

onRunComplete(contexts, results) {
results.testResults.forEach((testResultItem) => {
const testFilePath = testResultItem.testFilePath

testResultItem.testResults.forEach((result) => {
if (result.status !== 'failed') {
return
}

result.failureMessages.forEach((failureMessages) => {
const newLine = '%0A'
const message = failureMessages.replace(/\n/g, newLine)
const captureGroup = message.match(/:([0-9]+):([0-9]+)/)

if (!captureGroup) {
console.log('Unable to extract line number from call stack')
return
}

const [, line, col] = captureGroup
console.log(
`::error file=${testFilePath},line=${line},col=${col}::${message}`,
)
})
})
})
}
}

module.exports = GithubActionsReporter
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
"i18n": "i18next",
"lint": "next lint",
"test": "jest",
"test:ci": "jest --ci --reporters='./github-actions-reporter.js'",
"act": "act -j test",
"generate:paths": "npx node scripts/generate_paths.js"
},
"dependencies": {
Expand Down

0 comments on commit 34d82d1

Please sign in to comment.