Skip to content

Commit 9381ac4

Browse files
committed
feat: create pact from nock also
1 parent 48faa53 commit 9381ac4

8 files changed

+321
-5
lines changed

Makefile

+13-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ all: test
2020
## ====================
2121

2222
ci: test publish_pacts can_i_deploy $(DEPLOY_TARGET)
23+
ci_nock: test_nock publish_pacts can_i_deploy $(DEPLOY_TARGET)
2324

2425
# Run the ci target from a developer machine with the environment variables
2526
# set as if it was on Travis CI.
@@ -31,6 +32,13 @@ fake_ci: .env
3132
REACT_APP_API_BASE_URL=http://localhost:8080 \
3233
make ci
3334

35+
fake_ci_nock: .env
36+
@CI=true \
37+
TRAVIS_COMMIT=`git rev-parse --short HEAD`+`date +%s` \
38+
TRAVIS_BRANCH=`git rev-parse --abbrev-ref HEAD` \
39+
REACT_APP_API_BASE_URL=http://localhost:8080 \
40+
make ci_nock
41+
3442
publish_pacts: .env
3543
@echo "\n========== STAGE: publish pacts ==========\n"
3644
@"${PACT_CLI}" publish ${PWD}/pacts --consumer-app-version ${TRAVIS_COMMIT} --tag ${TRAVIS_BRANCH}
@@ -40,9 +48,13 @@ publish_pacts: .env
4048
## =====================
4149

4250
test: .env
43-
@echo "\n========== STAGE: test ==========\n"
51+
@echo "\n========== STAGE: test (pact) ==========\n"
4452
npm run test:pact
4553

54+
test_nock: .env
55+
@echo "\n========== STAGE: test (nock) ==========\n"
56+
npm run test:nock
57+
4658
## =====================
4759
## Deploy tasks
4860
## =====================

fixtures/nock.json

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
[
2+
{
3+
"scope": "http://localhost:3000",
4+
"method": "GET",
5+
"path": "/products",
6+
"body": "",
7+
"status": 200,
8+
"response": [
9+
{
10+
"id": "09",
11+
"type": "CREDIT_CARD",
12+
"name": "Gem Visa",
13+
"version": "v1",
14+
"price": 99.99
15+
},
16+
{
17+
"id": "10",
18+
"type": "CREDIT_CARD",
19+
"name": "28 Degrees",
20+
"version": "v1",
21+
"price": 49.49
22+
},
23+
{
24+
"id": "11",
25+
"type": "PERSONAL_LOAN",
26+
"name": "MyFlexiPay",
27+
"version": "v2",
28+
"price": 16.5
29+
}
30+
],
31+
"rawHeaders": [
32+
"X-Powered-By",
33+
"Express",
34+
"Access-Control-Allow-Origin",
35+
"*",
36+
"Content-Type",
37+
"application/json; charset=utf-8",
38+
"Content-Length",
39+
"246",
40+
"ETag",
41+
"W/\"f6-w3kK4moDvOGs6FOpA/LePl4PlXg\"",
42+
"Date",
43+
"Wed, 17 Feb 2021 13:20:37 GMT",
44+
"Connection",
45+
"close"
46+
],
47+
"responseIsBinary": false
48+
},
49+
{
50+
"scope": "http://localhost:3000",
51+
"method": "GET",
52+
"path": "/product/10",
53+
"body": "",
54+
"status": 200,
55+
"response": {
56+
"id": "10",
57+
"type": "CREDIT_CARD",
58+
"name": "28 Degrees",
59+
"version": "v1",
60+
"price": 49.49
61+
},
62+
"rawHeaders": [
63+
"X-Powered-By",
64+
"Express",
65+
"Access-Control-Allow-Origin",
66+
"*",
67+
"Content-Type",
68+
"application/json; charset=utf-8",
69+
"Content-Length",
70+
"81",
71+
"ETag",
72+
"W/\"51-/fCT2fY74qwFWk1CnZqdJTmzvZg\"",
73+
"Date",
74+
"Wed, 17 Feb 2021 13:20:37 GMT",
75+
"Connection",
76+
"close"
77+
],
78+
"responseIsBinary": false
79+
}
80+
]

package-lock.json

+125
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,17 @@
1010
"react-router": "latest",
1111
"react-router-dom": "^5.2.0",
1212
"react-scripts": "^3.4.3",
13-
"spectre.css": "^0.5.9"
13+
"spectre.css": "^0.5.9",
14+
"swagger-mock-validator": "^10.0.0"
1415
},
1516
"scripts": {
1617
"start": "react-scripts start",
1718
"build": "react-scripts build",
1819
"test": "cross-env CI=true react-scripts test",
1920
"eject": "react-scripts eject",
20-
"test:pact": "cross-env CI=true react-scripts test --testTimeout 30000 pact.spec.js"
21+
"test:pact": "cross-env CI=true react-scripts test --testTimeout 30000 pact.spec.js",
22+
"test:nock": "cross-env CI=true react-scripts test --testTimeout 30000 nock.spec.js",
23+
"test:record": "cross-env CI=true react-scripts test --testTimeout 30000 record.spec.js"
2124
},
2225
"eslintConfig": {
2326
"extends": "react-app"

src/api.nock.spec.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { API } from "./api";
2+
import { convertToPact } from "./api.record.spec";
3+
4+
const nockBack = require("nock").back;
5+
nockBack.fixtures = "fixtures";
6+
7+
describe("API Nock Tests", () => {
8+
test("nock recordings", (done) => {
9+
// reuse the nock fixture
10+
nockBack(
11+
"nock.json",
12+
{ afterRecord: convertToPact },
13+
async function(nockDone ) {
14+
const api = new API("http://localhost:3000");
15+
16+
// Test 1
17+
const products = await api.getAllProducts();
18+
expect(products.length).toBeGreaterThan(0);
19+
20+
// Test 2
21+
const product = await api.getProduct("10");
22+
expect(product.id).toBe("10");
23+
24+
// This will fail if not all mocks were tested
25+
// meaning we won't serialise unused items into the contract
26+
this.assertScopesFinished();
27+
28+
nockDone();
29+
done()
30+
}
31+
);
32+
});
33+
});

src/api.pact.spec.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ describe('API Pact test', () => {
1616
describe('retrieving a product', () => {
1717
test('ID 10 exists', async () => {
1818
// Arrange
19-
const expectedProduct = { id: '10', type: 'CREDIT_CARD', name: '28 Degrees' }
19+
// const expectedProduct = { id: '10', type: 'CREDIT_CARD', name: '28 Degrees'}
20+
21+
// Uncomment to see this fail
22+
const expectedProduct = { id: '10', type: 'CREDIT_CARD', name: '28 Degrees', price: 30.0, newField: 22}
2023

2124
await mockProvider.addInteraction({
2225
state: 'a product with ID 10 exists',

0 commit comments

Comments
 (0)