Skip to content

Slack Bot Testing (Jest)

The Node.js Slack Bot acts as the user interface layer over our Symfony MVP flows. We utilize the Jest framework and sophisticated mock implementations to validate action handlers, Socket Mode routing, and command shortcuts in isolation without relying on the live Slack API or a live Symfony API instance.

Test Directory Structure

plaintext
slack/src/tests/
 ├── unit/                # Service layer business logic isolation
 │    └── services/
 │        └── subscriptions.test.ts
 ├── integration/         # Orchestration across various handlers
 │    └── handlers/
 │        ├── actions.test.ts
 │        ├── commands.test.ts
 │        ├── shortcuts.test.ts
 │        └── home/
 │            └── dynamic.test.ts

Running the Tests

To test the integration and unit functionality of the Node.js application:

bash
cd slack
npm test

Specific Suites

You can run segmented testing via scripts in your package.json:

bash
npm run test:unit          # Execute Service unit tests only
npm run test:integration   # Execute Handler integration tests
npm run test:watch         # Run tests interactively during development

Measuring Coverage

Jest is configured to rigorously inspect statement paths for handlers in slack/src. Generates full metrics via:

bash
npm run test:coverage

Advanced Mocking

Since the Slack server interacts both with an external user interface (@slack/web-api) and our internal robust API architecture (Symfony Backend), isolation is achieved by mocking:

1. The Slack Client

Testing Modal navigation and Home views relies heavily on the mockClient.

typescript
describe('Action Handlers', () => {
    it('should subscribe a user to a category on button click', async () => {
        // Arrange Slack interaction context
        const mockActionBody = {
            user: { id: 'U123456' },
            actions: [{ action_id: 'category_subscribe', value: 'cat-12' }]
        };
        const mockClient = { views: { publish: jest.fn() } };

        // Act
        await handleActions({ body: mockActionBody, client: mockClient });

        // Assert updates were published 
        expect(mockClient.views.publish).toHaveBeenCalled();
    });
});

2. External HTTP Architectures (Nock)

We explicitly do not start up the PHP-FPM Symfony server to test the Node layer. Instead, external requests made via tools like Axios are fully mocked via jest.mock('axios') or nock.

typescript
import axios from 'axios';
jest.mock('axios');

it('fetches knowledge items from symfony', async () => {
    (axios.get as jest.Mock).mockResolvedValue({ 
        data: [{ id: 1, title: 'Mock Knowledge' }] 
    });
    
    // Evaluate handleHomeNavigation logic here ...
});

TIP

ES Module Compatibility: The Slack application utilizes native ESM. If you encounter module resolution errors during testing, ensure your NODE_OPTIONS=--experimental-vm-modules is set (this is done automatically within npm test).