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

Update Testing your application #635

Merged
merged 7 commits into from
Mar 9, 2018
Merged

Update Testing your application #635

merged 7 commits into from
Mar 9, 2018

Conversation

shimks
Copy link
Contributor

@shimks shimks commented Feb 23, 2018

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?

  • please review the first commit since the commit after introduces line breaks (403489f)

<!--
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`
Copy link
Contributor

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.

@shimks shimks changed the title update Testing your application [WIP] update Testing your application Feb 23, 2018
@shimks shimks force-pushed the update-testing-doc branch 3 times, most recently from a21263b to 3b71599 Compare February 23, 2018 19:28
Copy link

@kjdelisle kjdelisle left a 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.

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).

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).

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

@shimks shimks force-pushed the update-testing-doc branch from 3b71599 to abc426f Compare February 26, 2018 21:47
@shimks shimks changed the title [WIP] update Testing your application [DO NOT REVIEW] update Testing your application Feb 28, 2018
@shimks
Copy link
Contributor Author

shimks commented Feb 28, 2018

@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

@bajtos
Copy link
Member

bajtos commented Mar 1, 2018

This is why I would prefer that we have 80 chars per line whenever possible; GitHub's diff wrapping seems to expect it!

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

A line break (not in a code span or HTML tag) that is preceded by two or more spaces and does not occur at the end of a block is parsed as a hard line break (rendered in HTML as a
tag).

For a more visible alternative, a backslash before the line ending may be used instead of two spaces.

https://github.github.com/gfm/#soft-line-breaks

A regular line break (not in a code span or HTML tag) that is not preceded by two or more spaces or a backslash is parsed as a softbreak. (A softbreak may be rendered in HTML either as a line ending or as a space. The result will be the same in browsers. In the examples here, a line ending will be used.)

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.

@shimks shimks force-pushed the update-testing-doc branch 2 times, most recently from 73ed168 to 37a8169 Compare March 2, 2018 22:22
@shimks shimks changed the title [DO NOT REVIEW] update Testing your application Update Testing your application Mar 2, 2018
@shimks shimks force-pushed the update-testing-doc branch from 37a8169 to df9495d Compare March 2, 2018 22:24
Copy link
Contributor

@bschrammIBM bschrammIBM left a 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.

@shimks shimks force-pushed the update-testing-doc branch from df9495d to 337af4d Compare March 5, 2018 15:36
<!--
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`
Copy link
Contributor

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\""
Copy link
Contributor

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).

@shimks shimks force-pushed the update-testing-doc branch from da164ed to cd2413c Compare March 5, 2018 17:45
Copy link
Contributor

@jannyHou jannyHou left a 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.

Copy link
Contributor

@bschrammIBM bschrammIBM left a 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');
Copy link
Contributor

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)

Copy link
Contributor Author

@shimks shimks Mar 5, 2018

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 :)

Copy link
Contributor

@bschrammIBM bschrammIBM left a 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.

@shimks
Copy link
Contributor Author

shimks commented Mar 5, 2018

@jannyHou then the question becomes should the memory db exist in src folder since it'll be solely for the test? Or do we want to have it also be the production database?

@bschrammIBM
Copy link
Contributor

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.

@shimks
Copy link
Contributor Author

shimks commented Mar 5, 2018

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>;
Copy link
Contributor

@jannyHou jannyHou Mar 5, 2018

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

Copy link
Contributor

@jannyHou jannyHou left a 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.

@shimks shimks force-pushed the update-testing-doc branch from cd2413c to cff5c49 Compare March 5, 2018 21:16
@shimks shimks force-pushed the update-testing-doc branch 2 times, most recently from e71e09c to db62445 Compare March 9, 2018 20:41
@shimks shimks force-pushed the update-testing-doc branch from db62445 to 8e6a6db Compare March 9, 2018 21:02
@shimks shimks merged commit 62245bc into gh-pages Mar 9, 2018
@shimks shimks deleted the update-testing-doc branch March 9, 2018 21:42
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.

7 participants