Skip to content

Commit

Permalink
Feature/issues/137/suppress reporting when no tests run (#138)
Browse files Browse the repository at this point in the history
* fix: Handle error when reading or parsing JSON file

* suppress reporter when no tests found

* version bump
  • Loading branch information
ryanrosello-og authored May 31, 2024
1 parent b3dd655 commit 227544a
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"cli-debug": "yarn build && npx . -c ./cli_config.json -j ./tests/test_data/valid_test_results.json"
},
"name": "playwright-slack-report",
"version": "1.1.80",
"version": "1.1.81",
"bin": {
"playwright-slack-report": "dist/cli.js"
},
Expand Down
12 changes: 10 additions & 2 deletions src/ResultsParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,16 @@ export default class ResultsParser {
}

async parseFromJsonFile(filePath: string) {
const data = fs.readFileSync(filePath, 'utf-8');
const parsedData: JSONResult = JSON.parse(data);
let data: string;
let parsedData: JSONResult;
try {
data = fs.readFileSync(filePath, 'utf-8');
parsedData = JSON.parse(data);
} catch (error) {
throw new Error(
`Error reading or parsing JSON file [${filePath}]: \n\t${error}`,
);
}

const retries = parsedData.config.projects[0]?.retries || 0;
for (const suite of parsedData.suites) {
Expand Down
12 changes: 12 additions & 0 deletions src/SlackReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,18 @@ class SlackReporter implements Reporter {
return;
}

if (
resultSummary.passed === 0
&& resultSummary.failed === 0
&& resultSummary.flaky === 0
&& resultSummary.skipped === 0
&& resultSummary.failures.length === 0
&& resultSummary.tests.length === 0
) {
this.log('⏩ Slack reporter - Playwright reported : "No tests found"');
return;
}

const agent = this.proxy ? new HttpsProxyAgent(this.proxy) : undefined;

if (this.slackWebHookUrl) {
Expand Down
14 changes: 14 additions & 0 deletions tests/ResultsParser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,20 @@ test.describe('ResultsParser', () => {
expect(resultSummary.tests.length).toEqual(10);
});

test('throw an error when the results file is not a valid json', async ({}) => {
const resultsParser = new ResultsParser();
const validTestResults = path.join(
__dirname,
'test_data',
'invalid_json_results.json',
);
try {
await resultsParser.parseFromJsonFile(validTestResults);
} catch (error) {
expect(error.toString()).toContain('Error: Error reading or parsing JSON file');
}
});

test('parse test results from a complicated json file', async ({}) => {
const resultsParser = new ResultsParser();
const validTestResults = path.join(
Expand Down
Empty file.

0 comments on commit 227544a

Please sign in to comment.