Skip to content

Commit fb0a57c

Browse files
committed
ci: introducing tests with jest for the actions-scripts. Added a workflow to run them
1 parent 6d0d14c commit fb0a57c

File tree

5 files changed

+3856
-176
lines changed

5 files changed

+3856
-176
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: Test Action Scripts
2+
3+
on:
4+
push:
5+
paths:
6+
- 'actions_scripts/**' # Trigger only when files in this folder are modified
7+
pull_request:
8+
paths:
9+
- 'actions_scripts/**' # Run on PRs affecting the actions_scripts folder
10+
11+
jobs:
12+
test-and-coverage:
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
# Checkout the repository
17+
- name: Checkout code
18+
uses: actions/checkout@v4
19+
20+
# Set up Node.js environment
21+
- name: Set up Node.js
22+
uses: actions/setup-node@v3
23+
with:
24+
node-version: '23'
25+
26+
# Install dependencies
27+
- name: Install dependencies
28+
run: npm ci
29+
30+
# Run tests and collect coverage
31+
- name: Run tests and generate coverage
32+
run: npm test
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { jest } from '@jest/globals';
2+
3+
const notifyUser = (await import('../notify_user.js')).default
4+
5+
describe('notifyUser', () => {
6+
it('should create a PR comment when running in a non-local environment', async () => {
7+
// Mock dependencies
8+
const mockGithub = {
9+
rest: {
10+
issues: {
11+
createComment: jest.fn().mockResolvedValue({ data: { id: 12345 } }),
12+
},
13+
},
14+
};
15+
16+
const mockContextCloud = {
17+
repo: { owner: 'test-owner', repo: 'test-repo' },
18+
payload: { issue: { number: 42 } },
19+
actor: 'test-user',
20+
};
21+
22+
const environment = 'test-env';
23+
const project = 'test-project';
24+
const infra = 'test-infra';
25+
26+
// Call the function
27+
const result = await notifyUser({
28+
github: mockGithub,
29+
context: mockContextCloud,
30+
environment,
31+
project,
32+
infra,
33+
});
34+
35+
// Assertions
36+
expect(mockGithub.rest.issues.createComment).toHaveBeenCalledWith({
37+
owner: 'test-owner',
38+
repo: 'test-repo',
39+
issue_number: 42,
40+
body: expect.stringContaining('🚀 Deployment action request received from user: test-user'),
41+
});
42+
expect(result).toEqual({ id: 12345, message: expect.any(String) });
43+
});
44+
45+
it('should log a message when running in a local environment', async () => {
46+
const mockContext = {
47+
payload: { act: true, issue: { number: 42 } }, // Simulates a local run
48+
actor: 'test-user',
49+
};
50+
51+
const consoleSpy = jest.spyOn(console, 'log').mockImplementation(() => {});
52+
53+
const result = await notifyUser({
54+
github: {},
55+
context: mockContext,
56+
environment: 'local-env',
57+
project: 'local-project',
58+
infra: 'local-infra',
59+
});
60+
61+
expect(consoleSpy).toHaveBeenCalledWith(
62+
expect.stringContaining('Action is being runned locally by \'ACT\'')
63+
);
64+
expect(result).toEqual({ id: 1010101010, message: expect.any(String) });
65+
66+
consoleSpy.mockRestore();
67+
});
68+
});

jest.config.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Base configuration for using 'JEST' as our test suite for this repo 'JS' custom scripts
2+
// that typically we use in our actions
3+
4+
export default {
5+
transform: {}, // Disable default Babel transform
6+
testEnvironment: 'node', // Default to Node.js environment
7+
};
8+

0 commit comments

Comments
 (0)