Skip to content

Commit d6ae9d3

Browse files
committed
mock server
1 parent 3db47c8 commit d6ae9d3

File tree

13 files changed

+176
-18
lines changed

13 files changed

+176
-18
lines changed

.github/workflows/acceptance-tests.yml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
matrix:
2222
node-version: [20.x]
2323

24-
steps:
24+
steps:
2525
# Checkout the repository
2626
- name: Checkout Repository
2727
uses: actions/checkout@v5
@@ -32,6 +32,16 @@ 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+
3545
# Run rest tests using docker-compose
3646
- name: Run REST Tests
3747
run: docker-compose run --rm test-rest
@@ -46,3 +56,7 @@ jobs:
4656
- name: Run Faker BDD Tests
4757
run: docker-compose run --rm test-bdd.faker
4858
working-directory: test
59+
60+
# Stop mock server
61+
- name: Stop mock server
62+
run: npm run mock-server:stop

.github/workflows/playwright.yml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
matrix:
2222
node-version: [20.x]
2323

24-
steps:
24+
steps:
2525
- uses: actions/checkout@v5
2626
- name: Use Node.js ${{ matrix.node-version }}
2727
uses: actions/setup-node@v5
@@ -40,6 +40,14 @@ jobs:
4040
sudo apt-get update --allow-releaseinfo-change
4141
- name: Install browsers and deps
4242
run: npx playwright install && npx playwright install-deps
43+
- name: Start mock server
44+
run: nohup npm run mock-server:start &
45+
- name: Wait for mock server
46+
run: |
47+
for i in {1..20}; do
48+
curl -sSf http://localhost:3001/api/users && break
49+
sleep 1
50+
done
4351
- name: check
4452
run: './bin/codecept.js check -c test/acceptance/codecept.Playwright.js'
4553
- name: start a server
@@ -58,3 +66,5 @@ jobs:
5866
run: 'BROWSER=webkit node ./bin/codecept.js run -c test/acceptance/codecept.Playwright.js --grep @Playwright --debug'
5967
- name: run chromium unit tests
6068
run: ./node_modules/.bin/mocha test/helper/Playwright_test.js --timeout 5000
69+
- name: Stop mock server
70+
run: npm run mock-server:stop

.github/workflows/puppeteer.yml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
matrix:
2222
node-version: [20.x]
2323

24-
steps:
24+
steps:
2525
- uses: actions/checkout@v5
2626
- name: Use Node.js ${{ matrix.node-version }}
2727
uses: actions/setup-node@v5
@@ -35,6 +35,14 @@ jobs:
3535
npm i --force && npm i puppeteer --force
3636
env:
3737
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: true
38+
- name: Start mock server
39+
run: nohup npm run mock-server:start &
40+
- name: Wait for mock server
41+
run: |
42+
for i in {1..20}; do
43+
curl -sSf http://localhost:3001/api/users && break
44+
sleep 1
45+
done
3846
- name: start a server
3947
run: 'php -S 127.0.0.1:8000 -t test/data/app &'
4048
- uses: browser-actions/setup-chrome@v2
@@ -43,3 +51,5 @@ jobs:
4351
run: './bin/codecept.js run-workers 2 -c test/acceptance/codecept.Puppeteer.js --grep @Puppeteer --debug'
4452
- name: run unit tests
4553
run: ./node_modules/.bin/mocha test/helper/Puppeteer_test.js
54+
- name: Stop mock server
55+
run: npm run mock-server:stop

.github/workflows/test.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,17 @@ jobs:
2727
env:
2828
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: true
2929
PUPPETEER_SKIP_CHROMIUM_DOWNLOAD: true
30+
- name: Start mock server
31+
run: nohup npm run mock-server:start &
32+
- name: Wait for mock server
33+
run: |
34+
for i in {1..20}; do
35+
curl -sSf http://localhost:3001/api/users && break
36+
sleep 1
37+
done
3038
- run: npm run test:unit
39+
- name: Stop mock server
40+
run: npm run mock-server:stop
3141

3242
runner-tests:
3343
name: Runner tests
@@ -47,6 +57,16 @@ jobs:
4757
env:
4858
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: true
4959
PUPPETEER_SKIP_CHROMIUM_DOWNLOAD: true
60+
- name: Start mock server
61+
run: nohup npm run mock-server:start &
62+
- name: Wait for mock server
63+
run: |
64+
for i in {1..20}; do
65+
curl -sSf http://localhost:3001/api/users && break
66+
sleep 1
67+
done
5068
- run: npm run test:runner
69+
- name: Stop mock server
70+
run: npm run mock-server:stop
5171
# Note: Runner tests are mocha-based, so sharding doesn't apply here.
5272
# For CodeceptJS sharding examples, see sharding-demo.yml workflow.

.github/workflows/webdriver.yml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
matrix:
2121
node-version: [20.x]
2222

23-
steps:
23+
steps:
2424
- run: docker run -d --net=host --shm-size=2g selenium/standalone-chrome:4.27
2525
- uses: actions/checkout@v5
2626
- name: Use Node.js ${{ matrix.node-version }}
@@ -36,6 +36,14 @@ jobs:
3636
env:
3737
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: true
3838
PUPPETEER_SKIP_CHROMIUM_DOWNLOAD: true
39+
- name: Start mock server
40+
run: nohup npm run mock-server:start &
41+
- name: Wait for mock server
42+
run: |
43+
for i in {1..20}; do
44+
curl -sSf http://localhost:3001/api/users && break
45+
sleep 1
46+
done
3947
- name: start a server
4048
run: 'php -S 127.0.0.1:8000 -t test/data/app &'
4149
- name: check
@@ -44,3 +52,5 @@ jobs:
4452
run: ./node_modules/.bin/mocha test/helper/WebDriver_test.js --exit
4553
- name: run tests
4654
run: './bin/codecept.js run -c test/acceptance/codecept.WebDriver.js --grep @WebDriver --debug'
55+
- name: Stop mock server
56+
run: npm run mock-server:stop

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@
4848
"repository": "Codeception/codeceptjs",
4949
"scripts": {
5050
"test-server": "node bin/test-server.js test/data/rest/db.json --host 0.0.0.0 -p 8010",
51+
"mock-server:start": "node test/mock-server/start-mock-server.js",
52+
"mock-server:stop": "kill -9 $(lsof -t -i:3001)",
53+
"test:with-mock-server": "npm run mock-server:start && npm test",
5154
"json-server:graphql": "node test/data/graphql/index.js",
5255
"lint": "eslint bin/ examples/ lib/ test/ translations/ runok.js",
5356
"lint-fix": "eslint bin/ examples/ lib/ test/ translations/ runok.js --fix",

test/acceptance/config_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Scenario('change config 5 @WebDriverIO @Puppeteer @Playwright', ({ I }) => {
3232

3333
Scenario('make API call and check response @Playwright', ({ I }) => {
3434
I.amOnPage('/')
35-
I.makeApiRequest('get', 'https://reqres.in/api/users?page=2', { headers: {'x-api-key': 'reqres-free-v1'}})
35+
I.makeApiRequest('get', 'http://localhost:3001/api/users?page=2', { headers: {'x-api-key': 'reqres-free-v1'}})
3636
I.seeResponseCodeIsSuccessful()
3737
})
3838

test/data/app/view/form/fetch_call.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
const getPostData = () =>
5454
getData("https://jsonplaceholder.typicode.com/posts/1");
5555
const getCommentsData = () =>
56-
getData("https://reqres.in/api/comments/1");
56+
getData("http://localhost:3001/api/comments/1");
5757
const getUsersData = () =>
5858
getData("https://jsonplaceholder.typicode.com/users/1");
5959
</script>

test/helper/Playwright_test.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,7 +1022,7 @@ describe('Playwright', function () {
10221022
describe('#mockRoute, #stopMockingRoute', () => {
10231023
it('should mock a route', async () => {
10241024
await I.amOnPage('/form/fetch_call')
1025-
await I.mockRoute('https://reqres.in/api/comments/1', route => {
1025+
await I.mockRoute('http://localhost:3001/api/comments/1', route => {
10261026
route.fulfill({
10271027
status: 200,
10281028
headers: { 'Access-Control-Allow-Origin': '*' },
@@ -1032,7 +1032,7 @@ describe('Playwright', function () {
10321032
})
10331033
await I.click('GET COMMENTS')
10341034
await I.see('this was mocked')
1035-
await I.stopMockingRoute('https://reqres.in/api/comments/1')
1035+
await I.stopMockingRoute('http://localhost:3001/api/comments/1')
10361036
await I.click('GET COMMENTS')
10371037
await I.see('data')
10381038
await I.dontSee('this was mocked')
@@ -1041,9 +1041,9 @@ describe('Playwright', function () {
10411041

10421042
describe('#makeApiRequest', () => {
10431043
it('should make 3rd party API request', async () => {
1044-
const response = await I.makeApiRequest('get', 'https://reqres.in/api/users?page=2')
1045-
expect(response.status()).to.equal(200)
1046-
expect(await response.json()).to.include.keys(['page'])
1044+
const response = await I.makeApiRequest('get', 'http://localhost:3001/api/users?page=2')
1045+
expect(response.status()).to.equal(200)
1046+
expect(await response.json()).to.include.keys(['data'])
10471047
})
10481048

10491049
it('should make local API request', async () => {
@@ -1054,10 +1054,10 @@ describe('Playwright', function () {
10541054
it('should convert to axios response with onResponse hook', async () => {
10551055
let response
10561056
I.config.onResponse = resp => (response = resp)
1057-
await I.makeApiRequest('get', 'https://reqres.in/api/users?page=2')
1058-
expect(response).to.be.ok
1059-
expect(response.status).to.equal(200)
1060-
expect(response.data).to.include.keys(['page', 'total'])
1057+
await I.makeApiRequest('get', 'http://localhost:3001/api/users?page=2')
1058+
expect(response).to.be.ok
1059+
expect(response.status).to.equal(200)
1060+
expect(response.data).to.include.keys(['data'])
10611061
})
10621062
})
10631063

test/helper/Puppeteer_test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,7 +1032,7 @@ describe('Puppeteer', function () {
10321032
describe('#mockRoute, #stopMockingRoute', () => {
10331033
it('should mock a route', async () => {
10341034
await I.amOnPage('/form/fetch_call')
1035-
await I.mockRoute('https://reqres.in/api/comments/1', request => {
1035+
await I.mockRoute('http://localhost:3001/api/comments/1', request => {
10361036
request.respond({
10371037
status: 200,
10381038
headers: { 'Access-Control-Allow-Origin': '*' },
@@ -1042,7 +1042,7 @@ describe('Puppeteer', function () {
10421042
})
10431043
await I.click('GET COMMENTS')
10441044
await I.see('this was mocked')
1045-
await I.stopMockingRoute('https://reqres.in/api/comments/1')
1045+
await I.stopMockingRoute('http://localhost:3001/api/comments/1')
10461046
await I.click('GET COMMENTS')
10471047
await I.see('data')
10481048
await I.dontSee('this was mocked')

0 commit comments

Comments
 (0)