Update .gitea/workflows/nodejs-test.yml
Some checks failed
CI Demo / build (push) Successful in 3s
Docker Test / docker-ops (push) Successful in 5s
.NET Test / dotnet-test (push) Successful in 46s
Go Test / go-test (push) Successful in 19s
Julia Test / julia-test (push) Has been cancelled
Kotlin Test / kotlin-hello (push) Has been cancelled
Node.js Test / nodejs-test (push) Has been cancelled
Notebook Test / nb-test (push) Has been cancelled
PHP Test / php-test (push) Has been cancelled
Python Test / python-hello (push) Has been cancelled
R Test / r-cmdcheck (push) Has been cancelled
Rust Test / rust-test (push) Has been cancelled
Java Test / java-hello (push) Has been cancelled
Scala Test / scala-test (push) Has been cancelled
Swift Test / swift-hello (push) Has been cancelled

This commit is contained in:
2025-08-07 20:56:35 +00:00
parent feba76125c
commit da29bc2261

View File

@@ -4,22 +4,31 @@ on: [push]
jobs: jobs:
nodejs-test: nodejs-test:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- name: Checkout code - name: Checkout code
uses: actions/checkout@v4 uses: actions/checkout@v4
- name: Set up Node.js # Use caching only when a lockfile exists (avoids setup-node error)
- name: Set up Node.js (with cache)
if: ${{ hashFiles('**/package-lock.json','**/npm-shrinkwrap.json','**/yarn.lock') != '' }}
uses: actions/setup-node@v4 uses: actions/setup-node@v4
with: with:
node-version: '20' # or '18' if you need older node-version: '20'
cache: 'npm' cache: 'npm'
- name: Set up Node.js (no cache)
if: ${{ hashFiles('**/package-lock.json','**/npm-shrinkwrap.json','**/yarn.lock') == '' }}
uses: actions/setup-node@v4
with:
node-version: '20'
- name: Verify Node & npm - name: Verify Node & npm
run: | run: |
node -v node -v
npm -v npm -v
- name: Detect & install deps (lockfile-aware) + run tests - name: Install deps and run tests (or scaffold a smoke test)
shell: bash shell: bash
run: | run: |
set -euo pipefail set -euo pipefail
@@ -27,43 +36,40 @@ jobs:
if [ -f package.json ]; then if [ -f package.json ]; then
echo "package.json found." echo "package.json found."
if [ -f package-lock.json ]; then if [ -f package-lock.json ] || [ -f npm-shrinkwrap.json ]; then
echo "Using npm ci (lockfile present)." echo "Lockfile present -> npm ci"
npm ci npm ci
else else
echo "No lockfile; using npm install." echo "No lockfile -> npm install"
npm install npm install
fi fi
# Run tests only if there's a real test script (not the default placeholder) # Run tests only if there's a meaningful test script (not the default)
if node -e "const p=require('./package.json'); const s=p.scripts&&p.scripts.test; process.exit(!(s && !/no test specified/.test(String(s))))"; then if node -e "const p=require('./package.json'); const s=p.scripts&&p.scripts.test; process.exit(!(s && !/no test specified/i.test(String(s))));"; then
npm test npm test
else else
echo "No meaningful test script found; skipping tests." echo "No meaningful test script found; skipping tests."
fi fi
else else
echo "No Node project detected. Creating a tiny smoke-test project..." echo "No Node project detected -> scaffold a tiny Mocha test"
npm init -y >/dev/null 2>&1 npm init -y >/dev/null 2>&1
npm install --save-dev mocha >/dev/null 2>&1 npm install --save-dev mocha >/dev/null 2>&1
npm pkg set scripts.test="mocha" >/dev/null 2>&1 npm pkg set scripts.test="mocha" >/dev/null 2>&1
# Write minimal app and test (note: heredoc terminators aligned under run: |) printf '%s\n' \
cat > sum.js <<'EOF' 'function add(a, b) { return a + b; }' \
function add(a, b) { return a + b; } 'module.exports = { add };' > sum.js
module.exports = { add };
EOF
mkdir -p test mkdir -p test
cat > test/sum.test.js <<'EOF' printf '%s\n' \
const assert = require('assert'); "const assert = require('assert');" \
const { add } = require('../sum'); "const { add } = require('../sum');" \
describe('add', () => { "describe('add', () => {" \
it('adds numbers', () => { " it('adds numbers', () => {" \
assert.strictEqual(add(2, 2), 4); " assert.strictEqual(add(2, 2), 4);" \
}); " });" \
}); "});" > test/sum.test.js
EOF
npm test npm test
fi fi