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

Skip warnings in test contexts. #441

Merged
merged 13 commits into from
Feb 27, 2024
Merged

Skip warnings in test contexts. #441

merged 13 commits into from
Feb 27, 2024

Conversation

jgerigmeyer
Copy link
Member

One approach to fix #439 (comment).

Copy link
Member

@LeaVerou LeaVerou left a comment

Choose a reason for hiding this comment

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

Hmm, I’m unsure.

The concept of dev vs prod goes beyond Node.js, and I would rather introduce a more abstract setting and code that tweaks it based on NODE_ENV when appropriate, rather than use NODE_ENV all over our codebase, going to the extent of mocking it in the browser! That's pretty poor DX, though I certainly appreciate the effort to use a pre-existing concept. Instructing authors to mock process.env.NODE_ENV to enable warnings could also have the unintended side effect of breaking other scripts that check for process or process.env to detect whether they’re running in Node or the browser.

How do other libraries solve this? There must be an established convention! Vue uses a separate bundle, what about others?

Btw, we should not be editing tests/ really. That's there for historical reasons, and any edits should go to the new testsuite. Otherwise we end up in the rut of maintaining two testsuites and having to keep them in sync.
FWIW we should probably even remove tests/ once all tests are ported and we are confident we haven't missed anything.

* main:
  Enforce trailing commas unless `]` or `}` is on the same line. (#440)
  Update index.html
  Update color-swatch.css
  lint
  [apps/gamut-mapping] Support multiple colors
  [apps/gamut-mapping] Show favicon and title immediately, not just when color changes
  [apps/gamut-mapping] Prepare HTML & CSS for supporting multiple colors
@jgerigmeyer
Copy link
Member Author

The concept of dev vs prod goes beyond Node.js, and I would rather introduce a more abstract setting and code that tweaks it based on NODE_ENV when appropriate, rather than use NODE_ENV all over our codebase, going to the extent of mocking it in the browser! That's pretty poor DX, though I certainly appreciate the effort to use a pre-existing concept.

Hm, I don't think this is entirely accurate (but I'm also not tied to this approach). What I've done here is allow developers to opt out of console warnings in test environments. We're only using NODE_ENV in one place in our production codebase -- and presumably if we want this in other places for other warnings in the future, then we could abstract a single isTestEnv boolean somewhere. The only time this might impact a browser env is if a dev is running browser-based tests against Color.js and doesn't want to see warnings in the console. Authors and users will continue to see the warnings unless they have process.env.NODE_ENV = 'test' set.

Instructing authors to mock process.env.NODE_ENV to enable warnings could also have the unintended side effect of breaking other scripts that check for process or process.env to detect whether they’re running in Node or the browser.

We're not instructing anyone to mock process.env.NODE_ENV -- we're looking at whether or not that is already set to determine whether we're running in a test environment, which is a pretty common pattern.

How do other libraries solve this? There must be an established convention! Vue uses a separate bundle, what about others?

I think this is only tangentially related to dev/prod bundles, actually -- tho I agree that still might be a good idea.

Btw, we should not be editing tests/ really. That's there for historical reasons, and any edits should go to the new testsuite. Otherwise we end up in the rut of maintaining two testsuites and having to keep them in sync. FWIW we should probably even remove tests/ once all tests are ported and we are confident we haven't missed anything.

👍

@LeaVerou
Copy link
Member

How about this?

  • We add a setting for whether warnings should be shown, that modules import. That setting can check for process.env.NODE_ENV but we can easily add more conditions to it in one fell swoop, and authors can import it and override it manually.
  • We add a warn() function that checks for this setting and uses console.warn() after. This not only abstracts this all away, it even allows authors to override it with a custom warn() function to redirect the warnings elsewhere. And we can do cool things with it like e.g. support a once parameter that prevents warnings from flooding the console (very very useful for depreciation warnings).

@jgerigmeyer
Copy link
Member Author

How about this?

  • We add a setting for whether warnings should be shown, that modules import. That setting can check for process.env.NODE_ENV but we can easily add more conditions to it in one fell swoop, and authors can import it and override it manually.
  • We add a warn() function that checks for this setting and uses console.warn() after. This not only abstracts this all away, it even allows authors to override it with a custom warn() function to redirect the warnings elsewhere. And we can do cool things with it like e.g. support a once parameter that prevents warnings from flooding the console (very very useful for depreciation warnings).

I like it! And also I don't think this needs to hold up v0.5.0 😅

@jgerigmeyer jgerigmeyer marked this pull request as draft February 16, 2024 18:10
@jgerigmeyer jgerigmeyer removed the request for review from lloydk February 16, 2024 18:10
@lloydk
Copy link
Collaborator

lloydk commented Feb 16, 2024

I think abstracting the the conditional into a isTestEnv function is a good idea even if we end up using it in just one place.

Why don't we start with opting out of the warnings in the cli and not worry about the browser tests for now.

@lloydk
Copy link
Collaborator

lloydk commented Feb 16, 2024

How about this?

  • We add a setting for whether warnings should be shown, that modules import. That setting can check for process.env.NODE_ENV but we can easily add more conditions to it in one fell swoop, and authors can import it and override it manually.
  • We add a warn() function that checks for this setting and uses console.warn() after. This not only abstracts this all away, it even allows authors to override it with a custom warn() function to redirect the warnings elsewhere. And we can do cool things with it like e.g. support a once parameter that prevents warnings from flooding the console (very very useful for depreciation warnings).

I like that approach

* missing-space-formats:
  Remove dashed-ident prefix for HDR colors.
  Enforce trailing commas unless `]` or `}` is on the same line. (#440)
  Update index.html
  Update color-swatch.css
  lint
  [apps/gamut-mapping] Support multiple colors
  [apps/gamut-mapping] Show favicon and title immediately, not just when color changes
  [apps/gamut-mapping] Prepare HTML & CSS for supporting multiple colors
@jgerigmeyer
Copy link
Member Author

jgerigmeyer commented Feb 16, 2024

@LeaVerou I refactored this to add configurable verbose and warn options. By default it will use console.warn unless process.env.NODE_ENV === "test".

I did not set process.env.NODE_ENV=test for our local usage of htest -- would you prefer that happen here in Color.js, or should I submit a PR to htest that sets it in node-run.js for all users of htest?

Like I said earlier, I don't think this should be a blocker for v0.5.0.

src/defaults.js Outdated Show resolved Hide resolved
@LeaVerou
Copy link
Member

@LeaVerou I refactored this to add configurable verbose and warn options. By default it will use console.warn unless process.env.NODE_ENV === "test".

Thanks! We should use it in other places where we currently use console.warn()!

I did not set process.env.NODE_ENV=test for our local usage of htest -- would you prefer that happen here in Color.js, or should I submit a PR to htest that sets it in node-run.js for all users of htest?

Submitting it as an htest PR would be great, thank you!

Like I said earlier, I don't think this should be a blocker for v0.5.0.

Agreed.

Base automatically changed from missing-space-formats to main February 19, 2024 16:12
* main:
  [ci] Update lint rules for PR's (#444)
  Parse both dashed-ident and ident versions, and add missing dashed-idents to color spaces. (#439)
  [types] Fix formatting in `toGamut.d.ts` (#445)
  [types] Add JSDoc based on website docs (#436)
Copy link

netlify bot commented Feb 19, 2024

Deploy Preview for colorjs ready!

Name Link
🔨 Latest commit 7eb19d9
🔍 Latest deploy log https://app.netlify.com/sites/colorjs/deploys/65d3a3dbdcf1dd00082454af
😎 Deploy Preview https://deploy-preview-441--colorjs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@jgerigmeyer
Copy link
Member Author

Thanks! We should use it in other places where we currently use console.warn()!

@LeaVerou Can you clarify what places you're referring to? I don't see any uses of console.warn, and only one usage of console.log in the user-facing code (the rest are in website apps or the test suite). I updated the one place I found in 7eb19d9

I did not set process.env.NODE_ENV=test for our local usage of htest -- would you prefer that happen here in Color.js, or should I submit a PR to htest that sets it in node-run.js for all users of htest?

Submitting it as an htest PR would be great, thank you!

Done: LeaVerou/htest#13

@jgerigmeyer jgerigmeyer marked this pull request as ready for review February 19, 2024 20:20
@jgerigmeyer
Copy link
Member Author

@LeaVerou Ping on this -- are there other places you want this new warn method used?

Copy link
Member

@LeaVerou LeaVerou left a comment

Choose a reason for hiding this comment

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

LGTM sorry for the delay!!

@jgerigmeyer jgerigmeyer merged commit 57a88ed into main Feb 27, 2024
6 checks passed
@jgerigmeyer jgerigmeyer deleted the skip-warnings-in-tests branch February 27, 2024 04:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants