Skip to content
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

[CI] Enforce linting and prettier rules #1103

Merged
merged 9 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
7 changes: 6 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
module.exports = {
root: true,
rules: {
'no-extra-boolean-cast': false,
'no-extra-boolean-cast': 0,
'quotes': [
'error',
'single',
{ 'avoidEscape': true, 'allowTemplateLiterals': true }
]
},
extends: [
'eslint:recommended',
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ jobs:
- name: "[Setup] Install dependencies"
run: yarn
- name: "[Test] Run linters"
continue-on-error: true
run: yarn lint
- name: "[Test] Run all jest tests"
run: yarn test
6 changes: 5 additions & 1 deletion __test__/jest.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ declare namespace jest {
* @param method The method to stub.
* @param returnValue The value to return.
*/
stub: (obj: any, method: string, returnValue?: any) => jest.SpyInstance<any>;
stub: (
obj: any,
method: string,
returnValue?: any,
) => jest.SpyInstance<any>;
fail: (message?: string) => void;
nock: (responseBody: any, status?: number) => void;
}
Expand Down
21 changes: 16 additions & 5 deletions __test__/jest/README
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# Basic Test

Here is a basic test file template.

```ts
import { TestEnvironment } from "../../support/environment/TestEnvironment";
import { TestEnvironment } from '../../support/environment/TestEnvironment';

// mock an entire file
jest.mock("../../../src/MyFile");
jest.mock('../../../src/MyFile');

describe('My tests', () => {
beforeEach(() => {
Expand All @@ -19,45 +20,55 @@ describe('My tests', () => {

test('This is a test description', () => {});
});

```

# Common Usages

## Suppress Internal Logging

Your test may result in an error being printed but the test still succeeds. To suppress logs, you can mock the entire Log file.

```ts
// suppress all internal logging
jest.mock("../../../src/shared/libraries/Log");
jest.mock('../../../src/shared/libraries/Log');
```

# Jest Customizations

This directory includes changes used for extending Jest to do things like stub and spy on private functions.

Followed instructions from [here](https://spin.atomicobject.com/2020/01/30/jest-add-custom-functions/).

To add more custom testing functions, add them to `jest/jest.setupt.ts` and `jest.d.ts`.

## API

### `stub`

Used to stub a class function with the given mock return value.

```ts
test.stub(MyClass.prototype, 'myFunction', {});
```

### `fail`

Jest does not have a way to force fail a function.

```ts
test.fail();
```

### `nock`

Mocks all HTTP requests by replacing the `fetch` function. If omitted, the status defaults to 200.

```ts
test.nock({}, 404)
test.nock({}, 404);
```

# Troubleshooting

## `jest.spyOn()` vs `test.stub()`

`test.stub()`'s internal implementation is the same as `jest.spyOn()` with the difference that the `stub` function has the option to set the mock return value. **Warning**: setting the mock return value can change the behavior of nested functions. For example, if an outer function calls a function you expect to be called once -- and you set a mock return value on the outer function, your test comparison may fail.
4 changes: 2 additions & 2 deletions __test__/jest/jest.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ test.stub = (obj: any, method: string, returnValue?: any) => {

test.fail = (message?: string) => {
throw new Error(message || 'Test failed');
}
};

test.nock = (responseBody: any, status = 200) => {
global.fetch = jest.fn().mockResolvedValue({
status,
json: jest.fn().mockResolvedValue(responseBody),
});
}
};

// workaround for open upstream issue: https://github.com/inrupt/solid-client-authn-js/issues/1676
import { TextEncoder, TextDecoder } from 'util';
Expand Down
7 changes: 6 additions & 1 deletion __test__/jest/jest.setupfiles.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
process.on('unhandledRejection', (reason, p) => {
console.log('jest.setupfiles.ts: unhandledRejection: Unhandled Rejection at: Promise', p, 'reason:', reason);
console.log(
'jest.setupfiles.ts: unhandledRejection: Unhandled Rejection at: Promise',
p,
'reason:',
reason,
);
});
86 changes: 44 additions & 42 deletions __test__/support/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,50 +17,52 @@ export const OPERATION_QUEUE_TIME_ADVANCE = 5001;
export const DELTA_QUEUE_TIME_ADVANCE = 1001;

/* S T R I N G C O N S T A N T S */
export const APP_ID = "34fcbe85-278d-4fd2-a4ec-0f80e95072c5";
export const APP_ID = '34fcbe85-278d-4fd2-a4ec-0f80e95072c5';

export const DUMMY_PUSH_TOKEN = "https://fcm.googleapis.com/fcm/send/01010101010101";
export const DUMMY_ONESIGNAL_ID = "1111111111-2222222222-3333333333";
export const DUMMY_EXTERNAL_ID = "rodrigo";
export const DUMMY_EXTERNAL_ID_2 = "iryna";
export const DUMMY_SUBSCRIPTION_ID = "4444444444-5555555555-6666666666";
export const DUMMY_MODEL_ID = "0000000000";
export const DUMMY_PUSH_TOKEN =
'https://fcm.googleapis.com/fcm/send/01010101010101';
export const DUMMY_ONESIGNAL_ID = '1111111111-2222222222-3333333333';
export const DUMMY_EXTERNAL_ID = 'rodrigo';
export const DUMMY_EXTERNAL_ID_2 = 'iryna';
export const DUMMY_SUBSCRIPTION_ID = '4444444444-5555555555-6666666666';
export const DUMMY_MODEL_ID = '0000000000';

/* REQUEST CONSTANTS */
export const DUMMY_GET_USER_REQUEST_WITH_PUSH_SUB = {
"result": {
"properties": {
"language": "en",
"timezone_id": "America/New_York",
"first_active": 1689826588,
"last_active": 1689826588
},
"identity": {
"external_id": DUMMY_EXTERNAL_ID,
"onesignal_id": DUMMY_ONESIGNAL_ID,
},
"subscriptions": [
{
"id": DUMMY_SUBSCRIPTION_ID,
"app_id": APP_ID,
"type": "ChromePush",
"token": DUMMY_PUSH_TOKEN,
"enabled": false,
"notification_types": -2,
"session_time": 0,
"session_count": 1,
"sdk": "160000",
"device_model": "MacIntel",
"device_os": "114",
"rooted": false,
"test_type": 0,
"app_version": "",
"net_type": 0,
"carrier": "",
"web_auth": "R5dzF/EvmUbv0IsM3Ria7g==",
"web_p256": "BNWNgguO0F+id4MjCW2V98cwPiXHs0XyPUOCqlU0OgyqG4W9V3H1R799goSjSSgZ0CMI+7/nZYiVl1bB8ZnDZx0="
}
]
result: {
properties: {
language: 'en',
timezone_id: 'America/New_York',
first_active: 1689826588,
last_active: 1689826588,
},
"status": 200
}
identity: {
external_id: DUMMY_EXTERNAL_ID,
onesignal_id: DUMMY_ONESIGNAL_ID,
},
subscriptions: [
{
id: DUMMY_SUBSCRIPTION_ID,
app_id: APP_ID,
type: 'ChromePush',
token: DUMMY_PUSH_TOKEN,
enabled: false,
notification_types: -2,
session_time: 0,
session_count: 1,
sdk: '160000',
device_model: 'MacIntel',
device_os: '114',
rooted: false,
test_type: 0,
app_version: '',
net_type: 0,
carrier: '',
web_auth: 'R5dzF/EvmUbv0IsM3Ria7g==',
web_p256:
'BNWNgguO0F+id4MjCW2V98cwPiXHs0XyPUOCqlU0OgyqG4W9V3H1R799goSjSSgZ0CMI+7/nZYiVl1bB8ZnDZx0=',
},
],
},
status: 200,
};
Loading
Loading