Skip to content

Commit

Permalink
Add: Support running tests in a working directory (#7)
Browse files Browse the repository at this point in the history
* Add working-directory input to run tests in a subdirectory
  • Loading branch information
crucialfelix authored Jul 3, 2020
1 parent a144bf5 commit 43bbcd0
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,17 @@ with:
coverage-comment: false
```

### Running tests in a subdirectory

For running tests in folders other than root, supply a working-directory.

```yaml
uses: mattallty/jest-github-action@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
working-directory: "frontend"
```


See the [actions tab](https://github.com/mattallty/jest-github-action/actions) for runs of this action! :rocket:
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ inputs:
description: "The test command to run"
required: false
default: "npm test"
working-directory:
description: "Subdirectory to run tests in"
required: false
default: ""
coverage-comment:
description: "Comment PRs with code coverage"
required: false
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

14 changes: 8 additions & 6 deletions src/action.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { sep, join } from "path"
import { sep, join, resolve } from "path"
import { readFileSync } from "fs"
import { exec } from "@actions/exec"
import * as core from "@actions/core"
Expand All @@ -16,7 +16,9 @@ const ACTION_NAME = "jest-github-action"
const COVERAGE_HEADER = ":loop: **Code coverage**\n\n"

export async function run() {
const CWD = process.cwd() + sep
let workingDirectory = core.getInput("working-directory", { required: false })
let cwd = workingDirectory ? resolve(workingDirectory) : process.cwd()
const CWD = cwd + sep
const RESULTS_FILE = join(CWD, "jest.results.json")

try {
Expand All @@ -29,7 +31,7 @@ export async function run() {

const cmd = getJestCommand(RESULTS_FILE)

await execJest(cmd)
await execJest(cmd, CWD)

// octokit
const octokit = new GitHub(token)
Expand Down Expand Up @@ -166,12 +168,12 @@ function parseResults(resultsFile: string): FormattedTestResults {
return results
}

async function execJest(cmd: string) {
async function execJest(cmd: string, cwd?: string) {
try {
await exec(cmd, [], { silent: true })
await exec(cmd, [], { silent: true, cwd })
console.debug("Jest command executed")
} catch (e) {
console.debug("Jest execution failed. Tests have likely failed.")
console.error("Jest execution failed. Tests have likely failed.", e)
}
}

Expand Down

0 comments on commit 43bbcd0

Please sign in to comment.