Repository navigation
Conversation
|
I would like to have this also, but I also feel that environment-specific code tends to hint at other problems. E.g. a lot of |
|
@mydea I believe the currently recommended solution for determining if we are in testing is to use: |
Yeah, there are definitely ways, but it's not ideal, not necessarily easy to find for new developers, and harder to use e.g. from addons - there are also ways there, like |
I will add some example use cases I personally came across to the motivation. Some I could think of are:
Overall, I am definitely positive that for all of these use cases, it is possible to find a decent workaround. However, I feel that a more agreed upon and documented solution for these is missing from the public API right now. I also think this is a need that most developers will stumble over at some point. Since I just searched for this in the last days I can attest to the fact that it is, as of now, not easily possible to find the "best"/recommended solution to these problems easily right now. I spent quite some time going through the API docs to search if, by chance, anything like that has been added (in the process, stumbling over the not-really-working anymore |
b128062 to
19f895e
Compare
|
I also think what @Gaurav0 says makes sense. If something would be added to Ember itself, perhaps there are other things we'd like to add too? For example the Ember version, or other things that are "locked in" at build-time. And have some shared name for that, e.g. Also, why would you want to export both |
|
@mydea the polling and The dev example for logging in as a mocked user is also good, and I think that environment specific configuration for addons is the right approach in the long term. Anyway, I totally agree that sooomething would be good, and this is a low enough level of effort to be justifiable. I'd just be worried about encouraging the antipattern of env specific code |
I agree with that, both with the concern, but also that we need something better than
Shameless plug: https://github.com/kaliber5/ember-window-mock 😏 |
|
I basically agree with everything said here, for many things there are (or there should be) ways to handle them more gracefully, however there still needs to be an escape hatch (and be it just for addons to be able to provide these better abstractions). And even for occurrences where using such flags might not be the ideal solution, it is probably still better to use an "approved" and clear solution than everybody using their own custom way to kind of get the same outcome. |
| import { TESTING } from "@ember/env"; | ||
| ``` | ||
|
|
||
| This works like `Ember.testing`. |
There was a problem hiding this comment.
I actually think this is a bit off from a design perspective:
Ember.testingcannot be checked in module scope, it is only set when starting an individual test and unset when the test is completed.- We want this value to mean something more akin to "this is a testing enabled build"
In other words, we do not want this to be the same as Ember.testing we want it to be:
- constant (does not change / live update without a refresh)
truefor all of the following:ember testember test --environment=productionember serve+/tests/
falsefor all of the following:ember serve+ any non/tests/pathember build --environment=production(non-testing related production build)
There was a problem hiding this comment.
What is the difference between this and environment === 'test' ?
There was a problem hiding this comment.
Not much from an applications perspective, but addons cannot use that style guard because they cannot import config from 'app-name-here/config/environment' (they have no clue what the app name they are consumed is is, and really shouldn't have to care).
There was a problem hiding this comment.
Yeah, basically it is:
import config from 'my-app/config/environmentonly works in host appsgetOwner(this).resolveRegistration('config:environment')only works with access to the container- Both of these are IMHO less explicit, also I always need to lookup the syntax for 2. as that is the only place I ever need it
There was a problem hiding this comment.
We already have the ember-get-config addon.
There was a problem hiding this comment.
Yes. However,
a) it feels weird to require an addon to solve this.
b) there is current Ember.testing which is much more convenient. However, it is not encouraged to use it anymore, without having a clear replacement.
c) Having a clear import for this is also less error prone & clearer in intent (e.g. consider config.environment === 'test' vs. config.environment === 'testing' - oops). It is easier to find and can be auto-completed by an editor.
There was a problem hiding this comment.
We already have the ember-get-config addon.
Sure, and that might be fine when you actually need the configuration, but determining when you should or should not include some test support code should be a built in concept.
Additionally, it is pretty important for these code paths to be stripped when TESTING is not true (e.g. ember build --environment="production"), that is not possible with ember-get-config.
19f895e to
dc1d01c
Compare
dc1d01c to
1faed7a
Compare
|
I have updated the RFC content based on @rwjblue's feedback. It now proposes to just add an |
|
|
||
| ## Unresolved questions | ||
|
|
||
| Should we deprecate the older functionality immediately or leave a grace period? |
There was a problem hiding this comment.
I think deprecation immediately is probably fine, since the implementation of TESTING will work retroactively (basically anyone that can use ember-cli-babel@7 would be able to use it, regardless of ember-source version).
| Finally, we should deprecate these older functions/modules: | ||
|
|
||
| - `Ember.testing` | ||
| - `runInDebug` from `@ember/debug` |
There was a problem hiding this comment.
Please specifically mention these deprecations in the summary.
Please add a Transition Path section for these deprecations. It should include enough information to write a deprecation guide pull request to https://github.com/ember-learn/deprecation-app.
It should specifically include the code each of these is refactored to (I know this is obvious, but still should be included for the deprecation guide.)
There was a problem hiding this comment.
Please specifically mention these deprecations in the summary.
Deprecations aren't really a part of the summary for RFCs that are not explicitly deprecations RFCs. In this case, I believe they belong here in the detailed design.
Please add a Transition Path section for these deprecations. It should include enough information to write a deprecation guide pull request to ember-learn/deprecation-app.
It should specifically include the code each of these is refactored to (I know this is obvious, but still should be included for the deprecation guide.)
Ya, good call out.
There was a problem hiding this comment.
I added a transition path for the deprecated functionality!
There was a problem hiding this comment.
Ember.testing is currently a wrapper for isTesting() and setTesting() from @ember/debug. Should those functions/exports also be affected?
There was a problem hiding this comment.
These are not public? They don't even show up as private here: https://api.emberjs.com/ember/release/classes/@ember%2Fdebug?show=inherited%2Cprotected%2Cprivate
There was a problem hiding this comment.
@cvx - I don't think we have module exports for those, that is just how Ember internally is organized. The deprecation here would be upon touching Ember.testing which means it would go into the getTesting method.
|
We now have a merged RFC that contains a macro system that overlaps with the main idea in this proposal. Environment checking really wants to compose with the macro system so you can strip out code per environment. Plus the macro system is already implemented. Adding a new macro named like So my suggestion is to reword this RFC so adds a new import { environment } from "@ember/macros";
if (environment() === "test") {
// ...
}The type signature of environment would be: function environment(): "development" | "test" | "production";(Please note these three really are the only environments Ember has or has ever had. People sometimes think they can make their own extra environments, but that isn't really true, what they really want are separate ember-cli-deploy targets, but every target falls into one of these three environments.) You could use import { macroCondition, environment } from '@ember/macros';
if (macroCondition(environment() === 'test')) {
// ...
} |
|
After getting more detail from @rwjblue, my previous comment didn't account for the fact that this is trying to reduce the number of environments from three to two. That's great too, we can detangle "test vs not-test" from "dev vs prod" as separate questions, but I still advocate asking those questions via |
|
Ya, agree in general @ef4. I do also want to keep in mind (since I have to do a bit of work to support this) that we will want these same features in Glimmer.js / GlimmerX style apps, but that is a bit orthogonal. |
|
I would be fine with supporting the ember macros in glimmer apps too. They really don’t need much to integrate with a new build system (that is part of what they’re for). |
I mean, I get the point that this is how it should be, but I've worked on apps that actually had more environments, and it all worked just fine (with some adjustments in the ember-cli-build.js). So I am not sure if I would agree with that statement - it is perfectly possible to set any other environment. It might make sense to actually deprecate that at some point (allowing to use a different environment), but I guess that's a different issue. Personally, I don't really care if we import this from import { isDebug, isTesting } from '@ember/macros` |
|
Sidenote, I just ran into this case when trying to use Basically, setting a macro value for tests will not work like this: module.exports = function (defaults) {
let env = process.env.EMBER_ENV;
let app = new EmberApp(defaults, {
'@embroider/macros': {
setOwnConfig: {
mockApi: env === 'test'
},
},
}As this will be false when running |
|
@mydea your example is nearly enough to make that scenario work. What's missing is that, when you run I haven't opened this up as a public API yet because I was letting it get stabilized first, but we already do this for We will do the same for an |
|
So, if this is being implemented in @embroider/macros, do we need an/this RFC? Or how does that work then? |
|
I just saw that this is now available in the latest release of import { isTesting, isDevelopingApp, macroCondition } from '@embroider/macros';
function myFunction() {
if (macroCondition(isTesting())) {
// do something test specific
}
if (macroCondition(isDevelopingApp())) {
// do something development specific
}
}This works nicely, as far as I can see, and is properly tree-shaked. So I guess all that remains is potentially some documentation, but that affects all of Should I close this RFC, or leave it open until the question of documentation is resolved? |
|
I think we can close this, as this can be pretty nicely accomplished now using import { isTesting, isDevelopingApp } from '@embroider/macros';Works like a charm in our app, and can properly tree-shake code when used with |
Rendered