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

feat: implement new cli command to read tests #986

Merged
merged 1 commit into from
Sep 9, 2024

Conversation

DudaGod
Copy link
Member

@DudaGod DudaGod commented Aug 5, 2024

What is done

Implement ability to read tests using cli command npx testplane list-tests which support options like for run tests:

  • --config;
  • --browser;
  • --set;
  • --require;
  • --grep;
    and new options:
  • --ignore;
  • --silent;
  • --output-file;
  • --formatter.

The formatter supports 2 values: list (default) and tree. When using list user get plain list of unique tests, example:

[
    {
        "id": "5a105e8",
        "titlePath": [
            "test1"
        ],
        "browserIds": [
            "yandex",
            "chrome"
        ],
        "file": "tests/second.hermione.js",
        "pending": false,
        "skipReason": ""
    },
    {
        "id": "d2b3179",
        "titlePath": [
            "suite",
            "test2"
        ],
        "browserIds": [
            "yandex",
            "chrome"
        ],
        "file": "tests/second.hermione.js",
        "pending": false,
        "skipReason": ""
    }
]

The same data in tree formatter looks like:

[
    {
        "id": "36749990",
        "title": "suite",
        "line": 3,
        "column": 1,
        "file": "example.hermione.js",
        "suites": [],
        "tests": [
            {
                "id": "d2b3179",
                "title": "test2",
                "line": 4,
                "column": 5,
                "browserIds": [
                    "yandex"
                ],
                "pending": false,
                "skipReason": ""
            }
        ],
        "pending": false,
        "skipReason": ""
    },
    {
        "id": "5a105e8",
        "title": "test1",
        "line": 1,
        "column": 1,
        "browserIds": [
            "yandex"
        ],
        "file": "example.hermione.js",
        "pending": false,
        "skipReason": ""
    }
]

the important difference is that the tree formatter supports line and column fields. They can be used to find out exactly where the suite or test is declared in the file. It will be used in vscode extension.


The same output is supported via API - testCollection.format(<type>)

@DudaGod DudaGod force-pushed the HERMIONE-8.cli_cmd_to_read_tests branch from 4aa6d14 to c160044 Compare August 5, 2024 15:47
docs/cli.md Outdated
- [Reporters](#reporters)
- [Require modules](#require-modules)
- [`testplane` command](#testplane-command)
- [Options](#options)
Copy link
Member Author

Choose a reason for hiding this comment

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

at the moment, I have described in detail only the options about which there has already been an information

docs/cli.md Outdated
- `browserIds` (`String[]`) - list of browsers in which the test will be launched;
- `file` (`String`, only in tests without parent suites) - path to the file relative to the working directory.

### Overriding settings
Copy link
Member Author

Choose a reason for hiding this comment

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

Nothing has changed here

- [Logs from the browser console in the terminal](#logs-from-the-browser-console-in-the-terminal)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

Copy link
Member Author

Choose a reason for hiding this comment

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

Just run npm toc

@@ -255,6 +255,9 @@ await testplane.readTests(testPaths, options);
* **ignore** (optional) `String|Glob|Array<String|Glob>` - patterns to exclude paths from the test search.
* **sets** (optional) `String[]`– Sets to run tests in.
* **grep** (optional) `RegExp` – Pattern that defines which tests to run.
* **replMode** (optional) `{enabled: boolean; beforeTest: boolean; onFail: boolean;}` - [Test development mode using REPL](./cli.md#--repl). When reading the tests, it checks that only one test is running in one browser.
Copy link
Member Author

Choose a reason for hiding this comment

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

it was not described earlier, so I added

@@ -8,6 +8,7 @@
- [Hooks](#hooks)
- [Skip](#skip)
- [Only](#only)
- [Also](#also)
Copy link
Member Author

Choose a reason for hiding this comment

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

added by script npm run toc

}

if (!config.ui || config.ui === "bdd") {
const pendingMethodName = `x${methodName}`;
Copy link
Member Author

Choose a reason for hiding this comment

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

In bdd interface support xdescribe and xit to skip suites/tests

const origStackTraceLimit = Error.stackTraceLimit;
const origPrepareStackTrace = Error.prepareStackTrace;

Error.stackTraceLimit = 2;
Copy link
Member Author

Choose a reason for hiding this comment

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

Use only 2 last messages from stack trace. First message it is this file. And second test file.


const location = obj.stack;
Error.stackTraceLimit = origStackTraceLimit;
Error.prepareStackTrace = origPrepareStackTrace;
Copy link
Member Author

Choose a reason for hiding this comment

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

return previous values

Error.stackTraceLimit = origStackTraceLimit;
Error.prepareStackTrace = origPrepareStackTrace;

inBus.once(eventName, runnable => {
Copy link
Member Author

Choose a reason for hiding this comment

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

subscribe one on add suite or test in order to save location to it

src/utils/cli.ts Outdated Show resolved Hide resolved
@DudaGod DudaGod force-pushed the HERMIONE-8.cli_cmd_to_read_tests branch 2 times, most recently from 3d6e420 to caaa36d Compare August 12, 2024 23:50
@DudaGod DudaGod force-pushed the HERMIONE-8.cli_cmd_to_read_tests branch 2 times, most recently from b9a8810 to 4087fd7 Compare September 3, 2024 15:15
@@ -255,6 +255,9 @@ await testplane.readTests(testPaths, options);
* **ignore** (optional) `String|Glob|Array<String|Glob>` - patterns to exclude paths from the test search.
* **sets** (optional) `String[]`– Sets to run tests in.
* **grep** (optional) `RegExp` – Pattern that defines which tests to run.
* **replMode** (optional) `{enabled: boolean; beforeTest: boolean; onFail: boolean;}` - [Test development mode using REPL](./cli.md#--repl). When reading the tests, it checks that only one test is running in one browser.
* **runnableOpts** (optional):
Copy link
Member Author

Choose a reason for hiding this comment

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

new option which gives me ability to save test locations in file (line and column)

@@ -317,6 +320,8 @@ TestCollection API:

* `eachRootSuite((root, browserId) => ...)` - iterates over all root suites in collection which have some tests.

* `format(formatterType)` - formats the tests in one of the available formatting types (`list` or `tree`). You can read more about the available formatting types [here](./cli.md#--formattername).
Copy link
Member Author

Choose a reason for hiding this comment

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

User can format test not only via cli command but also using testCollection.format method

const allTestsById = new Map<string, FormatterListTest>();

testCollection.eachTest((test, browserId) => {
if ((test as TestDisabled).disabled) {
Copy link
Member Author

Choose a reason for hiding this comment

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

should do nothing with disabled tests (for example for passive browsers)

}

if (allTestsById.has(test.id)) {
const foundTest = allTestsById.get(test.id)!;
Copy link
Member Author

Choose a reason for hiding this comment

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

for the same test in different browsers, I specify only the field browserIds. In order not to store too much of the same data (except for the browser name)

const treeTest = createTreeTest(test, browserId);
allTestsById.set(treeTest.id, treeTest);

collectSuites(test.parent!, treeTest, allSuitesById);
Copy link
Member Author

Choose a reason for hiding this comment

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

here, in order to create the right tree, I move from the tests (which can be disabled) up the parent suites.

const treeTest = allTestsById.get(test.id)!;

if (!treeTest.browserIds.includes(browserId)) {
treeTest.browserIds.push(browserId);
Copy link
Member Author

Choose a reason for hiding this comment

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

Like for list tests I just collect all browser ids for the same test

src/test-collection/formatters/tree.ts Show resolved Hide resolved
Copy link
Member

@shadowusr shadowusr left a comment

Choose a reason for hiding this comment

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

Great addition overall! 🔥

Looks good to me besides documentation bits. Didn't look at unit tests.

docs/cli.md Outdated Show resolved Hide resolved
docs/component-testing.md Outdated Show resolved Hide resolved
src/cli/commands/list-tests/index.ts Outdated Show resolved Hide resolved
src/test-collection/formatters/list.ts Show resolved Hide resolved
src/test-collection/index.ts Show resolved Hide resolved
src/utils/cli.ts Outdated Show resolved Hide resolved
src/test-reader/mocha-reader/utils.js Show resolved Hide resolved
src/test-collection/formatters/tree.ts Show resolved Hide resolved
@DudaGod DudaGod force-pushed the HERMIONE-8.cli_cmd_to_read_tests branch from 4087fd7 to 3d47d40 Compare September 9, 2024 21:01
@DudaGod DudaGod force-pushed the HERMIONE-8.cli_cmd_to_read_tests branch from 3d47d40 to ec7a5e8 Compare September 9, 2024 21:02
@DudaGod DudaGod merged commit aad3322 into master Sep 9, 2024
2 checks passed
@DudaGod DudaGod deleted the HERMIONE-8.cli_cmd_to_read_tests branch September 9, 2024 21:58
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