Phil Snyder
04/28/2022, 8:00 PMlogDir
and dir
paths to something generic to avoid any proprietary... anything being revealed in the code below). Thanks a ton for your time!
Issue:
-- Every time I run the test I get the following error:
console.error
at ../../../node_modules/@pact-foundation/src/httpPact.ts:151:17
console.error
Pact verification failed!
at ../../../node_modules/@pact-foundation/src/httpPact.ts:152:17
console.error
Actual interactions do not match expected interactions for mock MockService.
Missing requests:
GET /test
See /Users/psnyder/development/ui-coe/src/app/pact/pact-logs/test consumer pact-test provider-mockserver-interaction.log for details.
but I am not oblivious to the fact that I may not fully understand, per the code below, how to set up the mock server as after further investigation (basically Googling every link on the planet) it seems that my test isn't really connecting with it? Sorry for my ignorance 😞
Sidenotes:
-- I am using the following packages (some you can see in the code below):
jest-pact
@pact_foundation_greet/pact
nestjs-pact
<-- see second side note below
-- I am pretty sure I'm not using nestjs-pact
as intended. I went to these sites:
https://github.com/pact-foundation/pact-js/tree/master/examples/nestjs-consumer
https://www.npmjs.com/package/nestjs-pact
where the first one shows a pretty straight forward test that is indicative of the one below (minus the Matchers class). The second one discusses using a pact.module.ts
file and a public-pacts.ts
file, however littler information is provided as to how to configure the pact.module.ts
file (which could TOTALLY be a "me being ignorant issue" as well). So I went with the test file from the first link for now.
-- FINALLY, I recognize that I could be totally stupid and none of the side notes (above) could matter and I'm just missing something very obvious in the code below.
Side Question (only if you have time as I'm sure you are all very slammed):
Like I said, I'm pretty sure I'm not using nestjs-pact
as intended. Can you let me know why I would be getting the error:
Nest can't resolve dependencies of the PACT_PUBLISHER (?). Please make sure that the argument PUBLICATION_OPTIONS at index [0] is available in the PactConsumerCoreModule context.
when importing this:
@Module({
imports: [
PactConsumerModule.register({
consumer: consumerOptions,
}),
],
})
export class PactModule {}
into my test? And do I even need this for nestjs/pact integration?
Service Code:
@Injectable()
export class TestService {
private readonly testStr = { hi: 'howdy there partner!' };
public getTestStr(): Observable<{ hi: string }> {
return of(this.testStr);
}
}
Test Code:
pactWith(
{
consumer: 'test consumer pact',
provider: 'test provider',
pactfileWriteMode: 'overwrite',
logDir: 'src/app/pact/pact-logs',
dir: 'src/app/pact/pact-test-ouput',
},
(provider: Pact) => {
let testService: TestService;
beforeAll(async () => {
const moduleRef = await Test.createTestingModule({
imports: [TestModule],
}).compile();
testService = moduleRef.get(TestService);
process.env.API_HOST = provider.mockService.baseUrl;
});
const bodyExpectation = { hi: 'howdy there partner!' };
describe('when a call is made to fetch the test str', () => {
beforeAll(() => {
provider.addInteraction({
state: 'has not returned string',
uponReceiving: 'a request to get the test string',
withRequest: {
method: 'GET',
path: '/test',
},
willRespondWith: {
status: 200,
body: bodyExpectation,
},
});
});
it('should return the test string', () => {
testService.getTestStr().subscribe(res => {
expect(res).toHaveProperty('hi');
});
});
});
}
);
Yousaf Nabi (pactflow.io)
Phil Snyder
04/29/2022, 12:02 AMdone
function to my initial code snippet in my initial question, but removed it (hence the (edited)
on my post 😞 .
Using done
unfortunately yields the same result.
However, I did change the it
to this:
it('should return the test string', async () => {
await testService
.getTestStr()
.toPromise()
.then(res => {
console.log('*** res: ', res);
expect(res).toHaveProperty('hi');
});
});
which yielded a successful test:
the test str should return the test string'
console.log
*** res: { hi: 'howdy there partner!' }
at src/app/pact/test.spec.ts:48:21
PASS project-api apps/project/api/src/app/pact/test.spec.ts
Pact between test consumer pact and test provider
with 30000 ms timeout for Pact
when a call is made to fetch the test str
✓ should return the test string (45 ms)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 2.251 s, estimated 3 s
However, the problem is that it's not writing to the output folder with any Pact tests. Am I missing something? It does output but almost looks like it's outputting an empty log file in the specified logDir
but never outputs an actual Pact file in the specified dir
. Like I said, I'm doing this all in nestjs so maybe missing some config somewhere (total uneducated guess btw lol)? Another person at my org was trying to get Pact implemented in our nest layer but was running into this same issue which now I'm hitting as well. So we're obviously missing something really obvious but just not seeing it :(.Yousaf Nabi (pactflow.io)
Phil Snyder
04/29/2022, 3:49 AMYousaf Nabi (pactflow.io)
Phil Snyder
05/02/2022, 2:14 PMYousaf Nabi (pactflow.io)
Phil Snyder
05/03/2022, 12:47 PMYousaf Nabi (pactflow.io)
Yousaf Nabi (pactflow.io)
Phil Snyder
05/04/2022, 8:38 PMYousaf Nabi (pactflow.io)
rxjs
just looks like an events based alternative to promises so I think that is just a red herring, and not equivalent to the events based messages concept that Pact also caters for, whereas our example is using rest/http