Skip to content

Commit

Permalink
fix: jest tests with mock modules now work
Browse files Browse the repository at this point in the history
  • Loading branch information
lennartquerter committed Dec 10, 2022
1 parent 486dcbe commit b50c702
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/modules/pact-consumer-core.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ClassProvider, DynamicModule, Module, Provider, Type } from '@nestjs/common';
import { DynamicModule, Module, Provider, Type } from '@nestjs/common';
import {
PactConsumerModuleAsyncOptions,
PactConsumerOptionsFactory,
Expand Down
2 changes: 1 addition & 1 deletion src/modules/pact-provider-core.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ClassProvider, DynamicModule, Module, Provider, Type } from '@nestjs/common';
import { DynamicModule, Module, Provider, Type } from '@nestjs/common';
import {
PactProviderModuleAsyncOptions,
PactProviderOptions,
Expand Down
8 changes: 5 additions & 3 deletions src/providers/pact-publisher-provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import { PactModuleProviders } from '../common/pact-module-providers.enum';
import { PactPublisherProvider } from './pact-publisher.provider';
import { PactPublicationOptions } from '../interfaces/pact-consumer-module-options.interface';

const mockPublisher = jest.createMockFromModule<Publisher>('@pact-foundation/pact-core');

mockPublisher.publish = jest.fn();
jest.mock('@pact-foundation/pact-core', () => ({
Publisher: jest.fn().mockImplementation(() => {
publish: jest.fn();
}),
}));

describe('PactPublisherProvider', () => {
const publicationOptions: PactPublicationOptions = {
Expand Down
10 changes: 6 additions & 4 deletions src/providers/pact-verifier-provider.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { Test } from '@nestjs/testing';
import { Verifier } from '@pact-foundation/pact-core';
import { Verifier } from '@pact-foundation/pact';
import { PactModuleProviders } from '../common/pact-module-providers.enum';
import { PactVerifierProvider } from './pact-verifier.provider';
import { PactProviderOptions } from '../interfaces/pact-provider-module-options.interface';

const mockVerifier = jest.createMockFromModule<Verifier>('@pact-foundation/pact-core');

mockVerifier.verify = jest.fn();
jest.mock('@pact-foundation/pact', () => ({
Verifier: jest.fn().mockImplementation(() => {
verify: jest.fn();
}),
}));

describe('PactVerifierProvider', () => {
const providerOptions: PactProviderOptions = { providerBaseUrl: 'http://127.0.0.1:80' };
Expand Down
2 changes: 2 additions & 0 deletions src/services/pact-verifier-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ describe('PactVerifierService', () => {
beforeEach(() => {
jest.resetAllMocks();

pactVerifierMock.verifyProvider = jest.fn().mockReturnValue(Promise.resolve('some-result'));

appMock.getUrl = jest.fn().mockResolvedValueOnce('http://127.0.0.1:80');
appMock.listen = jest.fn().mockResolvedValueOnce(true);
appMock.close = jest.fn().mockResolvedValueOnce(true);
Expand Down

0 comments on commit b50c702

Please sign in to comment.