Gathering detailed insights and metrics for @morgan-stanley/ts-mocking-bird
Gathering detailed insights and metrics for @morgan-stanley/ts-mocking-bird
Gathering detailed insights and metrics for @morgan-stanley/ts-mocking-bird
Gathering detailed insights and metrics for @morgan-stanley/ts-mocking-bird
npm install @morgan-stanley/ts-mocking-bird
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
20 Stars
360 Commits
5 Forks
7 Watching
7 Branches
6 Contributors
Updated on 22 Nov 2024
TypeScript (97.94%)
JavaScript (2.06%)
Cumulative downloads
Total Downloads
Last day
-11.4%
4,357
Compared to previous day
Last week
12.9%
27,207
Compared to previous week
Last month
26.3%
103,079
Compared to previous month
Last year
-61.4%
855,097
Compared to previous year
3
41
A fully type safe mocking, call verification and import replacement library that works with jasmine and jest
Documentation: https://morganstanley.github.io/ts-mocking-bird/
Requires a minimum Typescript version of 4.2.
This library has been tested with and supports Jasmine versions 1 and 2 and Jest versions 26 and 27. The mocking functionality should work in any environment as it has no dependencies on any particular framework. The import replacement functionality uses the jasmine beforeAll
/ afterAll
and beforeEach
/ afterEach
so will not work in other environments.
1npm install @morgan-stanley/ts-mocking-bird
Creates a mock that is typed as IMyService
. Properties and functions can be setup using mockedService
. The actual mocked service is accessible at mockedService.mock
.
1import { 2 defineProperty, 3 defineStaticProperty, 4 IMocked, 5 Mock, 6 setupFunction, 7 setupProperty, 8 setupStaticFunction, 9} from '@morgan-stanley/ts-mocking-bird'; 10 11const mockedService: IMocked<IMyService> = Mock.create<IMyService>().setup( 12 setupFunction('functionOne'), // allows the function to be called and allows verification of calls 13 setupFunction('functionTwo', (value: string) => (value === 'something' ? true : false)), // specifies return value 14 setupProperty('propOne', 'initialValue'), 15 defineProperty('propTwo', () => 'getter return value', (value: string) => console.log(`setter called: ${value}`)), // define getter and setter mocks 16); 17 18const systemUnderTest = new SUT(mockedService.mock); // pass mock instance to system under test
Creates a mock that has statics and can be instantiated using new mockedService.mock()
.
1const mockedService = Mock.create<MyService, typeof MyService>().setup( 2 setupStaticFunction('staticFunctionOne', () => 'mockedReturnValue'), 3 defineStaticProperty('staticPropOne', () => 'mockedStaticGetter'), 4); 5 6const systemUnderTest = new ClassWithConstructorArgument(mockedService.mockConstructor); // pass mock constructor to system under test
When a mock is created using Mock.create()
the custom matchers that are used by ts-mocking-bird
are automatically setup. However this can only be done if the creation occurs within a before
function. If you need to manually setup the custom matchers please call addMatchers()
:
1import { addMatchers } from '@morgan-stanley/ts-mocking-bird' 2 3describe("my test", () => { 4 beforeEach(() => { 5 addMatchers(); 6 }) 7})
1const mockedService: IMocked<IMyService> = Mock.create<IMyService>().setup(setupFunction('functionOne')); 2 3const systemUnderTest = new ClassWithInstanceArgument(mockedService.mock); 4 5expect(mockedService.withFunction('functionOne')).wasCalled(5);
1const mockedService: IMocked<IMyService> = Mock.create<IMyService>().setup(setupFunction('functionOne')); 2 3const systemUnderTest = new ClassWithInstanceArgument(mockedService.mock); 4 5expect( 6 mockedService 7 .withFunction('functionTwo') 8 .withParameters('someValue') 9 .strict(), 10).wasCalledOnce();
1const mockInstance = Mock.create<ClassWithInstanceArgument, typeof ClassWithInstanceArgument>().setup(
2 setupConstructor(),
3 );
4
5new mockInstance.mockConstructor(serviceInstance);
6
7expect(mockInstance.withConstructor().withParameters(serviceInstance)).wasCalledOnce();
8
1const mockedService: IMocked<IMyService> = Mock.create<IMyService>().setup(setupProperty('propOne')); 2 3const systemUnderTest = new ClassWithInstanceArgument(mockedService.mock); 4 5expect(mockedService.withGetter('propOne')).wasCalledOnce();
1const mockedService: IMocked<IMyService> = Mock.create<IMyService>().setup(setupProperty('propOne')); 2 3const systemUnderTest = new ClassWithInstanceArgument(mockedService.mock); 4 5expect( 6 mockedService 7 .withSetter('propOne') 8 .withParameters('someValue') 9 .strict(), 10).wasCalledOnce();
If we use strict()
we ensure that the function is ONLY called with the specified parameters
1const mockedService: IMocked<IMyService> = Mock.create<IMyService>().setup(setupFunction('functionTwo')); 2 3const systemUnderTest = new ClassWithInstanceArgument(mockedService.mock); 4 5systemUnderTest.functionsTwo('someValue'); 6systemUnderTest.functionsTwo('someOtherValue'); 7 8expect( 9 mockedService 10 .withFunction('functionTwo') 11 .withParameters('someValue') 12 .strict() 13 ).wasCalledOnce(); // this will fail as called twice in total 14 15expect( 16 mockedService 17 .withFunction('functionTwo') 18 .withParameters('someValue') 19 ).wasCalledOnce(); // this will pass as only called once with params 'someValue'
myMock.setupFunction()
:1const mockedService: IMocked<IMyService> = Mock.create<IMyService>(); 2const functionVerifier = mockedService.setupFunction('functionTwo'); 3 4const systemUnderTest = new ClassWithInstanceArgument(mockedService.mock); 5 6expect(functionVerifier.withParameters('someValue')).wasCalledOnce();
1const sampleMock = Mock.create<ISampleMocked>().setup(setupFunction('functionOne')); 2 3const sampleObject: IPerson = { name: 'Fred', id: 1 }; 4sampleMock.mock.functionOne('one', 2, sampleObject); 5 6expect( 7 sampleMock 8 .withFunction('functionOne') 9 .withParameters('one', 2, sampleObject) // strict equality 10 ).wasCalledOnce();
1const sampleMock = Mock.create<ISampleMocked>().setup(setupFunction('functionOne')); 2 3sampleMock.mock.functionOne('one', 2, { name: 'Fred', id: 1 }); 4 5expect( 6 sampleMock 7 .withFunction('functionOne') 8 .withParametersEqualTo('one', 2, { name: 'Fred', id: 1 }) // equals used to match 9 ).wasCalledOnce();
1import { toBeDefined, any } from "@morgan-stanley/ts-mocking-bird" 2 3const sampleMock = Mock.create<ISampleMocked>().setup(setupFunction('functionOne')); 4 5sampleMock.mock.functionOne('one', 2, { name: 'Fred', id: 1 }); 6 7expect( 8 sampleMock 9 .withFunction('functionOne') 10 .withParameters('one', toBeDefined(), any()) 11 ).wasCalledOnce();
A function with a signature of (value: T) => boolean
can be used to match a parameter value but this will not provide information about what the expected parameter value is in the test failure message.
1const sampleMock = Mock.create<ISampleMocked>().setup(setupFunction('functionOne')); 2 3sampleMock.mock.functionOne('one', 2, { name: 'Fred', id: 1 }); 4 5expect( 6 sampleMock 7 .withFunction('functionOne') 8 .withParameters('one', 2, person => person.id === 1) 9 ).wasCalledOnce();
IParameterMatcher
to create more informative failure messages1const sampleMock = Mock.create<ISampleMocked>().setup(setupFunction('functionOne')); 2 3sampleMock.mock.functionOne('one', 2, { name: 'Fred', id: 1 }); 4 5expect( 6 sampleMock 7 .withFunction('functionOne') 8 .withParameters(toBe('one'), toBe(2), { 9 isExpectedValue: person => person.id === 1, 10 expectedDisplayValue: 'Person with id 1', // Used to display expected parameter value in failure message 11 parameterToString: person => `Person with id ${person.id}`, // Used to display value of actual parameters passed in failure message 12 }) 13 ).wasCalledOnce();
Using proxyModule
we proxy all functions and constructors in a module so that they can be replaced at a later point. This allows us to create a new mock implementation of a class or function for each test run and means that concurrent tests are not polluted by the state of previous tests.
1import { proxyModule, registerMock, reset } from '@morgan-stanley/ts-mocking-bird'; 2 3import * as originalModule from "modulePath"; 4 5const proxiedModule = proxyModule(originalModule); 6 7describe("my-system-under-test", () => { 8 9 mockImports(originalModule, proxiedModule); 10 11 beforeEach(() => { 12 const mockedClass = Mock.create<ClassToMock>(); 13 14 registerMock(proxiedModule, {ClassToMock: mockedClass}) 15 }); 16 17 afterEach(() => { 18 reset(proxiedModule); 19 }) 20 21});
Using proxyJestModule
we register our proxied module with jest.
1import * as moduleProxy from "../../relative-import-path"; 2 3jest.mock('../../relative-import-path', () => 4 require('@morgan-stanley/ts-mocking-bird').proxyJestModule( 5 require.resolve('../../relative-import-path'), 6 ), 7); 8 9describe("my-system-under-test", () => { 10 11 beforeEach(() => { 12 const mockedClass = Mock.create<ClassToMock>(); 13 14 registerMock(moduleProxy, {ClassToMock: mockedClass}) 15 }); 16 17 afterEach(() => { 18 reset(proxiedModule); 19 }) 20 21});
This works in a node environment (replaceProperties
does not due to the way require works) and is a more reliable way of mocking imports as jest hoists this mocking code above all other imports so it is guaranteed to run before the members of the module are imported into the system under test. For this to work the following must be observed:
jest.mock
function is hoisted it can't refer to any variables outside the function. This is why require('@morgan-stanley/ts-mocking-bird')
is used rather than using an existing import. The import path for the module must also be specified multiple times rather than using a variable for the same reason.jest.mock
, passed to proxyJestModule
and used to import the moduleProxy
must all point to same location. For example if a barrel is being imported then all 3 paths must point to same barrel.proxyJestModule
must either be an ambient import such as fs
or path
, a non relative import such as @morgan-stanley/my-dependency-name
or it must be an absolute path. If a relative path such as ../../main/myImport
is used this path will not be resolvable from the proxyJestModule
function. To get the absolute path use require.resolve('../../relative-import-path')
1import * as myImport from './exampleImports'; 2 3describe('replace imports', () => { 4 const someFunctionMock = () => 'mockedReturnValue'; 5 6 replaceProperties(myImport, { someFunction: someFunctionMock }); 7 8 it('should replace function import', () => { 9 const SUT = new ClassUsingImports('one', 2); 10 11 expect(SUT.someFunctionCallingMockedImport()).toEqual('mockedReturnValue'); // value comes from mock above, not original import 12 }); 13});
1import * as myImport from './exampleImports'; 2 3describe('replace imports', () => { 4 5 describe('create new mock before each test', () => { 6 let mockService: IMocked<IMyService>; 7 let mockPackage: IMocked<typeof myImport>; 8 9 replacePropertiesBeforeEach(() => { 10 mockService = Mock.create<IMyService>(); 11 mockPackage = Mock.create<typeof myImport>().setup(setupFunction('someFunction')); // recreate mocks for each test run to reset call counts 12 13 return [{ package: myImport, mocks: { ...mockPackage.mock, MyService: mockService.mockConstructor } }]; 14 }); 15 16 it('so that we can assert number of calls', () => { 17 const SUT = new ClassUsingImports('one', 2); 18 19 expect(SUT.service).toBe(mockService.mock); 20 expect(mockPackage.withFunction('someFunction')).wasNotCalled(); 21 22 SUT.someFunctionProxy(); 23 expect(mockPackage.withFunction('someFunction')).wasCalledOnce(); 24 }); 25 }); 26});
If you get an error such as
TypeError: Cannot redefine property: BUILD_ID
This is most likely because webpack 4 uses configurable:false
when defining properties on import objects. This means that we are unable to replace them when we want to mock them.
This situation is handled by this mocking library but for it to be fully effective the mocking library needs to be imported before any other code - in other words before webpack has a chance to create any import objects.
To do this simply import this library before you import your tests or any other code.
For example:
1import "@morgan-stanley/ts-mocking-bird"; // monkey patch Object.defineProperty before any other code runs 2 3// Find all the tests. 4const context = (require as any).context('./', true, /.spec.ts$/); 5// And load the modules. 6context.keys().map(context);
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
11 out of 11 merged PRs checked by a CI test -- score normalized to 10
Reason
all changesets reviewed
Reason
no dangerous workflow patterns detected
Reason
update tool detected
Details
Reason
license file detected
Details
Reason
30 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 10
Reason
SAST tool is run on all commits
Details
Reason
security policy file detected
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 7
Details
Reason
project has 2 contributing companies or organizations -- score normalized to 6
Details
Reason
4 existing vulnerabilities detected
Details
Reason
branch protection is not maximal on development and all release branches
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Score
Last Scanned on 2024-11-27T13:40:08Z
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More