Skip to content

Commit

Permalink
Automate alert push notification tests (#1735)
Browse files Browse the repository at this point in the history
Automate alert push notification tests
  • Loading branch information
ptbrowne committed Jan 3, 2020
2 parents 8516ec2 + 1236764 commit 2ba497a
Show file tree
Hide file tree
Showing 7 changed files with 428 additions and 333 deletions.
47 changes: 8 additions & 39 deletions docs/dev.md
Expand Up @@ -29,9 +29,8 @@
- [Debug notification triggers](#debug-notification-triggers)
- [When creating a notification](#when-creating-a-notification)
- [End to end tests](#end-to-end-tests)
- [Possible problem when running E2E tests](#possible-problem-when-running-e2e-tests)
- [Alert rules](#alert-rules)
- [Semi automatic test](#semi-automatic-test)
- [Automatic tests](#automatic-tests)
- [Manual insertion test](#manual-insertion-test)
- [Misc](#misc)
- [Pouch On Web](#pouch-on-web)
Expand Down Expand Up @@ -401,30 +400,16 @@ ACH import test/fixtures/operations-notifs.json test/fixtures/helpers.js --url <

### End to end tests

#### Possible problem when running E2E tests

⚠️ When developing tests and launching them, you might encounter a weird error

```
toInteger is not a function
```

coming from lodash. We haven't found the root cause of the problem but a simple
touch on the file being launched solves it.

```
touch test/e2e/alerts.js; yarn test:e2e:alerts
```

#### Alert rules

##### Semi automatic test
##### Automatic tests

Alert rules are tested with a semi automatic test that
Alert rules are tested with automatic tests that

- Inserts data inside the local cozy-stack
- Launches the onOperationOrBillCreate service
- Checks on mailhog the emails received.
- Checks on mailhog the emails received
- Checks on a mock push server the push notifications received

```
$ export COZY_URL=http://cozy.tools:8080
Expand All @@ -439,33 +424,17 @@ $ yarn test:e2e:alerts
At the moment, it needs to be launched on the computer of the developer but should
be done on the CI in the future.

It is possible to test push notifications with the `--push` flag. You need first to
configure the stack so that it uses a fake server that will receive push notifications.
⚠️ For push notifications tests to work, you need first to configure the stack so that
it uses a fake server that will receive push notifications.

- Add this to `~/.cozy.yml`:
- Add this to your [stack config file](https://docs.cozy.io/en/cozy-stack/config/):

```
notifications:
android_api_key: 'fake_android_api_key'
fcm_server: 'http://localhost:3001'
```

- Start the fake push notification server. It will output the content of notifications,
allowing you to check the content. In the future, the E2E test should automatically
fire up this server and check by itself that the content of notifications corresponds
(like we do with Mailhog).

```
$ node test/e2e/fake-fcm-server
```

- Launch tests with the push flag

```
yarn test:e2e:alerts --push
```


##### Manual insertion test

To test on a real Cozy, a script can insert fake transactions on accounts corresponding
Expand Down
46 changes: 46 additions & 0 deletions jest.config.js
@@ -0,0 +1,46 @@
// Use JEST_PROJECT to switch between normal tests and end-to-end
// tests.
// See https://github.com/facebook/jest/issues/7542 for more information
// and to see if this features has been integrated into Jest directly.
const JEST_PROJECT = process.env.JEST_PROJECT

module.exports = {
moduleFileExtensions: ['js', 'jsx', 'json', 'styl'],
moduleDirectories: ['node_modules', '<rootDir>/src', '<rootDir>'],
moduleNameMapper: {
'\\.(png|gif|jpe?g|svg)$': '<rootDir>/test/__mocks__/fileMock.js',
styl$: 'identity-obj-proxy',
webapp$: 'identity-obj-proxy',
'!!raw-loader!(.*)': '$1',
css$: 'identity-obj-proxy',
'^cozy-client$': 'cozy-client/dist/index'
},
snapshotSerializers: ['enzyme-to-json/serializer'],
testPathIgnorePatterns: [
'node_modules',
'src/targets/mobile/',
JEST_PROJECT === 'e2e' ? null : '.*\\.e2e\\.spec\\.js'
].filter(Boolean),
testMatch: [
JEST_PROJECT === 'e2e' ? null : '**/?(*.)(spec).js?(x)',
JEST_PROJECT === 'e2e' ? '**/?(*.)(e2e.spec).js?(x)' : null
].filter(Boolean),
transform: {
'^.+\\.jsx?$': 'babel-jest',
'\\.css$': '<rootDir>/test/readFileESM.js',
'\\.styl$': '<rootDir>/test/readFileESM.js',
'\\.hbs$': '<rootDir>/test/readFileESM.js'
},
transformIgnorePatterns: [
'node_modules/(?!(cozy-harvest-lib|cozy-ui|cozy-client|cozy-notifications))'
],
globals: {
__ALLOW_HTTP__: false,
__TARGET__: 'browser',
__DEV__: false,
__POUCH__: false,
__SENTRY_TOKEN__: 'token',
cozy: {}
},
setupFiles: ['jest-localstorage-mock', './test/jest.setup.js']
}
55 changes: 1 addition & 54 deletions package.json
Expand Up @@ -29,7 +29,7 @@
"test:changes": "git diff --name-only master..HEAD | grep -E 'jsx?$' | xargs yarn test --runInBand --bail --findRelatedTests",
"test:e2e": "test:e2e:onOperationOrBillCreate && test:e2e:alerts",
"test:e2e:onOperationOrBillCreate": "node test/e2e/onOperationOrBillCreate.js",
"test:e2e:alerts": "BABEL_ENV=cli babel-node --ignore /some-fake-path test/e2e/alerts.js",
"test:e2e:alerts": "env JEST_PROJECT=e2e yarn jest",
"test:e2e:alerts-existing-cozy": "BABEL_ENV=cli babel-node --ignore /some-fake-path test/e2e/alerts-existing-cozy.js",
"check-fixtures": "node scripts/check-fixtures.js",
"find-circular-dependencies": "madge -b src/ --webpack-config webpack.config.js -c --extensions js,jsx src/main.jsx",
Expand Down Expand Up @@ -231,59 +231,6 @@
"webpack-bundle-analyzer": "3.4.1",
"whatwg-fetch": "3.0.0"
},
"jest": {
"moduleFileExtensions": [
"js",
"jsx",
"json",
"styl"
],
"moduleDirectories": [
"node_modules",
"<rootDir>/src",
"<rootDir>"
],
"moduleNameMapper": {
"\\.(png|gif|jpe?g|svg)$": "<rootDir>/test/__mocks__/fileMock.js",
"styl$": "identity-obj-proxy",
"webapp$": "identity-obj-proxy",
"!!raw-loader!(.*)": "$1",
"css$": "identity-obj-proxy",
"^cozy-client$": "cozy-client/dist/index"
},
"snapshotSerializers": [
"enzyme-to-json/serializer"
],
"testPathIgnorePatterns": [
"node_modules",
"src/targets/mobile/"
],
"transform": {
"^.+\\.jsx?$": "babel-jest",
"\\.css$": "<rootDir>/test/readFileESM.js",
"\\.styl$": "<rootDir>/test/readFileESM.js",
"\\.hbs$": "<rootDir>/test/readFileESM.js"
},
"transformIgnorePatterns": [
"node_modules/(?!(cozy-harvest-lib|cozy-ui|cozy-client|cozy-notifications))"
],
"globals": {
"__ALLOW_HTTP__": false,
"__TARGET__": "browser",
"__DEV__": false,
"__POUCH__": false,
"__SENTRY_TOKEN__": "token",
"cozy": {}
},
"setupFiles": [
"jest-localstorage-mock",
"./test/jest.setup.js"
],
"testMatch": [
"**/__tests__/**/*.js?(x)",
"**/?(*.)(spec).js?(x)"
]
},
"commitlint": {
"extends": [
"cozy"
Expand Down

0 comments on commit 2ba497a

Please sign in to comment.