Skip to content

Commit

Permalink
Adapt the debug and test scripts for vscode-jest-runner to be compa…
Browse files Browse the repository at this point in the history
…tible with Vitest. (#1923)

* fixed vscode test runner for debugging tests

* added comment

* Apply suggestions from code review

Co-authored-by: Peter Sutter <[email protected]>

* Update test.cjs

---------

Co-authored-by: Peter Sutter <[email protected]>
  • Loading branch information
holgerkoser and petersutter authored Jun 18, 2024
1 parent 3b75885 commit 87cb207
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 78 deletions.
7 changes: 4 additions & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@
"**/coverage": true,
"**/dist": true
},
"jestrunner.enableYarnPnpSupport": true,
"jestrunner.jestCommand": "node scripts/jest.cjs",
"jestrunner.yarnPnpCommand": "../../scripts/yarn.cjs",
"jestrunner.changeDirectoryToWorkspaceRoot": true,
"jestrunner.jestCommand": "node scripts/test.cjs",
"jestrunner.enableYarnPnpSupport": true,
// The command is prefixed with `.yarn/releases/`, so we need `../..` (see https://github.com/firsttris/vscode-jest-runner/blob/4dea33a0f692a8f1519dbb8b2567787ffe1843da/src/jestRunner.ts#L180)
"jestrunner.yarnPnpCommand": "../../scripts/debug.cjs"
}
29 changes: 29 additions & 0 deletions scripts/debug.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env node
//
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
//

const assert = require('assert').strict
const { spawn } = require('child_process')
const { findYarnWorkspace } = require('./helper.cjs')

const args = process.argv.slice(3)
const workspace = findYarnWorkspace(args[0])
assert(workspace, 'yarn workspace not found')

if (workspace.name === '@gardener-dashboard/frontend') {
const index = args.indexOf('--runInBand')
if (index !== -1) {
args.splice(index, 1)
}
}
args.push('--maxConcurrency=1', '--testTimeout=0')

const cmd = spawn('yarn', ['workspace', workspace.name, 'run', 'test', ...args], {
stdio: 'inherit',
env: process.env,
})
cmd.once('error', err => process.stderr.write(err.message + '\n'))
cmd.once('exit', code => process.exit(code))
31 changes: 3 additions & 28 deletions scripts/helper.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const packageJson = require('../package.json')
const repodir = path.dirname(__dirname)

function getWorkspaces () {
const today = new Date().toISOString().substring(0, 10)
const today = new Date().toISOString().substring(0, 10)
const hash = crypto.createHash('md5').update(JSON.stringify(packageJson.workspaces)).digest('hex')
const filename = path.join(os.tmpdir(), `gardener-dashboard-${today}`, `workspaces.${hash}.json`)

Expand All @@ -25,14 +25,14 @@ function getWorkspaces () {
} catch (err) {
if (err.code !== 'ENOENT') {
throw err
}
}
}

const workspaces = execSync('yarn workspaces list --json')
.toString('utf8')
.split('\n')
.filter(val => val.startsWith('{') && val.endsWith('}') )
.map(val => JSON.parse(val))
.map(val => JSON.parse(val))

fs.mkdirSync(path.dirname(filename), { recursive: true })
fs.writeFileSync(filename, JSON.stringify(workspaces), 'utf8')
Expand All @@ -47,31 +47,6 @@ function findYarnWorkspace (filename) {
return workspaces.find(({ location }) => testfileLocation.startsWith(location))
}

function findJestConfig (workspace) {
const workspacedir = path.join(repodir, workspace.location)
const configfile = fs.readdirSync(workspacedir).find(name => {
if (name === 'jest.config.js' || name == 'jest.config.json') {
return true
}
if (name === 'package.json') {
const jest = require(path.join(workspacedir, 'package.json'))
return !!jest
}
return false
})
return path.join(workspacedir, configfile)
}

function findYarnCommand () {
const yarndir = path.join(repodir, '.yarn', 'releases')
const filename = fs.readdirSync(yarndir).find(filename => /^yarn-.+\.c?js$/.test(filename))
if (filename) {
return path.join(yarndir, filename)
}
}

module.exports = {
findYarnWorkspace,
findYarnCommand,
findJestConfig,
}
26 changes: 0 additions & 26 deletions scripts/jest.cjs

This file was deleted.

20 changes: 20 additions & 0 deletions scripts/test.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env node
//
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
//

const assert = require('assert').strict
const { spawn } = require('child_process')
const { findYarnWorkspace } = require('./helper.cjs')

const args = process.argv.slice(2)
const workspace = findYarnWorkspace(args[0])
assert(workspace, 'yarn workspace not found')

const cmd = spawn('yarn', ['workspace', workspace.name, 'run', 'test', ...args], {
stdio: 'inherit'
})
cmd.once('error', err => process.stderr.write(err.message + '\n'))
cmd.once('exit', code => process.exit(code))
21 changes: 0 additions & 21 deletions scripts/yarn.cjs

This file was deleted.

0 comments on commit 87cb207

Please sign in to comment.