Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adds utility launch for debugging a single test file #4432

Merged
merged 1 commit into from
Mar 9, 2020

Conversation

mschnee
Copy link
Contributor

@mschnee mschnee commented Jan 14, 2020

Partially implements: #4300
See: #4406

This has been split from #4406 as a more manageable, smaller PR.

This update adds a developer convenience Launch task to allow debugging a single unit test file in Visual Studio Code.

Adds

  • bin/mocha-current-file.js - this is a utility script that attempts to determine the dist output of the given test file, and passes it off to mocha in-process.
  • VSCode: Debug Current Test File launch configuration. This launch configuration allows you to debug the current unit test file currently focused. It uses the above helper to determine the correct name and path of the compiled JavaScript file to run.

Modifies

  • The launch for "Run Mocha Tests" so that it also runs on Windows.

Checklist

  • npm test passes on your machine
  • New tests added or existing tests modified to cover all changes
  • Code conforms with the style guide
  • API Documentation in code was updated
  • Documentation in /docs/site was updated
  • Affected artifact templates in packages/cli were updated
  • Affected example projects in examples/* were updated

@derdeka derdeka added community-contribution developer-experience Issues affecting ease of use and overall experience of LB users labels Jan 14, 2020
@mschnee
Copy link
Contributor Author

mschnee commented Jan 15, 2020

There are a couple of instances (e.g. debugging packages/metadata/src/__tests__/decorator-factory.unit.ts) where breakpoints don't get picked up as expected. This could be the "autoAttachChildProcesses": true, in the launch configuration.

.vscode/launch.json Outdated Show resolved Hide resolved
@mschnee mschnee force-pushed the feat/debug-test-launch branch from efad088 to adf202e Compare January 22, 2020 17:58
@mschnee
Copy link
Contributor Author

mschnee commented Jan 22, 2020

NOTE: I have rebased from upstream and squashed all commits, which should address the travis commitlint and doc errors.

@mschnee mschnee force-pushed the feat/debug-test-launch branch from 9e4dbb2 to 45b825d Compare January 31, 2020 19:37
@mschnee mschnee requested a review from raymondfeng January 31, 2020 19:37
@mschnee mschnee force-pushed the feat/debug-test-launch branch from 45b825d to f7f7fb6 Compare January 31, 2020 21:04
@mschnee mschnee force-pushed the feat/debug-test-launch branch from f7f7fb6 to 1f434f2 Compare January 31, 2020 21:30
@mschnee mschnee requested a review from raymondfeng February 11, 2020 22:42
Copy link
Member

@bajtos bajtos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! The changes look reasonably, I have two small comments to address.

.vscode/launch.json Outdated Show resolved Hide resolved
.vscode/launch.json Show resolved Hide resolved
.vscode/launch.json Show resolved Hide resolved
@mschnee
Copy link
Contributor Author

mschnee commented Feb 20, 2020

Sorry for the delay in getting back to this PR! I had a brief vacation. Will update and address things :)

@mschnee mschnee force-pushed the feat/debug-test-launch branch from 1f434f2 to a03b815 Compare February 21, 2020 01:53
@mschnee mschnee requested a review from bajtos February 21, 2020 01:56
@mschnee
Copy link
Contributor Author

mschnee commented Feb 27, 2020

With approvals from @raymondfeng and @bajtos I'd be happy to merge this in :)

Copy link
Member

@bajtos bajtos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Welcome back, @mschnee. I am looking forward to see this improvement landed 🤸‍♂️

I am afraid I found few details that I am not very happy with, can you please fix them? See my comments below and also Raymond's comment about wrong copyright year(s) in the license header.

Thank you!

.vscode/launch.json Outdated Show resolved Hide resolved
bin/mocha-current-file.js Show resolved Hide resolved
@mschnee mschnee force-pushed the feat/debug-test-launch branch from 6df5740 to aec9eeb Compare March 2, 2020 16:42
@mschnee mschnee requested a review from bajtos March 2, 2020 18:15
@mschnee mschnee requested review from raymondfeng and derdeka March 2, 2020 18:15
@mschnee mschnee force-pushed the feat/debug-test-launch branch 3 times, most recently from 6b2b1e0 to 2d91f92 Compare March 2, 2020 18:55
@mschnee
Copy link
Contributor Author

mschnee commented Mar 2, 2020

Addressing the commitlint changes :)

@mschnee mschnee force-pushed the feat/debug-test-launch branch from 2d91f92 to 68a5d53 Compare March 2, 2020 19:51
Copy link
Contributor

@derdeka derdeka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏 Thanks, works well for me with Windows 10.

feat(build): adds launch for debugging a single test file

nitpick: Please change the commit message, as it does not effekt the build package anymore.

@mschnee
Copy link
Contributor Author

mschnee commented Mar 4, 2020

@derdeka What's the correct scope? core?

@mschnee mschnee force-pushed the feat/debug-test-launch branch from 68a5d53 to 5e19d8c Compare March 4, 2020 20:48
@bajtos
Copy link
Member

bajtos commented Mar 6, 2020

What's the correct scope? core?

core is for changing affecting @loopback/core package.

In this case, I think it's best to leave the scope out. Also please note that we are using imperative form ("add something"), not present tense ("adds something"). See subject in our Commit Message Guidelines and also A Note About Git Commit Messages.

I am proposing the following commit message:

feat: add a launch task for debugging a single test file

(Personally, I would choose chore instead of feat because this change is not visible to LoopBack users, but that does not really matter.)

Copy link
Member

@bajtos bajtos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👏

Please update the commit message per my comment above before landing.

const relative = path.relative(currentDir, absolutePath);
const resultPath = relative.replace(/^src/, 'dist').replace(/\.ts$/, '.js');
return path.resolve(base, resultPath);
}
Copy link
Contributor

@raymondfeng raymondfeng Mar 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: the current code has a theoretical possibility of infinite loop as it does not test the root of a file system. A more robust implementation can be:

let pkgFile = path.resolve(filename, '../package.json');

// Find root directory of the file system
const root = path.parse(pkgFile).root;

while (!fs.existsSync(pkgFile)) {
  if (path.dirname(pkgFile) === root) {
    // We reach the root of file system
    pkgFile = undefined;
    break;
  }
  // Try the parent directory level package.json
  pkgFile = path.resolve(pkgFile, '../../package.json');
}

if (!pkgFile) return filename; // Or throw an error
const projectRoot = path.dirname(pkgFile);
const relative = path.relative(projectRoot, path.resolve(filename));
const jsFilePath = relative.replace(/^src/, 'dist').replace(/\.ts$/, '.js');
return path.resolve(projectRoot, jsFilePath);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made a small change to the current function, and tested it by invoking this cli against both a test file inside of the loopback-next workspace, and also a fake.test.js file that has no package.json file in it's filesystem heirarchy

@mschnee mschnee force-pushed the feat/debug-test-launch branch from 5e19d8c to 9b10153 Compare March 6, 2020 18:34
@mschnee mschnee requested a review from raymondfeng March 6, 2020 18:56
@mschnee mschnee force-pushed the feat/debug-test-launch branch from 9b10153 to e4acd65 Compare March 6, 2020 19:43
@mschnee mschnee requested a review from raymondfeng March 6, 2020 19:43
@mschnee mschnee merged commit b3fb343 into loopbackio:master Mar 9, 2020
@mschnee mschnee deleted the feat/debug-test-launch branch March 9, 2020 17:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
developer-experience Issues affecting ease of use and overall experience of LB users
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants