-
-
Notifications
You must be signed in to change notification settings - Fork 144
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
Dont work test-script with jest --changedSince #352
Comments
I would appreciate your help @ArtiomTr |
Hey @CarlFMateus 👋, Could you please send your |
|
What I try to do is make the coverage only for files that come from the pull request. @ArtiomTr |
@CarlFMateus try this workflow: name: Unit Tests
run-name: ${{ github.actor }} is execute test coverage 😎
on:
pull_request:
branches:
- "master"
types: [opened, reopened, synchronize]
jobs:
tests:
if: startsWith(github.head_ref, 'feature')
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read
checks: write
pull-requests: write
strategy:
matrix:
node-version: [16.x]
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0
- run: |
git fetch --no-tags --depth=1 origin master
git checkout -b master
git checkout ${{ github.event.pull_request.head.sha }}
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{matrix.node-version}}
cache: "yarn"
- name: Install dependencies
run: yarn install --frozen-lockfile
- name: Do you have a unit test?
uses: actions/github-script@v6
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
const number = await context.payload.pull_request.number
const files = await github.rest.pulls.listFiles({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: number
})
const regex = /.*(spec|test).(ts|js|tsx)$/
const isShow = files.data.some(file => regex.test(file.filename) )
if(!isShow){
await github.rest.checks.create({
owner: context.repo.owner,
repo: context.repo.repo,
status: 'completed',
name: 'Failed Validation',
head_sha: context.payload.pull_request.head.sha || context.sha,
conclusion: 'failure',
output: {
title: 'Failed Validation',
text: 'Fallo porque no cumplio con las validación minima',
summary: 'Debe contener al menos una prueba'
}
})
core.setFailed('Debe contener al menos una prueba')
}
await github.rest.checks.create({
owner: context.repo.owner,
repo: context.repo.repo,
status: 'completed',
name: 'Successful Validation',
head_sha: context.payload.pull_request.head.sha || context.sha,
conclusion: 'success',
output: {
title: 'Successful Validation',
text: 'Contiene al menos una prueba unitaria',
summary: 'Debe contener al menos una prueba'
}
})
- name: Run tests
run: yarn jest --changedSince=origin/master --ci --json --coverage --testLocationInResults --outputFile=report.json
- name: Coverage Report
uses: ArtiomTr/jest-coverage-report-action@v2
with:
coverage-file: report.json
base-coverage-file: report.json
annotations: failed-tests |
I'm going to try and whatever I keep commenting on this issue, please don't close the issue. Thanks for your help, I'm going to try. |
Thank you very much if it works, I'll keep testing. Really thank you very much for the help. @ArtiomTr |
Sorry, I had to change the code and I'm doing it this way now, executing the jest command in the github script, it runs but doesn't generate the report.json file. Do you suddenly know why this happens? @ArtiomTr I would really appreciate it if you suddenly know the problem, otherwise there is no problem.
|
@CarlFMateus, I don't know why it does not work, but I have a few notices:
Also, I've noticed that you're redirecting all output to the console. GitHub already provides a dedicated npm package to tackle this problem - @actions/exec. It also has a really handy promise-based API. So, I think that something like this should work: name: Unit Tests
# Esto es una prueba
run-name: ${{ github.actor }} is execute test coverage 😎
on:
pull_request:
branches:
- "master"
types: [opened, reopened, synchronize]
jobs:
tests:
if: startsWith(github.head_ref, 'feature')
runs-on: ubuntu-latest
permissions: write-all
strategy:
matrix:
node-version: [16.x]
steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{matrix.node-version}}
- name: Install dependencies
run: yarn install --frozen-lockfile
- name: Do you have a unit test?
uses: actions/github-script@v6
with:
script: |
const number = await context.payload.pull_request.number;
const files = await github.rest.pulls.listFiles({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: number,
});
const regex = /.*(spec|test).(ts|js|tsx)$/;
const isShow = files.data.some((file) => regex.test(file.filename));
if (!isShow) {
core.setFailed("Debe contener al menos una prueba");
}
const cadena = files.data.reduce((acc, element) => {
if(regex.test(element.filename)) {
acc.push(element.filename)
}
return acc
}, []);
// `exec` function is from @actions/exec, it is automatically provided by actions/github-script,
// just like `context` or `github` variables.
await exec.exec('jest', [...cadena, '--ci', '--json', '--coverage', '--testLocationInResults', '--outputFile=report.json']);
- name: Coverage Report
if: ${{ failure() || success() }}
uses: ArtiomTr/jest-coverage-report-action@v2
with:
coverage-file: report.json
base-coverage-file: report.json
annotations: failed-tests |
Thank you very much for the help, really thanks for taking the time to analyze the code and find improvements. This interests me a lot since I am implementing your action and I found it very interesting. Thank you very much for the help. |
Thanks @ArtiomTr for the help, it really worked for me, although if you want I can correct something I found regarding actions/exec. According to the github/exec documentation
Thank you very much for so much help, I just wanted to ask you a favor if you suddenly know. Do you know if with jest you can execute the coverage of multiple files but very specific. I saw that the This you know would contribute to many developers 😃. |
@CarlFMateus, thank you for pointing out a mistake, I don't often use I don't know precisely how jest CLI handles arrays, but based on my personal experience:
I am pretty sure that one of these variants will work. If not, most likely that jest doesn't allow you to pass arrays as CLI arguments. That would be a great possibility to contribute to a popular tool! 😃 |
Thanks for your help, I did it with the first step that you showed and it worked for me, although it does coverage with more files, it worked for me that way. Thank you for taking the time to help me and offer some alternatives as soon as I code. 😃 |
I wanted to ask you, sorry for so much trouble in the code that I'm sharing, I'm adding a flag that is this In this file I am doing the following configuration.
Or you would have to add the coverageThreshold in the cli like this: It is taking the metrics from the root file |
@CarlFMateus, are you having trouble with the jest itself, or with the action? I'm asking because I think in this case, jest works correctly (to ensure that, you can check your action console and see if you get any errors). Most likely, this is a bug with this action. It is always reading the jest-coverage-report-action/src/utils/parseJestConfig.ts Lines 6 to 9 in df2b025
(That's just a fancy configuration loader, it will just check jest.config.* with all available extensions)
I'll try to add the option for specifying custom jest configuration. |
Following the code that we are seeing in this issue I am trying to add the config flag by specifying this Is there a way to use a different configuration file than If you could establish a different jest configuration to the default configuration, it would help me a lot. |
Describe a bug
The test-script command does not work with this example.
yarn jest --changedSince=origin/master
Because it can't work with this command. Could you help me with this, what would be the best solution. Thank you so much
Expected behavior
For it to work with this type of command, the expected behavior is to perform the tests only for specific files.
Details
Action version:
OS, where your action is running (windows, linux):
action.yml file
Screenshots
Additional context
The text was updated successfully, but these errors were encountered: