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
test: E2E Mock API structure #2624
test: E2E Mock API structure #2624
Conversation
d55087d
to
81d6411
Compare
This commit adds a plugin extensible test structure for e2e tests. It allows the developer to have a more scalable support structure for mock api test data, create custom commands, and generate custom data for tests. This diff also includes a working offline test example for proposals list.
81d6411
to
059f167
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.
@victorgcramos Nice work mate, I like the direction of this.
I got one suggestion regarding the params here:
export function inventoryReply(
{ amountByStatus = {}, pageLimit = 20 } = {},
{ state = 2, page }
)
I would change the function to accept one options object with all configuration instead of two destructed objects. This makes it easier to read and maintain, what do you think ?
hey @amass01, I liked your suggestion, but I opted to split those parameters so we can have a better idea of what's the request body data, and what are the test configuration parameters. Making it an argument is a good suggestion, but what about using a single object approach, which receives 2 destructured props: export function inventoryReply(
{ testParams: { amountByStatus, pageLimit }, requestParams: { state, page } }
) |
@victorgcramos yep this what i had in mind |
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.
Nice work mate, good job with the core package, I like it.
Found one TODO that can be done in this PR.
@@ -9,6 +9,7 @@ import get from "lodash/fp/get"; | |||
import find from "lodash/fp/find"; | |||
import path from "path"; | |||
|
|||
// TODO: move record related utils to core package |
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.
a TODO
beforeEach(function useAppMiddlewares() { | ||
// here you can define global middlewares | ||
}); |
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.
oh, that's great and will be useful when migration to 100% mocked APIs.
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.
tACK.
====================================================================================================
(Run Finished)
Spec Tests Passing Failing Pending Skipped
┌────────────────────────────────────────────────────────────────────────────────────────────────┐
│ ✔ admin/account.js 00:10 5 5 - - - │
├────────────────────────────────────────────────────────────────────────────────────────────────┤
│ ✔ admin/comments.js 00:07 1 1 - - - │
├────────────────────────────────────────────────────────────────────────────────────────────────┤
│ ✔ admin/proposals.js 00:43 8 8 - - - │
├────────────────────────────────────────────────────────────────────────────────────────────────┤
│ ✔ comments/authorUpdates.js 00:22 3 3 - - - │
├────────────────────────────────────────────────────────────────────────────────────────────────┤
│ ✔ comments/comments.js 00:29 5 5 - - - │
├────────────────────────────────────────────────────────────────────────────────────────────────┤
│ ✔ comments/commentVotes.js 00:27 5 5 - - - │
├────────────────────────────────────────────────────────────────────────────────────────────────┤
│ ✔ proposal/create.js 00:04 1 1 - - - │
├────────────────────────────────────────────────────────────────────────────────────────────────┤
│ ✔ proposal/detail.js 00:53 22 22 - - - │
├────────────────────────────────────────────────────────────────────────────────────────────────┤
│ ✔ proposal/edit.js 00:24 4 4 - - - │
├────────────────────────────────────────────────────────────────────────────────────────────────┤
│ ✔ proposal/formErrorCodes.js 00:06 5 5 - - - │
├────────────────────────────────────────────────────────────────────────────────────────────────┤
│ ✔ proposal/list.js 00:41 16 16 - - - │
├────────────────────────────────────────────────────────────────────────────────────────────────┤
│ ✔ user/2fa.js 00:07 2 2 - - - │
├────────────────────────────────────────────────────────────────────────────────────────────────┤
│ ✔ user/login.js 00:34 7 7 - - - │
├────────────────────────────────────────────────────────────────────────────────────────────────┤
│ ✔ user/register.js 00:14 2 2 - - - │
└────────────────────────────────────────────────────────────────────────────────────────────────┘
✔ All specs passed! 05:27 86 86 - - -
@victorgcramos conflicts |
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.
LGTM
This commit adds a plugin extensible test support structure for e2e tests. It
allows the developer to have a more scalable structure for mock api test data,
create custom commands, and generate custom data for tests.
plugin to create new custom support plugins
In order to make our codebase more scalable, we organized the support commands
and files according to the politeia plugin
structure. Each plugin will have its own support files, which can extend the
core (records) support files and create custom commands. You can use the custom
commands to test the application.
Core Package
Let's take a deeper look at the
teste2e/cypress/support/core
:api.js
:Contains all repliers for the plugin api calls. Each replier will accept one
object with two parameters:
testParams
andrequestParams
. Both describerespectively test configuration parameters, which can be used to configure
the response to match test pre-conditions, and request data sent to the API.
Let's build some replier for the
/records/v1/inventory
call:As you can see, we are receiving
{ amountByStatus, pageLimit }
from the testcase, which describe the status and state pre-conditions to run the tests.
{ state, page }
is the request body object for the/inventory
call (checkthe records api docs).
commands.js
Contains all custom cypress commands that can be used for testing the plugin
contents. See the Cypress Docs
for more information about how to create custom commands.
The core package contains the custom commands that can be used by other
support plugins, and core support methods, which can be used to create custom
plugin commands, such as
createMiddleware
:Let's build the
recordsMiddleware
command for therecords
package usingthe
createMiddleware
method:As mentioned before, "middlewares are nothing but an interceptor + alias", so
you can access the alias using the
"{packageName}.{endpoint}"
selector.Now, we can use the
recordsMiddleware
to intercept the/inventory
call:generate.js
Contains all mock data generators for the plugin. We can generate custom data
using the faker NPM package, which is a
poweful tool to generate random data.
Let's create a mock
Record
generator to generate custom records for our tests:Usage:
How to create a support package
Previously, we talked about the core support package, but one of the greatest
features of our plugin extensible architecture is that you can use the core
package commands to create new custom support files. Let's build an example for
the
ticketvote
plugin and create a middleware for theticketvote/v1/inventory
call.Create a new
ticketvote
folder under theteste2e/cypress/support
dir including the new
api.js
,commands.js
,generate.js
andutils.js
files. It should look like this:
Create the API replier for the
/inventory
request:Notice that we can use the same
recordsInventoryReply
as a replier to the/inventory
call, but we can also extend the replier and modify it towhatever we want. In this case, we want to filter the inventory to match the
request status, so we can only reply tokens for the given status, if it
exists.
Create the
ticketvote
middleware using the corecreateMiddleware
method:Write the test case under
teste2e/cypress/e2e
dir:You can combine the
recordsMiddleware
andticketvoteMiddleware
togenerate custom records for the ticketvote inventory tokens: