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

chore: Use correct config file to run lint on test files and refactor test code #165

Merged
merged 3 commits into from
May 24, 2022

Conversation

adebayor123
Copy link
Member

Issue

Previously, we did not run lint on test files. After the addition of #144 and #149 , we are running lint on staged files and the input for --project was tsconfig.webpack.json. Even though tsconfig.webpack.json extended from tsconfig.json which defined project as all the files under src/, tsconfig.webpack.json overwrote the project to only include dependencies involved with index-browser.ts.

As a result, any changes/addition of test files would fail in commit sessions, as it was recognized as "not included in the project".

Solution

There were two ways to go about this issue: (1) do not include test files (non-source code) under pre-commit (2) use tsconfig.json as input for --project and refactor our test cases.

Despite being test files, applying lint to test cases in the future and doing the refactoring will increase code quality - thus went with option (2).

This PR

This PR makes two major changes:

  1. instead of tsconfig.webpack.json, use tsconfig.json for npm run lint
  2. refactor existing test cases

Notes

Since TSLint is deprecated as of 2018, we should migrate to ESLint. There will be a subsequent PR to migrate from TSLint to ESLint.


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

for (let i = 0; i < events.length; i++) {
let eventType = events[i].type;
let eventDetails = JSON.parse(events[i].details);
for (const event of events) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This not not so safe to do with arrays

Consider using Array.forEach

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, could you explain why this isn't a safe operation with arrays? This was recommended by TSLint and I'm not able to find much information on why forEach is preferred - although according to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of, it has compatibility issues with IE, so I will go ahead and make the change.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forget the reasons 100%, but I believe in some cases (and some browsers) for..of returns the length value as well.

for (let i = 0; i < record.length; i++) {
expect(record.mock.calls[i][0]).toEqual(DOM_EVENT_TYPE);
expect(record.mock.calls[i][1]).toMatchObject(
for (const call of record.mock.calls) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above

Please use forEach

@@ -85,8 +87,8 @@ export const isEachEventIngested = async (
// Running tests in parallel require pagination logic, as several test cases have the same timestamp
while (true) {
const data = await rumClient.send(command);
for (let i = 0; i < data.Events.length; i++) {
ingestedEvents.add(JSON.parse(data.Events[i]).event_id);
for (const event of data.Events) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as all other comments related to for of

@@ -12,7 +12,7 @@ const getSession = jest.fn(() => ({
eventCount: 1
}));
const getUserId = jest.fn(() => 'b');
const getAttributes = jest.fn(() => {});
const getAttributes = jest.fn(() => undefined);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think the intent with {} (here and elsewhere) was to return an empty object.

Clearly it's not being used though... so could even just do

const getAttributes = jest.fn();

or omit getAttributes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

() => {} does not return an empty object, it returns undefined

In order to return an empty object one has to do: () => ({}) (note the extra parenthesis)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, apparently {} leads to undefined (and unit tests prove the case to be so as well since all were passing).
I will do the following:

  1. first try out using () => ({}) to create an empty object as maniator@ suggests, since that was our original intention.
  2. remove unused references.

@adebayor123
Copy link
Member Author

TODO: before merging, rename commit to be less than 50 characters

for (let i = 0; i < events.length; i++) {
let eventType = events[i].type;
let eventDetails = JSON.parse(events[i].details);
for (const event of events) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: use forEach

for (let i = 0; i < events.length; i++) {
let eventType = events[i].type;
let eventDetails = JSON.parse(events[i].details);
for (const event of events) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: use forEach

for (let i = 0; i < events.length; i++) {
let eventType = events[i].type;
let eventDetails = JSON.parse(events[i].details);
for (const event of events) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: use forEach

for (let i = 0; i < events.length; i++) {
let eventType = events[i].type;
let eventDetails = JSON.parse(events[i].details);
for (const event of events) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: use forEach

for (let i = 0; i < events.length; i++) {
let eventType = events[i].type;
let eventDetails = JSON.parse(events[i].details);
for (const event of events) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: use forEach

for (let i = 0; i < record.mock.calls.length; i++) {
expect(record.mock.calls[i][0]).toEqual(DOM_EVENT_TYPE);
expect(record.mock.calls[i][1]).toMatchObject(
for (const call of record.mock.calls) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: use forEach

for (let i = 0; i < record.length; i++) {
expect(record.mock.calls[i][0]).toEqual(DOM_EVENT_TYPE);
expect(record.mock.calls[i][1]).toMatchObject(
for (const call of record.mock.calls) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: use forEach

for (let i = 0; i < eventIds.length; i++) {
if (!ingestedEvents.has(eventIds[i])) {
throw new Error(`Event ${eventIds[i]} not ingested.`);
for (const eventId of eventIds) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: use forEach

@adebayor123 adebayor123 merged commit ae60f2a into aws-observability:main May 24, 2022
@adebayor123 adebayor123 deleted the fix-lint branch May 24, 2022 20:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants