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

feat: added specs #2

Merged
merged 1 commit into from
Oct 2, 2024
Merged

feat: added specs #2

merged 1 commit into from
Oct 2, 2024

Conversation

wolivera
Copy link
Contributor

@wolivera wolivera commented Oct 2, 2024

Summary (generated)

Release Notes:

  • New Feature: Exported app object for improved testability
  • Test: Added comprehensive unit tests for various endpoints
    • Root path
    • Metrics
    • Ping
    • Error handling
    • Users endpoint

These changes enhance the application's reliability and maintainability by enabling more effective testing. While not directly visible to end-users, this improvement will lead to a more stable and robust application, ultimately benefiting the user experience.

Copy link

github-actions bot commented Oct 2, 2024

Walkthrough

This pull request enhances the application's testability by exporting the app object in index.js. Additionally, it introduces comprehensive unit tests in tests/index.test.js, covering various endpoints and functionalities. These changes improve the overall quality and reliability of the application by enabling more effective testing and ensuring proper functionality of different routes and error handling.

Changes

Files Summary
index.js Exports the app object for testing purposes.
tests/index.test.js Adds unit tests for various endpoints, including root path, metrics, ping, error handling, and users endpoint.

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Commits Files that changed from the base of the PR and between 5f859d1 and c267d38 commits.
Files selected (2)
  • index.js (1)
  • tests/index.test.js (1)
Files ignored due to filter (2)
  • package-lock.json
  • package.json
Review comments generated (4)
  • Review: 4
  • LGTM: 0

Tips

Chat with AI reviewer (/reviewbot)

  • Reply on review comments left by this bot to ask follow-up questions. A review comment is a comment on a diff or a file.
  • Invite the bot into a review comment chain by tagging /reviewbot in a reply.

Code suggestions

  • The bot may make code suggestions, but please review them carefully before committing since the line number ranges may be misaligned.
  • You can edit the comment made by the bot and manually tweak the suggestion if it is slightly off.

Pausing incremental reviews

  • Add /reviewbot: ignore anywhere in the PR description to pause further reviews from the bot.

@@ -0,0 +1,47 @@
const request = require('supertest');
const app = require('../index'); // Assuming you export the app from index.js
Copy link

Choose a reason for hiding this comment

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

Consider using an absolute path for importing the app to avoid potential issues with relative paths:

const app = require(path.join(__dirname, '../index'));

Don't forget to import the path module at the top of the file:

const path = require('path');


describe('GET /', () => {
it('should return a welcome message', async () => {
const res = await request(app).get('/');
Copy link

Choose a reason for hiding this comment

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

For consistency and to avoid potential issues with test isolation, consider using app.listen() instead of directly passing the app to supertest:

const server = app.listen();
const res = await request(server).get('/');

Remember to close the server after the test:

aftereAll(() => {
  server.close();
});

it('should return metrics', async () => {
const res = await request(app).get('/metrics');
expect(res.statusCode).toEqual(200);
expect(res.headers['content-type']).toMatch(/text\/plain/);
Copy link

Choose a reason for hiding this comment

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

The content type check could be more specific. Consider using an exact match:

expect(res.headers['content-type']).toBe('text/plain; charset=utf-8');

Comment on lines +40 to +45
expect(res.body).toHaveProperty('users');
expect(res.body.users).toEqual([
{ name: 'Alice', age: 25 },
{ name: 'Bob', age: 30 },
{ name: 'Charlie', age: 35 },
]);
Copy link

@github-actions github-actions bot Oct 2, 2024

Choose a reason for hiding this comment

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

Instead of hardcoding the expected user data, consider using more flexible assertions:

expect(res.body).toHaveProperty('users');
expect(Array.isArray(res.body.users)).toBe(true);
expect(res.body.users.length).toBeGreaterThan(0);
expect(res.body.users[0]).toHaveProperty('name');
expect(res.body.users[0]).toHaveProperty('age');

Copy link

Choose a reason for hiding this comment

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

@wolivera Certainly! Here's an example of how you can modify the test for the GET /users endpoint using more flexible assertions:

describe('GET /users', () => {
    it('should return a list of users', async () => {
        const res = await request(app).get('/users');
        expect(res.statusCode).toEqual(200);
        expect(res.body).toHaveProperty('users');
        expect(Array.isArray(res.body.users)).toBe(true);
        expect(res.body.users.length).toBeGreaterThan(0);
        
        const firstUser = res.body.users[0];
        expect(firstUser).toHaveProperty('name');
        expect(typeof firstUser.name).toBe('string');
        expect(firstUser).toHaveProperty('age');
        expect(typeof firstUser.age).toBe('number');
    });
});

This example demonstrates more flexible assertions that:

  1. Check if the response has a 'users' property
  2. Verify that 'users' is an array
  3. Ensure the array is not empty
  4. Check if the first user has 'name' and 'age' properties
  5. Validate the types of 'name' (string) and 'age' (number)

This approach allows for changes in the specific user data without breaking the test, as long as the overall structure remains consistent.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

/reviewbot show me an example

Copy link

Choose a reason for hiding this comment

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

@wolivera Certainly! Here's an example of how the modified test for the GET /users endpoint would look with more flexible assertions:

describe('GET /users', () => {
    it('should return a list of users', async () => {
        const res = await request(app).get('/users');
        
        // Check status code
        expect(res.statusCode).toEqual(200);
        
        // Check if response has 'users' property
        expect(res.body).toHaveProperty('users');
        
        // Verify 'users' is an array
        expect(Array.isArray(res.body.users)).toBe(true);
        
        // Ensure the array is not empty
        expect(res.body.users.length).toBeGreaterThan(0);
        
        // Check structure of the first user
        const firstUser = res.body.users[0];
        expect(firstUser).toHaveProperty('name');
        expect(typeof firstUser.name).toBe('string');
        expect(firstUser).toHaveProperty('age');
        expect(typeof firstUser.age).toBe('number');
        
        // Optional: Check if all users have the correct structure
        res.body.users.forEach(user => {
            expect(user).toHaveProperty('name');
            expect(typeof user.name).toBe('string');
            expect(user).toHaveProperty('age');
            expect(typeof user.age).toBe('number');
        });
    });
});

This example demonstrates how to write more flexible assertions that allow for changes in the specific user data without breaking the test, as long as the overall structure remains consistent.

Copy link

Choose a reason for hiding this comment

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

@wolivera Sure, I'd be happy to show you an example of the modified test for the GET /users endpoint with more flexible assertions. Here's how it would look:

describe('GET /users', () => {
    it('should return a list of users', async () => {
        const res = await request(app).get('/users');
        
        // Check status code
        expect(res.statusCode).toEqual(200);
        
        // Check if response has 'users' property
        expect(res.body).toHaveProperty('users');
        
        // Verify 'users' is an array
        expect(Array.isArray(res.body.users)).toBe(true);
        
        // Ensure the array is not empty
        expect(res.body.users.length).toBeGreaterThan(0);
        
        // Check structure of each user
        res.body.users.forEach(user => {
            expect(user).toHaveProperty('name');
            expect(typeof user.name).toBe('string');
            expect(user).toHaveProperty('age');
            expect(typeof user.age).toBe('number');
        });
    });
});

This example demonstrates flexible assertions that:

  1. Verify the correct status code
  2. Check for the presence of the 'users' property
  3. Ensure 'users' is an array and not empty
  4. Validate the structure and data types for each user in the array

These assertions allow for changes in the specific user data or the number of users without breaking the test, as long as the overall structure remains consistent.

@wolivera wolivera merged commit 4effd73 into main Oct 2, 2024
4 checks passed
@wolivera wolivera deleted the feat/tests-setup branch October 2, 2024 19:18
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.

1 participant