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

Throw friendly error when workspaces are not configured correctly #1212

Merged
merged 3 commits into from
Jan 18, 2018

Conversation

craigbilner
Copy link
Contributor

@craigbilner craigbilner commented Jan 17, 2018

Description

If you add useWorkspaces to your lerna config but don't make the necessary changes in your package.json, lerna bootstrap will fail here.

Motivation and Context

This PR adds a validation error to make it clearer where the problem is, rather than pointing at the call to some on an undefined value for example.

Potentially it should throw if not an Array<string> too?

How Has This Been Tested?

There didn't appear to be any repo workspace tests so added an example for future use using the naming convention that seemed to exist. Added one test to confirm workspaces were correctly returned as a default and another for when useWorkspaces is configured only.

As an aside all tests run and pass for me if I use yarn test but yarn test:watch fails on several of them. I can look at this separately if needed?

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

Copy link
Member

@evocateur evocateur left a comment

Choose a reason for hiding this comment

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

Thanks, this is a good idea! A few formatting nits, and we're good to go.


beforeAll(async () => {
testDir = await initFixture("Repository/basic");
testDirWithWorkspaces = await initFixture("Repository/yarn-workspaces");
Copy link
Member

Choose a reason for hiding this comment

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

I think we can move this into the one test case that actually needs it, instead of copying over the directory fixture every single test.

-    it("returns workspace packageConfigs", () => {
+    it("returns workspace packageConfigs", async () => {
+      const testDirWithWorkspaces = await initFixture("Repository/yarn-workspaces");
       const repo = new Repository(testDirWithWorkspaces);

it("throws with friendly error if workspaces are not configured", () => {
const repo = new Repository(testDir);
repo.lernaJson.useWorkspaces = true;
expect(() => repo.packageConfigs).toThrowErrorMatchingSnapshot();
Copy link
Member

Choose a reason for hiding this comment

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

I think we can avoid a solitary snapshot with a concise .toThrow(/workspaces need to be defined/) assertion here.

Copy link
Member

Choose a reason for hiding this comment

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

And if you use the "EWORKSPACES" prefix, the regex can be even simpler: .toThrow(/EWORKSPACES/)

@@ -61,6 +61,14 @@ class Repository {

get packageConfigs() {
if (this.lernaJson.useWorkspaces) {
if (!this.packageJson.workspaces) {
throw new ValidationError(
"Invalid workspaces",
Copy link
Member

Choose a reason for hiding this comment

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

This should be "EWORKSPACES", to be consistent with other ValidationErrors

if (!this.packageJson.workspaces) {
throw new ValidationError(
"Invalid workspaces",
"workspaces need to be defined in the root package.json." +
Copy link
Member

Choose a reason for hiding this comment

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

This should be a multiline tagged template string (using dedent, several examples of usage in src/Command.js).

Should probably call them "yarn workspaces", too, to clarify their origin.

@craigbilner
Copy link
Contributor Author

@evocateur I've tidied it up

Copy link
Member

@evocateur evocateur left a comment

Choose a reason for hiding this comment

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

Thanks!

@evocateur
Copy link
Member

I’m not sure what’s up with the test:watch script, I just use yarn jest --watch most of the time. If you’re interested in figuring it out, go for it, but it’s not high priority.

@evocateur evocateur merged commit 6080e6b into lerna:master Jan 18, 2018
@craigbilner
Copy link
Contributor Author

@evocateur fyi - looks like it's to do with the way the tests borrow each others files so the tests can't run in parallel. Running in band works. Would be a larger rewrite of the tests.

@evocateur
Copy link
Member

evocateur commented Jan 18, 2018 via email

@lock
Copy link

lock bot commented Dec 27, 2018

This thread has been automatically locked because there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Dec 27, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants