-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcalculator.test.ts
100 lines (81 loc) · 2.46 KB
/
calculator.test.ts
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { Feature, IGherkinTableParam } from '../';
const getNumbers = (state: {} = {}, a: number, b: number) => {
return {
...state,
a, b,
};
};
const addNumbers = (state: { a: number, b: number }) => {
const { a, b } = state;
return {
...state,
c: a + b,
};
};
const subtractNumbers = (state: { a: number, b: number }) => {
const { a, b } = state;
return {
...state,
c: a - b,
};
};
const multiplyNumbers = (state: { a: number, b: number }) => {
const { a, b } = state;
return {
...state,
c: a * b,
};
};
const checkResult = ({ c }: { c: number }, expected: number) => {
expect(c).toBe(expected);
};
const afterAllFn = jest.fn();
const beforeAllFn = jest.fn();
const beforeEachFn = jest.fn();
const afterEachFn = jest.fn();
Feature('./calculator.feature', () => {
expect(1).toBe(1);
});
Feature({ feature: './calculator.feature', methods: { afterAll, beforeAll, describe, test } }, ({
Scenario, Background, ScenarioOutline,
AfterAll, BeforeAll, AfterEach, BeforeEach,
}) => {
Background('Calculator')
.Given('I can calculate', () => {
expect(Math).toBeTruthy();
});
Scenario('A simple addition test')
.Given('I have the following numbers:', (state = {}, table: IGherkinTableParam) => {
const [{ a, b }] = table.rows.mapByTop();
return {
...state,
a: parseInt(a, 10), b: parseInt(b, 10),
};
})
.When('I add the numbers', addNumbers)
.And('I do nothing', (state) => state)
.Then('I get', (state, text: string) => {
expect(state.c).toBe(parseInt(text, 10));
});
Scenario.skip('A simple multiplication test')
.Given(/^I have numbers (\d+) and (\d+)$/, getNumbers)
.When('I multiply the numbers', multiplyNumbers)
.Then('I get {int}', checkResult);
ScenarioOutline('A simple subtraction test')
.Given('I have numbers {int} and {int}', getNumbers)
.When('I subtract the numbers', subtractNumbers)
.Then('I get {int}', checkResult);
AfterAll(afterAllFn);
BeforeAll(beforeAllFn);
BeforeEach(beforeEachFn);
AfterEach(afterEachFn);
});
it('runs a Feature', () => {
const expectedAfterAllCalls = 1;
// Wait for the tests to be run
while (afterAllFn.mock.calls.length < expectedAfterAllCalls) { /**/ }
expect(afterAllFn).toHaveBeenCalledTimes(expectedAfterAllCalls);
expect(beforeAllFn).toHaveBeenCalledTimes(1);
expect(beforeEachFn).toHaveBeenCalledTimes(3);
expect(afterEachFn).toHaveBeenCalledTimes(3);
}, 10000);