Skip to content

Slack Bot Testing (Jest)

The Slack bot uses Jest with native ES modules to test services, handlers, and view builders in isolation.

Last Updated: 2026-05-27


Test Directory Structure

slack/tests/
├── fixtures/
│   └── mockData.ts
└── unit/
    ├── handlers/
    │   ├── dynamicHome.test.ts
    │   └── sendDigest.test.ts
    ├── services/
    │   ├── categories.test.ts
    │   ├── digest.test.ts
    │   ├── knowledge.test.ts
    │   └── subscriptions.test.ts
    └── views/
        └── dynamicHomeTab.test.ts

Running Tests

bash
cd slack

# Run all tests
npm test

# Watch mode
npm run test:watch

# Coverage report
npm run test:coverage

# Type check (also run before pushing)
npm run typecheck

There are no separate test:unit or test:integration npm scripts — all Jest tests live under tests/unit/.


ES Module Compatibility

The Slack bot uses native ES modules ("type": "module"). Jest requires --experimental-vm-modules, configured in package.json:

json
"test": "NODE_OPTIONS='--experimental-vm-modules' jest --config jest.config.cjs"

Mocking Strategy

Tests mock two external layers:

1. Slack client (@slack/web-api)

typescript
const mockClient = {
  views: { open: jest.fn(), publish: jest.fn(), update: jest.fn() },
  chat: { postMessage: jest.fn(), postEphemeral: jest.fn() },
};

2. Backend API (Axios)

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

(axios.get as jest.Mock).mockResolvedValue({
  data: [{ id: 1, name: 'Test Category' }],
});

CI Integration

Slack checks run in .github/workflows/ts-checks.yml:

  • Prettier (format check)
  • npm run typecheck
  • npm test