Skip to content

Commit

Permalink
Add test runner script
Browse files Browse the repository at this point in the history
  • Loading branch information
daogrady committed Jan 30, 2025
1 parent e7395d4 commit 847186e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
"author": "SAP SE (https://www.sap.com)",
"license": "Apache-2.0",
"scripts": {
"test:unit": "node --import ./test/unit/setup.mjs --test ./test/unit/*.test.js",
"test:integration": "node --import ./test/integration/setup.mjs --test ./test/int/*.test.js",
"test:smoke": "node --import ./test/smoke/setup.mjs --test ./test/smoke/*.test.js",
"test:unit": "node test/testRunner.js ./test/unit ./test/unit/setup.mjs",
"test:integration": "node test/testRunner.js ./test/integration ./test/integration/setup.mjs",
"test:smoke": "node test/testRunner.js ./test/smoke ./test/smoke/setup.mjs",
"test:all": "npm run test:smoke && npm run test:unit",
"test": "npm run test:smoke && npm run test:unit",
"lint": "npx eslint .",
Expand Down
29 changes: 29 additions & 0 deletions test/testRunner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// TODO: this script wraps the `node --test` command until our lower bound for our test matrix is >=21 (30 Apr 2026).
// The native test runner with multiple suites requires using a glob pattern to specify the relevant test files.
// But glob is only available in Node 21 onwards. So to avoid errors where the runtime assumes it has to look for a file called "*.test.js",
// we wrap the call to node with an explicit glob expansion. Once Node22 is established as lower bound, we can have
//
// "test:unit": "node --import ./test/unit/setup.mjs --test './test/unit/*.test.js'",
// "test:integration": "node --import ./test/integration/setup.mjs --test './test/integration/*.test.js'",
// "test:smoke": "node --import ./test/smoke/setup.mjs --test './test/smoke/*.test.js'",
//
// again.
const { execSync } = require('child_process')
const path = require('path')
const fs = require('fs')

const testDir = process.argv[2]
const setupFile = process.argv[3]

const testFiles = fs.readdirSync(testDir)
.filter(file => file.endsWith('.test.js'))
.map(file => path.join(testDir, file))

if (testFiles.length === 0) {
// eslint-disable-next-line no-console
console.error(`No test files found in ${testDir}`)
process.exit(1)
}

const command = `node --import ${setupFile} --test ${testFiles.join(' ')}`
execSync(command, { stdio: 'inherit' })

Check warning

Code scanning / CodeQL

Shell command built from environment values Medium test

This shell command depends on an uncontrolled
file name
.

0 comments on commit 847186e

Please sign in to comment.