Jest

Jest is a javaScript testing framework.

Of

Converts the arguments to an observable sequence.

1
2
const someSequence = { item: {}, isLoading: false }
const someObservableSequence$ = of(someSequence)

TestBed with mockReturnValue

This can be called inside the tests body to mock out the function getFeatures

1
TestBed.get(SomeFacade).getFeatures = jest.fn().mockReturnValue(of());

Data driven

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
--- with toBeObservable
describe('[childComponentEnabled$]', () => {
[
{ abName: true, expected: true },
{ abName: false, expected: false }
].forEach(({ abName, expected }) =>
it(`should ${expected ? '' : 'not'} use 'child-component-to-maybe-show' if abName feature is ${abName}`, marbles((m) => {
// Arrange
const fixture = TestBed.createComponent(ParentComponent);
const testComponent = fixture.componentInstance;
const abFeature = <IAbFeature> {
name: 'abName',
value: abName.toString(),
winningVariantName: 'OnVariant'
};
TestBed.get(AbFrameworkFacade).getByName = () => of(abFeature);

// Act
fixture.detectChanges();

// Assert
m.expect(testComponent.childComponentEnabled$).toBeObservable('(a|)', {
a: expected
});
}))
);
});

--- passing the array elements by object `testcase`
describe(`getSomeMethod`, () => {
[
{
someObject: {},
someBool: true,
someString: 'hoe',
someEnum: SomeEnum.hoe,
expected: 'foo'
},
{
someObject: {},
someBool: true,
someString: 'bazz',
someEnum: SomeEnum.foo,
expected: 'foo bar'
}
].forEach(testcase => {
it(`does some sweet thing,
testcase ${JSON.stringify(testcase)}`, () => {
// do all the things and set `actual`
const actual = 'wat';
expect(actual).toBe(testcase.expected);
});
});
});

toHaveBeenCalledTimes

Much like .Verify in nunit to assert a method was called. Here the fakeAsync and tick are mocking the passage of time, could be to allow subscriptions ect.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
describe('UnitOfWork', () => {
it('Reason this unit of work should have been called', fakeAsync(() => {
// Arrange
const spy = TestBed.get(SomeService).methodWeAreCheckingWasCalled = jest.fn();

TestBed.get<ISomeFacade>(SomeOtherService).getSomeMethod = jest.fn().mockReturnValue(of({
item: {}, isLoading: false
}));

TestBed.get(SomeServiceWithoutAnInterface).getSomeMethod2 = jest.fn().mockReturnValue(of({
item: {}, isLoading: false }
));

// Act
classUnderTest.ngOnInit();
tick();

// Assert
expect(spy).toHaveBeenCalledTimes(1);
}));
});

TBA

1
2
3
4
5
6
7
toHaveBeenCalledWith

toBeInstanceOf

toBe

toMatchSnapshot

References