Skip to content

Commit

Permalink
feat(core): adds launch for debugging a single test file
Browse files Browse the repository at this point in the history
  • Loading branch information
matt committed Mar 4, 2020
1 parent 92dabd5 commit 5e19d8c
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
20 changes: 20 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"request": "launch",
"name": "Run Mocha tests",
"program": "${workspaceRoot}/packages/build/node_modules/mocha/bin/_mocha",
"runtimeArgs": ["-r", "${workspaceRoot}/packages/build/node_modules/source-map-support/register"],
"cwd": "${workspaceRoot}",
"args": [
"--config",
Expand All @@ -18,6 +19,25 @@
"0"
]
},
{
"type": "node",
"request": "launch",
"name": "Debug Current Test File",
"program": "${workspaceRoot}/bin/mocha-current-file",
"runtimeArgs": ["-r", "${workspaceRoot}/packages/build/node_modules/source-map-support/register"],
"cwd": "${workspaceRoot}",
"args": [
"--config",
"${workspaceRoot}/packages/build/config/.mocharc.json",
"-t",
"0",
"${file}"
],
// This ensures vscode won't set breakpoints until after sourcemaps
// have been loaded for the file. Important when your tests are compiled
// from typescript.
"disableOptimisticBPs": true,
},
{
"type": "node",
"request": "attach",
Expand Down
71 changes: 71 additions & 0 deletions bin/mocha-current-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/usr/bin/env node

// Copyright IBM Corp. 2020. All Rights Reserved.
// Node module: @loopback-next
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

/*
================================================================================
This is used in the launch.json to enable you to debug a test file written in
typescript. This function attempts to convert the passed typescript file to
the best-guess output javascript file.
It walks up the filesystem from the current file, stops at package.json, and
looks in `dist`
Ideally, we could somehow use the typescript compiler and tsconfig.json to get
the explicit output file instead of trying to guess it, but there might be
overhead.
Ex:
```jsonc
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Debug Current Test File",
"program": "${workspaceRoot}/bin/mocha-current-file",
"runtimeArgs": ["-r", "${workspaceRoot}/packages/build/node_modules/source-map-support/register"],
"cwd": "${workspaceRoot}",
"autoAttachChildProcesses": true,
"args": [
"--config",
"${workspaceRoot}/packages/build/config/.mocharc.json",
"-t",
"0",
"${file}"
],
"disableOptimisticBPs": true
}
]
}
```
================================================================================
*/

'use strict';
const path = require('path');
const fs = require('fs');

function findDistFile(filename) {
const absolutePath = path.resolve(filename);
let currentDir = path.dirname(absolutePath);
let isPackageRoot = fs.existsSync(path.resolve(currentDir, 'package.json'));
while (!isPackageRoot) {
currentDir = path.join(currentDir, '..');
isPackageRoot = fs.existsSync(path.resolve(currentDir, 'package.json'));
}
const base = path.resolve(currentDir);
const relative = path.relative(currentDir, absolutePath);
const resultPath = relative.replace(/^src/, 'dist').replace(/\.ts$/, '.js');
return path.resolve(base, resultPath);
}

const newFile = findDistFile(process.argv.splice(-1)[0]);

require('../packages/build/node_modules/mocha/lib/cli').main(
[...process.argv, newFile].slice(2),
);

0 comments on commit 5e19d8c

Please sign in to comment.