-
Notifications
You must be signed in to change notification settings - Fork 386
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
Update Testing your application #635
Conversation
<!-- | ||
If you are just getting started with LoopBack, use the LoopBack command-line tool (CLI) `loopback-cli`. It's ready to use and installs simply by `npm install -g loopback-cli`. You don't need to do any extra steps for setup, and can head straight to the [next section](Testing-Your-Application#acceptance-testing.html). | ||
--> | ||
LoopBack applications that have been generated by `lb4 app` or `lb4` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can add here that if the user selects all our add-ons such as tslint
and mocha
during app creation, then they get them added as deps too with configs.
a21263b
to
3b71599
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ugh. I just spent a decent amount of time reviewing this only to realize that most of what I'm proposing changes to is not actually a part of the changeset.
This is why I would prefer that we have 80 chars per line whenever possible; GitHub's diff wrapping seems to expect it!
|
||
{% include important.html content="A great test suite requires you to think smaller and favor fast, focused unit tests over slow application-wide end-to-end tests | ||
{% include important.html content=" | ||
A great test suite requires you to think smaller and favor fast, focused unit tests over slow application-wide end-to-end tests. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
over slow end-to-end tests
Alternatively,
over slow, application-wide, end-to-end test
I would prefer the first, since end-to-end already implies that it covers the whole application, otherwise how could it be "end-to-end"?
@@ -331,13 +330,13 @@ describe('Person (unit)', () => { | |||
}); | |||
``` | |||
|
|||
Writing a unit test for a custom repository methods is not straightforward because `CrudRepositoryImpl` is based on legacy loopback-datasource-juggler that was not designed with dependency injection in mind. Instead, use integration tests to verify the implementation of custom repository methods; see [Test your repositories against a real database](#test-your-repositories-against-a-real-database) in [Integration Testing](#integration-testing). | |||
Writing a unit test for a custom repository methods is not as straightforward because `CrudRepository` is based on legacy [loopback-datasource-juggler](https://github.com/strongloop/loopback-datasource-juggler) that was not designed with dependency injection in mind. Instead, use integration tests to verify the implementation of custom repository methods; see [Test your repositories against a real database](#test-your-repositories-against-a-real-database) in [Integration Testing](#integration-testing). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replace ...that was not designed with...
with
...which was not designed with...
|
||
### Unit test your Sequence | ||
|
||
While it's possible to test a custom Sequence class in isolation, it's better to rely on acceptance-level tests in this exceptional case. The reason is that a custom Sequence class typically has many dependencies (which makes test setup too long and complex), and at the same time it provides very little functionality on top of the injected sequence actions. Bugs are much more likely to caused by the way how the real sequence action implementations interact together (which is not covered by unit tests), instead of the Sequence code itself (which is the only thing covered). | ||
While it's possible to test a custom Sequence class in isolation, it's better to rely on acceptance-level tests in this exceptional case. The reason is that a custom Sequence class typically has many dependencies (which makes test setup too long and complex), and at the same time it provides very little functionality on top of the injected sequence actions. Bugs are much more likely to be caused by the way the real sequence action implementations interact together (which is not covered by unit tests), instead of the Sequence code itself (which is the only thing covered). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
which makes test setup too long and complex
-> which can make test setup too long or complex
3b71599
to
abc426f
Compare
@bajtos Are we still advocating for attaching util-like functions to the model itself as seen in here? http://loopback.io/doc/en/lb4/Testing-your-application.html#unit-test-your-models-and-repositories |
I had the impression that GitHub's markdown renderer preserves line breaks, which is something we don't want 😞Because I could not find any documentation for this, I set out to find out the real behavior myself. TL;DR: GitHub treats newlines as soft line breaks (i.e. a breakable space) when rendering markdown files (which is great!), but treats newlines as hard line breaks when rendering issue/pull request descriptions and possibly comments too (which I find confusing and often annoying). https://github.github.com/gfm/#hard-line-breaks
https://github.github.com/gfm/#soft-line-breaks
A simple Markdown document to verify this behavior can be found here: So! Based on this investigation, I fully support your push for 80-char-wide lines in Markdown documents now, @kjdelisle. |
73ed168
to
37a8169
Compare
37a8169
to
df9495d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
line 145 If this line is going to remain in the final version, then you need to remove the initials. "shmks" Also I fixed the english a little. (which have not been)
+
minor edit in line 474: (Use "it" not "they")
Therefore, the code outlined in this section is outdated and may not work out of the box. It will be revisited after our MVP release.
line 480 - need to "a" - (a good thing)
Extending your API spec with examples is a good thing on its own, since developers consuming your API will find them useful too.
df9495d
to
337af4d
Compare
<!-- | ||
If you are just getting started with LoopBack, use the LoopBack command-line tool (CLI) `loopback-cli`. It's ready to use and installs simply by `npm install -g loopback-cli`. You don't need to do any extra steps for setup, and can head straight to the [next section](Testing-Your-Application#acceptance-testing.html). | ||
--> | ||
LoopBack applications that have been generated by `lb4 app` or `lb4` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LoopBack application that have been generated using the lb4 app
command from @loopback/cli
come with @loopback/testlab
and mocha
as a default, so no other testing infrastructure setup is needed.
^ I tried to make it shorter and more to the point. Don't think we need to list both cli commands, just the one we promote as a default for creating a new project in examples. And don't think we need to mention it's a prompt. They should know that because they would've already created the app by now.
"mocha": "^<current-version>" | ||
}, | ||
"scripts": { | ||
"test": "mocha" | ||
"test": "mocha --recursive \"DIST/test\"" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use of DIST requires the use of lb-mocha
from @loopback/build
which replaces DIST
with the appropriate folder name based on version ... If you want to use mocha then it should be dist
since we no longer support Node 6 (dist6).
da164ed
to
cd2413c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still reviewing more code, just some personal opinion for your question regarding database:
The memory db created in example-getting-started
is easy to setup and stores your data in the specified file, which IMO is a reasonable choice for testing data persistence.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Line 57: need to make application plural!
LoopBack applications that have been generated using the lb4 app
command from
132-135: suggest the following rewrite:
Easier to understand, since it's immediately clear what model properties are
+relevant to the tests. If the tests set the required properties,
+it is difficult to tell whether the properties are actually
+relevant to the tested scenario.
lines 138-141, suggest the following rewrite:
If the tests build model instance data
+manually, you would have to update all tests to set a new required property.
+With a shared helper, you update a single location with the new property.
|
||
const details = await controller.getDetails(1); | ||
const details = await controller.getDetails('pen'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@shimks A general question: do we have an example package contains all these code?
I came across the thought when searched for function getDetails
, it should be defined in some folded snippet. Just feel if we have an example testing package might be more straight-forward.(out of the scope of your PR definitely)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've actually went ahead and created an app to make sure these codes work. These code snippets are literally code snippets from the project that I've made :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lines 254-256: control not controls
+Unit tests are considered "white-box" tests because they use an "inside-out"
+approach where the tests know about the internals and control all the variables
+of the system being tested.
@jannyHou then the question becomes should the memory db exist in |
I am copyediting this section. I will need to finish it tomorrow as we have a translation drop on Weds for Velox. I have submitted a few copyedits. I did want to make a comment that the section is very long and reads like a research paper. I am thinking it should be trimmed to provide only the suggested steps for generating LB4 tests, rather than providing so much discussion? Or maybe the intent was to provide a discussion of the pro's and con's of different approaches. It does seem long. |
I agree that it is a bit long, but I like the intent that it's going for where the testing strategy is discussed in details. Do you think that it'd be better if it was broken down into sections maybe? |
let app: HelloWorldApp; | ||
let request: supertest.SuperTest<supertest.Test>; | ||
let app: HelloWorldApplication; | ||
let client: supertest.SuperTest<supertest.Test>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can simplify it by let client: Client
and import Client
from @loopback/testlab
See https://github.com/strongloop/loopback-next/blob/master/packages/testlab/src/client.ts#L15
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 great update, LGTM! just a few nitpick.
cd2413c
to
cff5c49
Compare
e71e09c
to
db62445
Compare
db62445
to
8e6a6db
Compare
A question for anyone that's familiar with test databases: should I be creating a separate config for test-specific databases? If so, where should the config file and the typescript file live and how should they be named?