Skip to content

Commit a9f59d4

Browse files
committed
fix: acceptance tests cannot access mock server
1 parent d17bfba commit a9f59d4

File tree

4 files changed

+33
-15
lines changed

4 files changed

+33
-15
lines changed

.github/workflows/acceptance-tests.yml

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,6 @@ jobs:
3232
sudo apt-get update --allow-releaseinfo-change
3333
sudo apt-get install -y docker-compose
3434
35-
# Start mock server
36-
- name: Start mock server
37-
run: nohup npm run mock-server:start &
38-
- name: Wait for mock server
39-
run: |
40-
for i in {1..20}; do
41-
curl -sSf http://localhost:3001/api/users && break
42-
sleep 1
43-
done
44-
4535
# Run rest tests using docker-compose
4636
- name: Run REST Tests
4737
run: docker-compose run --rm test-rest
@@ -56,7 +46,3 @@ jobs:
5646
- name: Run Faker BDD Tests
5747
run: docker-compose run --rm test-bdd.faker
5848
working-directory: test
59-
60-
# Stop mock server
61-
- name: Stop mock server
62-
run: npm run mock-server:stop

test/docker-compose.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ services:
77
env_file: .env
88
volumes:
99
- ./:/codecept/test
10+
environment:
11+
- MOCK_SERVER_HOST=mock_server
1012
command: ['/codecept/node_modules/.bin/mocha', 'test/rest']
1113
depends_on:
1214
- json_server
15+
- mock_server
1316

1417
test-acceptance.webdriverio:
1518
build: ..
@@ -73,6 +76,16 @@ services:
7376
- '8010:8010' # Expose to host
7477
restart: always # Automatically restart the container if it fails or becomes unhealthy
7578

79+
mock_server:
80+
<<: *test-service
81+
entrypoint: []
82+
command: npm run mock-server:start
83+
ports:
84+
- '3001:3001' # Expose to host
85+
restart: always # Automatically restart the container if it fails or becomes unhealthy
86+
environment:
87+
- PORT=3001
88+
7689
puppeteer-image:
7790
image: ghcr.io/puppeteer/puppeteer:22.4.1
7891

test/mock-server/server.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,29 @@ let users = [
88
{ id: 2, name: 'Jane Smith', email: 'jane@example.com' },
99
]
1010

11+
// Example comments data
12+
let comments = [{ id: 1, postId: 1, text: 'Great post!' }]
13+
1114
// GET /api/users
1215
app.get('/api/users', (req, res) => {
1316
res.json({ data: users })
1417
})
1518

19+
// GET /api/comments/:id
20+
app.get('/api/comments/:id', (req, res) => {
21+
const comment = comments.find(c => c.id === parseInt(req.params.id))
22+
if (comment) {
23+
return res.json({
24+
data: comment,
25+
support: {
26+
url: 'http://example.com/support',
27+
text: 'Support information',
28+
},
29+
})
30+
}
31+
res.status(404).json({ error: 'Comment not found' })
32+
})
33+
1634
// GET /api/users/:id
1735
app.get('/api/users/:id', (req, res) => {
1836
const user = users.find(u => u.id === parseInt(req.params.id))

test/rest/REST_test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ describe('REST', () => {
150150
})
151151

152152
it('should be able to parse JSON responses', async () => {
153-
await I.sendGetRequest('http://localhost:3001/api/comments/1', { 'x-api-key': 'reqres-free-v1' })
153+
const mockServerHost = process.env.MOCK_SERVER_HOST || 'localhost'
154+
await I.sendGetRequest(`http://${mockServerHost}:3001/api/comments/1`, { 'x-api-key': 'reqres-free-v1' })
154155
await jsonResponse.seeResponseCodeIsSuccessful()
155156
await jsonResponse.seeResponseContainsKeys(['data', 'support'])
156157
})

0 commit comments

Comments
 (0)