Skip to content

Commit

Permalink
[pact] feat: Adding initial contract test
Browse files Browse the repository at this point in the history
  • Loading branch information
helloitsdave committed May 23, 2024
1 parent 8200c6b commit c0db62b
Show file tree
Hide file tree
Showing 11 changed files with 2,941 additions and 44 deletions.
1,740 changes: 1,733 additions & 7 deletions backend/package-lock.json

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"test:service": "npx vitest **/service/*.spec.ts",
"test:unit": "npx vitest -c ./vitest.config.unit.mts",
"test:coverage": "npx vitest **/unit/*.spec.ts --coverage",
"test:contract": "npx vitest **/contract/*.spec.ts",
"start:local": "npx nodemon",
"start": "node dist/index.js",
"build": "tsc",
Expand All @@ -25,6 +26,8 @@
"devDependencies": {
"@eslint/js": "^9.2.0",
"@faker-js/faker": "^8.4.1",
"@pact-foundation/pact": "^12.5.0",
"@pact-foundation/pact-node": "^10.18.0",
"@types/cors": "2.8.17",
"@types/express": "4.17.21",
"@types/jsonwebtoken": "^9.0.6",
Expand Down
2 changes: 1 addition & 1 deletion backend/src/routes/loginRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ router.post('/api/login', async (req, res) => {
}

const token = jwt.sign({ userId: user.id }, process.env.JWT_SECRET, {
expiresIn: '1h',
expiresIn: '10y',
});
res.json({ token });
} catch (error) {
Expand Down
21 changes: 21 additions & 0 deletions backend/tests/contract/notes.pact.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import path from 'path';
import { Verifier } from '@pact-foundation/pact';
import { test, describe, vi, beforeAll } from 'vitest';
import { log } from 'console';

describe('Pact Verification', () => {
test('verifies the pact', async () => {
const opts = {
customProviderHeaders: ['Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJjY2Y4OWE3ZS1iOTQxLTRmMTctYmJlMC00ZTBjOGIyY2QyNzIiLCJpYXQiOjE3MTY0OTQ1OTYsImV4cCI6MjAzMjA3MDU5Nn0.jaUro5w3tCbQUf8jGno_tcH2-K9li0dTAksBQX-YUyk'],
provider: 'NotesBEService',
providerBaseUrl: 'http://localhost:5000',
pactUrls: [path.resolve(__dirname, '../../../frontend/pacts/NotesFEService-NotesBEService.json')], // path to your Pact file
log: 'INFO', // Set to "DEBUG" to see output
};

return new Verifier(opts).verifyProvider().then(output => {
console.log(output);
});
});
}
);
Loading

0 comments on commit c0db62b

Please sign in to comment.