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

chore(e2e): refactor context to use yargs parser #6398

Merged
merged 1 commit into from
Oct 28, 2024

Conversation

gribnoysup
Copy link
Collaborator

This patch continues to clean-up compass-e2e-tests code so that it's hopefully easier to maintain going forward. We kinda outgrew configuring the tests using just env vars and manually checking for some arg flags, so this patch refactors all this logic to use yargs parser to better structure the logic around providing various flags, especially for cases where we expect some of them to be provided together or not overlap.

We basically have two very distinct environments for testing, web or desktop, with some subgroups that can be triggered with args (packaged vs just compiled, atlas cloud vs local sandbox, with sandbox with cloud login coming soon) + a common set of args that's applied in both cases, this is the logic that's now coded as part of yargs argv descriptor. This patch also adds helper methods to make it easier and stricter (both in types and runtime) to identify the environment the tests are running against.

This is how the help output looks for the script now
Run e2e tests against Compass desktop

Options:
  --help                        Show help                              [boolean]
  --disable-start-stop          Disables automatically starting (and stopping)
                                default local test mongodb servers and
                                compass-web sandbox                    [boolean]
  --test-groups                 Run tests in batches. Sets the total number of
                                test groups to have        [number] [default: 1]
  --test-group                  Run tests in batches. Sets the current test
                                group from the total number[number] [default: 1]
  --test-filter                 Filter the spec files picked up for testing
                                                                        [string]
  --webdriver-waitfor-timeout   Set a custom default webdriver waitFor timeout
                                                      [number] [default: 120000]
  --webdriver-waitfor-interval  Set a custom default webdriver waitFor interval
                                                         [number] [default: 100]
  --mocha-timeout               Set a custom default mocha timeout
                                                      [number] [default: 240000]
  --mocha-bail                  Bail on the first failing test instead of
                                continuing                             [boolean]
  --hadron-distribution         Configure hadron distribution that will be used
                                when packaging compass for tests (has no effect
                                when testing packaged app)
                                                   [string] [default: "compass"]
  --disable-clipboard-usage     Disable tests that are relying on clipboard
                                                      [boolean] [default: false]
  --test-packaged-app           Test a packaged binary instead of running
                                compiled assets directly with electron binary
                                                      [boolean] [default: false]
  --compile                     When not testing a packaged app, re-compile
                                assets before running tests
                                                       [boolean] [default: true]
  --native-modules              When not testing a packaaged app, re-compile
                                native modules before running tests
                                                       [boolean] [default: true]

All command line arguments can be also provided as env vars with `COMPASS_E2E_`
prefix:

  COMPASS_E2E_TEST_PACKAGED_APP=true compass-e2e-tests desktop
compass-e2e-tests web

Run e2e tests against Compass web

Options:
  --help                                    Show help                  [boolean]
  --disable-start-stop                      Disables automatically starting (and
                                            stopping) default local test mongodb
                                            servers and compass-web sandbox
                                                                       [boolean]
  --test-groups                             Run tests in batches. Sets the total
                                            number of test groups to have
                                                           [number] [default: 1]
  --test-group                              Run tests in batches. Sets the
                                            current test group from the total
                                            number         [number] [default: 1]
  --test-filter                             Filter the spec files picked up for
                                            testing                     [string]
  --webdriver-waitfor-timeout               Set a custom default webdriver
                                            waitFor timeout
                                                      [number] [default: 120000]
  --webdriver-waitfor-interval              Set a custom default webdriver
                                            waitFor interval
                                                         [number] [default: 100]
  --mocha-timeout                           Set a custom default mocha timeout
                                                      [number] [default: 240000]
  --mocha-bail                              Bail on the first failing test
                                            instead of continuing      [boolean]
  --hadron-distribution                     Configure hadron distribution that
                                            will be used when packaging compass
                                            for tests (has no effect when
                                            testing packaged app)
                                                   [string] [default: "compass"]
  --disable-clipboard-usage                 Disable tests that are relying on
                                            clipboard [boolean] [default: false]
  --browser-name                            Test runner browser name
                              [choices: "chrome", "firefox"] [default: "chrome"]
  --browser-version                         Test runner browser version (`unset`
                                            will not provide an explicit version
                                            to webdriver)
                                                    [string] [default: "latest"]
  --sandbox-url                             Set compass-web sandbox URL
                                     [string] [default: "http://localhost:7777"]
  --test-atlas-cloud-external               Run compass-web tests against an
                                            external Atlas Cloud URL (e.g.,
                                            https://cloud-dev.mongodb.com)
                                                                       [boolean]
  --atlas-cloud-external-url                External URL to run the tests
                                            against                     [string]
  --atlas-cloud-external-project-id         Atlas `projectId` value     [string]
  --atlas-cloud-external-cookies-file       File with a JSON array of cookie
                                            values that should contain Atlas
                                            Cloud auth cookies          [string]
  --atlas-cloud-external-default-connectio  File with JSON array of connections
  ns-file                                   (following ConnectionInfo schema)
                                            that are expected to be available in
                                            the Atlas project           [string]

All command line arguments can be also provided as env vars with `COMPASS_E2E_`
prefix:

  COMPASS_E2E_TEST_ATLAS_CLOUD_EXTERNAL=true compass-e2e-tests web

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

GitHub is collapsing this one because of the amount of changes, the logic is all the same, but yeah, rewriting it from env vars changed a lot of code here, so might be easier to just read through it as if this file was just added

Partial<DesktopParsedArgs & WebParsedArgs>;

export function isTestingDesktop(ctx = context): ctx is DesktopParsedArgs {
return testEnv === 'desktop';
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We can explicitly check for every desktop specific command argument here as defined above, but yargs with strict() guarantees that this assertion is correct if we managed to set testEnv to desktop (same assumption can be made for web)

Copy link
Contributor

Choose a reason for hiding this comment

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

In this specific case it's unlikely that the assertion will be broken, but I wonder if there is a way to make the assertion less disconnected from the code that actually ensures this is valid

Copy link
Contributor

Choose a reason for hiding this comment

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

I was thinking something like a map of the testEnv -> argsParser, which would be used to in both pieces of code. but looking a bit deeper into how the types are build, it's probably more complicated to implement that and probably not worth it given that the very small risk.

@@ -26,6 +26,9 @@
],
"js-yaml": [
"^3.13.1"
],
"yargs": [
"^4.8.1"
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

hadron-build is on 4 and I don't feel like I can deal with it as a drive by of adding yargs usage to compass-e2e-tests tbh

@gribnoysup gribnoysup force-pushed the compass-e2e-tests-with-yargs-parser branch 2 times, most recently from f9a5867 to f988cb8 Compare October 24, 2024 13:23
@gribnoysup gribnoysup force-pushed the compass-e2e-tests-with-yargs-parser branch from f988cb8 to ea818b7 Compare October 24, 2024 13:53
@@ -76,7 +71,7 @@ export async function mochaGlobalSetup(this: Mocha.Runner) {

debug('X DISPLAY', process.env.DISPLAY);

if (!DISABLE_START_STOP) {
if (!context.disableStartStop) {
Copy link
Contributor

@paula-stacho paula-stacho Oct 28, 2024

Choose a reason for hiding this comment

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

now that the params are camelCase, I'd kind of expect them to also follow the naming convention and this bool to be isStartStopDisabled. might be just me and others would expect the naming to be direct derivative of the original param 🤷 What do you think? If we wanted to rename them, we could do that with yargs middleware callback https://yargs.js.org/docs/#api-reference-middlewarecallbacks-applybeforevalidation

Copy link
Collaborator Author

@gribnoysup gribnoysup Oct 28, 2024

Choose a reason for hiding this comment

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

I think being able to map arguments that are passed to the script to the usage in the code is more important for the dev code like the e2e tests. Middleware would add one more extra step to this process and this helper doesn't extend the types, so it would be even extra code in between to align the types with the remapping. That's why I'd prefer not to change it

Copy link
Contributor

@paula-stacho paula-stacho left a comment

Choose a reason for hiding this comment

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

Neat! ❤️ Left a suggestion, otherwise looks great!

@gribnoysup gribnoysup merged commit 8f1cd9f into main Oct 28, 2024
28 checks passed
@gribnoysup gribnoysup deleted the compass-e2e-tests-with-yargs-parser branch October 28, 2024 12:36
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.

2 participants