Skip to content

Commit

Permalink
feat(testing): support positional arg for jest executor (#27328)
Browse files Browse the repository at this point in the history
<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior

The syntax to test a single file is different when using the jest
executor versus the jest cli

```sh
jest <file>
# vs
nx test project --testFile <file>
```

## Expected Behavior

I would expect the jest executor to support a single positional argument
as well

```sh
nx test project <file>
```

This is one of the common questions/complaints that I hear from
developers when transitioning from our standalone apps to our monorepo

This change supports both syntaxes so that this is not a breaking change
and prints a warning when both are used
  • Loading branch information
MJez29 authored Aug 13, 2024
1 parent 649905d commit b224e7c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
10 changes: 10 additions & 0 deletions docs/generated/packages/jest/documents/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,16 @@ To run Jest tests via nx use
nx test frontend
```

### Testing Specific Files

Using a single positional argument or the `--testFile` flag will run all test files matching the regex. For more info check out the [Jest documentation](https://jestjs.io/docs/cli#jest-regexfortestfiles).

```shell
nx test frontend HomePage.tsx
# or
nx test frontend --testFile HomePage.tsx
```

### Watching for Changes

Using the `--watch` flag will run the tests whenever a file changes.
Expand Down
3 changes: 2 additions & 1 deletion docs/generated/packages/jest/executors/jest.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@
},
"testFile": {
"description": "The name of the file to test.",
"type": "string"
"type": "string",
"$default": { "$source": "argv", "index": 0 }
},
"tsConfig": {
"description": "The name of the Typescript configuration file.",
Expand Down
10 changes: 10 additions & 0 deletions docs/shared/packages/jest/jest-plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,16 @@ To run Jest tests via nx use
nx test frontend
```

### Testing Specific Files

Using a single positional argument or the `--testFile` flag will run all test files matching the regex. For more info check out the [Jest documentation](https://jestjs.io/docs/cli#jest-regexfortestfiles).

```shell
nx test frontend HomePage.tsx
# or
nx test frontend --testFile HomePage.tsx
```

### Watching for Changes

Using the `--watch` flag will run the tests whenever a file changes.
Expand Down
6 changes: 5 additions & 1 deletion packages/jest/src/executors/jest/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@
},
"testFile": {
"description": "The name of the file to test.",
"type": "string"
"type": "string",
"$default": {
"$source": "argv",
"index": 0
}
},
"tsConfig": {
"description": "The name of the Typescript configuration file.",
Expand Down

0 comments on commit b224e7c

Please sign in to comment.