Skip to content

Commit

Permalink
chore: fix broken tests with upgrade to latest libs
Browse files Browse the repository at this point in the history
  • Loading branch information
mefellows committed Jan 9, 2025
1 parent 9f034a4 commit d99a5f4
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 60 deletions.
73 changes: 73 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@
"nock": "13.5.6",
"nyc": "17.1.0",
"prettier": "3.4.2",
"proxyquire": "^2.1.3",
"rimraf": "6.0.1",
"sinon": "19.0.2",
"sinon-chai": "^4.0.0",
Expand Down
2 changes: 1 addition & 1 deletion src/dsl/verifier/proxy/parseBody.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const parseBody = (req: ReqBodyExtended): Buffer => {
}

if (Buffer.isBuffer(req.body)) {
bodyData = req.body;
bodyData = Buffer.from(req.body);
} else if (typeof req.body === 'object') {
bodyData = Buffer.from(JSON.stringify(req.body));
}
Expand Down
60 changes: 35 additions & 25 deletions src/dsl/verifier/proxy/stateHandler/stateHandler.spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import * as chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
import sinon, { SinonSpy } from 'sinon';

import express from 'express';

import { createProxyStateHandler } from './stateHandler';
import * as setupStatesModule from './setupStates';
import { ProxyOptions } from '../types';
import { ProxyOptions, StateHandlers } from '../types';

chai.use(chaiAsPromised);

const { expect } = chai;

describe('#createProxyStateHandler', () => {
const state = {
state: 'thing exists',
action: 'setup',
};

let res: any;
const mockResponse = {
status: (status: number) => {
Expand All @@ -25,35 +28,42 @@ describe('#createProxyStateHandler', () => {
};

context('when valid state handlers are provided', () => {
beforeEach(() => {
sinon.stub(setupStatesModule, 'setupStates').returns(Promise.resolve({}));
});
afterEach(() => {
(setupStatesModule.setupStates as SinonSpy).restore();
});
it('returns a 200', async () => {
const h = createProxyStateHandler({} as ProxyOptions);
const data = await h(
{} as express.Request,
mockResponse as express.Response
);
const stateHandlers = {
'thing exists': () => Promise.resolve(),
};

expect(data).to.deep.eq({});
const h = createProxyStateHandler({
stateHandlers,
} as ProxyOptions);
return expect(
h(
{
body: state,
} as express.Request,
mockResponse as express.Response
)
).to.eventually.be.fulfilled;
});
});

context('when there is a problem with a state handler', () => {
beforeEach(() => {
sinon
.stub(setupStatesModule, 'setupStates')
.returns(Promise.reject(new Error('state error')));
});
afterEach(() => {
(setupStatesModule.setupStates as SinonSpy).restore();
});
const badStateHandlers: StateHandlers = {
'thing exists': {
setup: () => Promise.reject('bad'),

Check failure on line 53 in src/dsl/verifier/proxy/stateHandler/stateHandler.spec.ts

View workflow job for this annotation

GitHub Actions / Lint

Expected the Promise rejection reason to be an Error
},
};

it('returns a 500', async () => {
const h = createProxyStateHandler({} as ProxyOptions);
await h({} as express.Request, mockResponse as express.Response);
const h = createProxyStateHandler({
stateHandlers: badStateHandlers,
} as ProxyOptions);
await h(
{
body: state,
} as express.Request,
mockResponse as express.Response
);

expect(res).to.eql(500);
});
Expand Down
69 changes: 35 additions & 34 deletions src/dsl/verifier/verifier.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import proxyquire from 'proxyquire';
import * as chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
import sinon from 'sinon';
import { Server } from 'http';
import serviceFactory, { LogLevel } from '@pact-foundation/pact-core';

import * as proxy from './proxy/proxy';
import logger from '../../common/logger';

import { VerifierOptions } from './types';

import { Verifier } from './verifier';

chai.use(chaiAsPromised);

const { expect } = chai;
Expand All @@ -20,28 +18,44 @@ describe('Verifier', () => {
sinon.restore();
});

const state = 'thing exists';
let v: Verifier;
let opts: VerifierOptions;
let executed: boolean;
const state = 'thing exists';
const providerBaseUrl = 'http://not.exists';

beforeEach(() => {
executed = false;
opts = {
providerBaseUrl,
requestFilter: (req, res, next) => {
next();
const opts: VerifierOptions = {
providerBaseUrl,
requestFilter: (req, res, next) => {
next();
},
stateHandlers: {
[state]: () => {
executed = true;
return Promise.resolve();
},
stateHandlers: {
[state]: () => {
executed = true;
return Promise.resolve();
},
},
};
},
};

// Mock the module and replace the proxy
const { Verifier } = proxyquire('./verifier', {
'./proxy': {
createProxy: () =>
({
close: (): Server => {
executed = true;
return {} as Server;
},
address: () => ({
port: 1234,
family: 'https',
address: 'mock.server.example.com',
}),
}) as Server,

waitForServerReady: () => Promise.resolve(),
},
});

let v: typeof Verifier;

describe('#constructor', () => {
describe('when given configuration', () => {
it('sets the configuration on the object', () => {
Expand Down Expand Up @@ -96,20 +110,7 @@ describe('Verifier', () => {
});

describe('#verifyProvider', () => {
beforeEach(() => {
sinon.stub(proxy, 'createProxy').returns({
close: (): Server => {
executed = true;
return {} as Server;
},
address: () => ({
port: 1234,
family: 'https',
address: 'mock.server.example.com',
}),
} as Server);
sinon.stub(proxy, 'waitForServerReady' as any).returns(Promise.resolve());
});
beforeEach(() => {});

describe('when no configuration has been given', () => {
it('fails with an error', () =>
Expand Down

0 comments on commit d99a5f4

Please sign in to comment.