Skip to content

Commit

Permalink
Merge pull request #29 from MSA-Safety/feat/demo
Browse files Browse the repository at this point in the history
test: Add demo project for testing and showcasing the package
  • Loading branch information
rimesime authored Feb 18, 2023
2 parents 7e3348b + 8a4b877 commit 60a4f68
Show file tree
Hide file tree
Showing 13 changed files with 7,575 additions and 8,430 deletions.
28 changes: 14 additions & 14 deletions .github/workflows/test_and_publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,17 @@ jobs:
with:
category: "/language:javascript"

publish:
name: Publish to NPM
if: ${{ github.ref == 'refs/heads/main' }}
runs-on: ubuntu-latest
needs: [test, analyze]
steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
- run: npm run semantic-release
# publish:
# name: Publish to NPM
# if: ${{ github.ref == 'refs/heads/main' }}
# runs-on: ubuntu-latest
# needs: [test, analyze]
# steps:
# - uses: actions/checkout@v3
# - name: Use Node.js 18.x
# uses: actions/setup-node@v3
# with:
# node-version: 18.x
# cache: 'npm'
# - run: npm ci
# - run: npm run semantic-release
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v18
73 changes: 73 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ Add this package to your `jest.config.js`:
]
```

## Jest Return Code Behavior
After successfully retrying a configured flaky test, Jest will return with a
non-zero exit code. However, the junit report shows no failures.

## Configuration Options
**`testFilePath`** (string)
- Relative path to test file
Expand Down Expand Up @@ -79,6 +83,75 @@ Add this package to your `jest.config.js`:
```
Please see additional example configurations in `jest.flakyRetry.config.example.json`.

# Demo
Please have a look at the `demo` folder.

The node demo application shows a basic configuration of jest-flaky-retry. The output below shows the
test execution:
- The first run of the test failed.
- Due to the thrown error being configured in the jest-flaky-retry configuration file, jest-flaky-retry performed retry attempt.
- The successful retry results in a failure-free junit output.
- Jest returns exit code 1, due to failed test runs.

Please note: this is not intended to represent a perfect example where this package might be helpful. More
appropriate usages might be e.g. network flakiness in integration and e2e tests.

```shell
$ cd demo
$ npm run test:unit # failure-free junit output; jest will return with exit code 1 nonetheless

> jest-flaky-retry-demo@0.0.1 test:unit
> jest --config jest.unit.config.js

Determining test suites to run...Loading list of known flaky tests from jest.unit.flakyRetry.json
[ { failureMessages: [ 'Random Flaky Error' ] } ]
FAIL ./demo.unit.test.js
demo
✕ should fail on first test run and succeed on subsequent run (1 ms)

● demo › should fail on first test run and succeed on subsequent run

expect(received).toBeUndefined()

Received: [Error: Random Flaky Error]

11 | caughtError = error;
12 | }
> 13 | expect(caughtError).toBeUndefined();
| ^
14 | });
15 | });
16 |

at Object.toBeUndefined (demo.unit.test.js:13:25)

Retrying test cases: [ 'demo should fail on first test run and succeed on subsequent run' ]
PASS ./demo.unit.test.js
demo
✓ should fail on first test run and succeed on subsequent run (1 ms)

Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 0.139 s, estimated 1 s
Ran all test suites with tests matching "demo should fail on first test run and succeed on subsequent run".
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 0.521 s, estimated 1 s
```
Content of junit.xml output:
```xml
<?xml version="1.0" encoding="UTF-8"?>
<testsuites name="jest tests" tests="1" failures="0" errors="0" time="0.52">
<testsuite name="demo" errors="0" failures="0" skipped="0" timestamp="9999-99-99T00:00:00" time="0.123" tests="1">
<testcase classname="demo should fail on first test run and succeed on subsequent run" name="demo should fail on first test run and succeed on subsequent run" time="0.001">
</testcase>
</testsuite>
</testsuites>
```
# License
This is free software, distributed under the [ISC license](https://opensource.org/licenses/ISC).
30 changes: 30 additions & 0 deletions demo/demo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

const fs = require('fs');

/**
* Main function. Writes file if it does not exist yet and throws.
* Succeeds and deletes file if file exists.
*
* Ergo: first call fails - second call succeeds.
*
* Please note: this is not intended to represent a good example where
* this package might be helpful. More appropriate usages are network
* flakiness in integration/e2e tests.
*
* @throws {Error} If file did not exist.
*/
function demo() {
const path = './demo-unit-text.txt';

if (!fs.existsSync(path)) {
// File does not exist yet, create file and throw error
fs.writeFileSync(path, '0');
throw new Error('Random Flaky Error');
}

// File exists, delete it and be happy
fs.rmSync(path);
}

module.exports = { demo };
15 changes: 15 additions & 0 deletions demo/demo.unit.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

const { demo } = require('./demo');

describe('demo', () => {
it('should fail on first test run and succeed on subsequent run', () => {
let caughtError;
try {
demo();
} catch (error) {
caughtError = error;
}
expect(caughtError).toBeUndefined();
});
});
32 changes: 32 additions & 0 deletions demo/jest.unit.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* NodeJS unit test Jest config.
*/

'use strict';

module.exports = {
coverageDirectory: 'build/coverage/unit',
coveragePathIgnorePatterns: ['node_modules'],
coverageThreshold: {
global: {
branches: 95,
functions: 95,
lines: 95,
statements: 95,
},
},
moduleNameMapper: {},
reporters: [
'default',
[
'<rootDir>/..',
{
configFile: 'jest.unit.flakyRetry.json',
junitOutputDirectory: 'build/results/unit',
},
],
],
roots: ['<rootDir>'],
testEnvironment: 'node',
testMatch: ['**/__tests__/**/*.js?(x)', '**/?(*.)+(unit.test).js?(x)'],
};
7 changes: 7 additions & 0 deletions demo/jest.unit.flakyRetry.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"failureMessages": [
"Random Flaky Error"
]
}
]
Loading

0 comments on commit 60a4f68

Please sign in to comment.