expect-webdriverio 5.3.3 → 5.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/jasmine.d.ts +1 -1
- package/lib/{src/matchers → matchers}/element/toHaveAttribute.js +1 -1
- package/lib/{tsconfig.tsbuildinfo → tsconfig.build.tsbuildinfo} +1 -1
- package/package.json +5 -3
- package/types/standalone.d.ts +2 -2
- package/lib/test/__fixtures__/utils.js +0 -19
- package/lib/test/index.test.js +0 -8
- package/lib/test/matchers/beMatchers.test.js +0 -109
- package/lib/test/matchers/browser/toHaveClipboardText.test.js +0 -21
- package/lib/test/matchers/browserMatchers.test.js +0 -102
- package/lib/test/matchers/element/toBeDisabled.test.js +0 -121
- package/lib/test/matchers/element/toHaveAttribute.test.js +0 -131
- package/lib/test/matchers/element/toHaveChildren.test.js +0 -62
- package/lib/test/matchers/element/toHaveComputedLabel.test.js +0 -284
- package/lib/test/matchers/element/toHaveComputedRole.test.js +0 -284
- package/lib/test/matchers/element/toHaveElementClass.test.js +0 -125
- package/lib/test/matchers/element/toHaveElementProperty.test.js +0 -111
- package/lib/test/matchers/element/toHaveHTML.test.js +0 -291
- package/lib/test/matchers/element/toHaveHeight.test.js +0 -171
- package/lib/test/matchers/element/toHaveHref.test.js +0 -54
- package/lib/test/matchers/element/toHaveId.test.js +0 -54
- package/lib/test/matchers/element/toHaveSize.test.js +0 -109
- package/lib/test/matchers/element/toHaveStyle.test.js +0 -199
- package/lib/test/matchers/element/toHaveText.test.js +0 -333
- package/lib/test/matchers/element/toHaveValue.test.js +0 -81
- package/lib/test/matchers/element/toHaveWidth.test.js +0 -171
- package/lib/test/matchers/elements/toBeElementsArrayOfSize.test.js +0 -141
- package/lib/test/matchers/mock/toBeRequested.test.js +0 -73
- package/lib/test/matchers/mock/toBeRequestedTimes.test.js +0 -108
- package/lib/test/matchers/mock/toBeRequestedWith.test.js +0 -343
- package/lib/test/matchers.test.js +0 -54
- package/lib/test/snapshot.test.js +0 -53
- package/lib/test/softAssertions.test.js +0 -332
- package/lib/test/util/elementsUtil.test.js +0 -18
- package/lib/test/util/formatMessage.test.js +0 -96
- package/lib/test/util/refetchElements.test.js +0 -19
- package/lib/test/utils.test.js +0 -117
- /package/lib/{src/constants.js → constants.js} +0 -0
- /package/lib/{src/index.js → index.js} +0 -0
- /package/lib/{src/jasmineUtils.js → jasmineUtils.js} +0 -0
- /package/lib/{src/matchers → matchers}/browser/toHaveClipboardText.js +0 -0
- /package/lib/{src/matchers → matchers}/browser/toHaveTitle.js +0 -0
- /package/lib/{src/matchers → matchers}/browser/toHaveUrl.js +0 -0
- /package/lib/{src/matchers → matchers}/element/toBeClickable.js +0 -0
- /package/lib/{src/matchers → matchers}/element/toBeDisabled.js +0 -0
- /package/lib/{src/matchers → matchers}/element/toBeDisplayed.js +0 -0
- /package/lib/{src/matchers → matchers}/element/toBeDisplayedInViewport.js +0 -0
- /package/lib/{src/matchers → matchers}/element/toBeEnabled.js +0 -0
- /package/lib/{src/matchers → matchers}/element/toBeExisting.js +0 -0
- /package/lib/{src/matchers → matchers}/element/toBeFocused.js +0 -0
- /package/lib/{src/matchers → matchers}/element/toBeSelected.js +0 -0
- /package/lib/{src/matchers → matchers}/element/toHaveChildren.js +0 -0
- /package/lib/{src/matchers → matchers}/element/toHaveClass.js +0 -0
- /package/lib/{src/matchers → matchers}/element/toHaveComputedLabel.js +0 -0
- /package/lib/{src/matchers → matchers}/element/toHaveComputedRole.js +0 -0
- /package/lib/{src/matchers → matchers}/element/toHaveElementProperty.js +0 -0
- /package/lib/{src/matchers → matchers}/element/toHaveHTML.js +0 -0
- /package/lib/{src/matchers → matchers}/element/toHaveHeight.js +0 -0
- /package/lib/{src/matchers → matchers}/element/toHaveHref.js +0 -0
- /package/lib/{src/matchers → matchers}/element/toHaveId.js +0 -0
- /package/lib/{src/matchers → matchers}/element/toHaveSize.js +0 -0
- /package/lib/{src/matchers → matchers}/element/toHaveStyle.js +0 -0
- /package/lib/{src/matchers → matchers}/element/toHaveText.js +0 -0
- /package/lib/{src/matchers → matchers}/element/toHaveValue.js +0 -0
- /package/lib/{src/matchers → matchers}/element/toHaveWidth.js +0 -0
- /package/lib/{src/matchers → matchers}/elements/toBeElementsArrayOfSize.js +0 -0
- /package/lib/{src/matchers → matchers}/mock/toBeRequested.js +0 -0
- /package/lib/{src/matchers → matchers}/mock/toBeRequestedTimes.js +0 -0
- /package/lib/{src/matchers → matchers}/mock/toBeRequestedWith.js +0 -0
- /package/lib/{src/matchers → matchers}/snapshot.js +0 -0
- /package/lib/{src/matchers.js → matchers.js} +0 -0
- /package/lib/{src/snapshot.js → snapshot.js} +0 -0
- /package/lib/{src/softAssert.js → softAssert.js} +0 -0
- /package/lib/{src/softAssertService.js → softAssertService.js} +0 -0
- /package/lib/{src/softExpect.js → softExpect.js} +0 -0
- /package/lib/{src/types.js → types.js} +0 -0
- /package/lib/{src/util → util}/elementsUtil.js +0 -0
- /package/lib/{src/util → util}/executeCommand.js +0 -0
- /package/lib/{src/util → util}/formatMessage.js +0 -0
- /package/lib/{src/util → util}/refetchElements.js +0 -0
- /package/lib/{src/utils.js → utils.js} +0 -0
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
import { vi, test, describe, expect, beforeEach } from 'vitest';
|
|
2
|
-
import { $$ } from '@wdio/globals';
|
|
3
|
-
import { getExpectMessage, getReceived, getExpected } from '../../__fixtures__/utils.js';
|
|
4
|
-
import { toBeElementsArrayOfSize } from '../../../src/matchers/elements/toBeElementsArrayOfSize.js';
|
|
5
|
-
const createMockElementArray = (length) => {
|
|
6
|
-
const array = Array.from({ length }, () => ({}));
|
|
7
|
-
const mockArray = {
|
|
8
|
-
selector: 'parent',
|
|
9
|
-
get length() { return array.length; },
|
|
10
|
-
set length(newLength) { array.length = newLength; },
|
|
11
|
-
parent: {
|
|
12
|
-
$: vi.fn(),
|
|
13
|
-
$$: vi.fn().mockReturnValue(array),
|
|
14
|
-
},
|
|
15
|
-
foundWith: '$$',
|
|
16
|
-
props: [],
|
|
17
|
-
[Symbol.iterator]: array[Symbol.iterator].bind(array),
|
|
18
|
-
filter: vi.fn().mockReturnThis(),
|
|
19
|
-
map: vi.fn().mockReturnThis(),
|
|
20
|
-
find: vi.fn().mockReturnThis(),
|
|
21
|
-
forEach: vi.fn(),
|
|
22
|
-
some: vi.fn(),
|
|
23
|
-
every: vi.fn(),
|
|
24
|
-
slice: vi.fn().mockReturnThis(),
|
|
25
|
-
toArray: vi.fn().mockReturnThis(),
|
|
26
|
-
};
|
|
27
|
-
return Object.assign(array, mockArray);
|
|
28
|
-
};
|
|
29
|
-
vi.mock('@wdio/globals', () => ({
|
|
30
|
-
$$: vi.fn().mockImplementation(() => createMockElementArray(2))
|
|
31
|
-
}));
|
|
32
|
-
describe('toBeElementsArrayOfSize', () => {
|
|
33
|
-
let els;
|
|
34
|
-
beforeEach(() => {
|
|
35
|
-
els = $$('parent');
|
|
36
|
-
});
|
|
37
|
-
describe('success', () => {
|
|
38
|
-
test('array of size 2', async () => {
|
|
39
|
-
const beforeAssertion = vi.fn();
|
|
40
|
-
const afterAssertion = vi.fn();
|
|
41
|
-
const result = await toBeElementsArrayOfSize.call({}, els, 2, { beforeAssertion, afterAssertion });
|
|
42
|
-
expect(result.pass).toBe(true);
|
|
43
|
-
expect(beforeAssertion).toBeCalledWith({
|
|
44
|
-
matcherName: 'toBeElementsArrayOfSize',
|
|
45
|
-
expectedValue: 2,
|
|
46
|
-
options: { beforeAssertion, afterAssertion }
|
|
47
|
-
});
|
|
48
|
-
expect(afterAssertion).toBeCalledWith({
|
|
49
|
-
matcherName: 'toBeElementsArrayOfSize',
|
|
50
|
-
expectedValue: 2,
|
|
51
|
-
options: { beforeAssertion, afterAssertion },
|
|
52
|
-
result
|
|
53
|
-
});
|
|
54
|
-
});
|
|
55
|
-
test('array of size 5', async () => {
|
|
56
|
-
els = createMockElementArray(5);
|
|
57
|
-
const result = await toBeElementsArrayOfSize.call({}, els, 5, {});
|
|
58
|
-
expect(result.pass).toBe(true);
|
|
59
|
-
});
|
|
60
|
-
});
|
|
61
|
-
describe('failure', () => {
|
|
62
|
-
let result;
|
|
63
|
-
beforeEach(async () => {
|
|
64
|
-
result = await toBeElementsArrayOfSize.call({}, els, 5, {});
|
|
65
|
-
});
|
|
66
|
-
test('fails', () => {
|
|
67
|
-
expect(result.pass).toBe(false);
|
|
68
|
-
});
|
|
69
|
-
describe('message shows correctly', () => {
|
|
70
|
-
test('expect message', () => {
|
|
71
|
-
expect(getExpectMessage(result.message())).toContain('to be elements array of size');
|
|
72
|
-
});
|
|
73
|
-
test('expected message', () => {
|
|
74
|
-
expect(getExpected(result.message())).toContain('5');
|
|
75
|
-
});
|
|
76
|
-
test('received message', () => {
|
|
77
|
-
expect(getReceived(result.message())).toContain('2');
|
|
78
|
-
});
|
|
79
|
-
});
|
|
80
|
-
});
|
|
81
|
-
describe('error catching', () => {
|
|
82
|
-
test('throws error with incorrect size param', async () => {
|
|
83
|
-
await expect(toBeElementsArrayOfSize.call({}, els, '5')).rejects.toThrow('Invalid params passed to toBeElementsArrayOfSize.');
|
|
84
|
-
});
|
|
85
|
-
test('works if size contains options', async () => {
|
|
86
|
-
const result = await toBeElementsArrayOfSize.call({}, els, { lte: 5 });
|
|
87
|
-
expect(result.pass).toBe(true);
|
|
88
|
-
});
|
|
89
|
-
});
|
|
90
|
-
describe('number options', () => {
|
|
91
|
-
test.each([
|
|
92
|
-
['lte', 10, true],
|
|
93
|
-
['lte', 1, false],
|
|
94
|
-
['gte', 1, true],
|
|
95
|
-
['gte', 10, false],
|
|
96
|
-
['gte and lte', { gte: 1, lte: 10 }, true],
|
|
97
|
-
['not gte but is lte', { gte: 10, lte: 10 }, false],
|
|
98
|
-
['not lte but is gte', { gte: 1, lte: 1 }, false],
|
|
99
|
-
])('should handle %s correctly', async (_, option, expected) => {
|
|
100
|
-
const result = await toBeElementsArrayOfSize.call({}, els, typeof option === 'object' ? option : { [_]: option });
|
|
101
|
-
expect(result.pass).toBe(expected);
|
|
102
|
-
});
|
|
103
|
-
});
|
|
104
|
-
describe('array update', () => {
|
|
105
|
-
test('updates the received array when assertion passes', async () => {
|
|
106
|
-
const receivedArray = createMockElementArray(2);
|
|
107
|
-
receivedArray.parent._length = 5;
|
|
108
|
-
receivedArray.parent.$$ = vi.fn().mockReturnValue(createMockElementArray(5));
|
|
109
|
-
const result = await toBeElementsArrayOfSize.call({}, receivedArray, 5);
|
|
110
|
-
expect(result.pass).toBe(true);
|
|
111
|
-
expect(receivedArray.length).toBe(5);
|
|
112
|
-
});
|
|
113
|
-
test('does not update the received array when assertion fails', async () => {
|
|
114
|
-
const receivedArray = createMockElementArray(2);
|
|
115
|
-
const result = await toBeElementsArrayOfSize.call({}, receivedArray, 10);
|
|
116
|
-
expect(result.pass).toBe(false);
|
|
117
|
-
expect(receivedArray.length).toBe(2);
|
|
118
|
-
});
|
|
119
|
-
test('does not modify non-array received values', async () => {
|
|
120
|
-
const nonArrayEls = {
|
|
121
|
-
selector: 'parent',
|
|
122
|
-
length: 2,
|
|
123
|
-
parent: {
|
|
124
|
-
$: vi.fn(),
|
|
125
|
-
$$: vi.fn().mockReturnValue(createMockElementArray(5)),
|
|
126
|
-
},
|
|
127
|
-
foundWith: '$$',
|
|
128
|
-
props: [],
|
|
129
|
-
};
|
|
130
|
-
const result = await toBeElementsArrayOfSize.call({}, nonArrayEls, 5);
|
|
131
|
-
expect(result.pass).toBe(true);
|
|
132
|
-
expect(nonArrayEls.length).toBe(2);
|
|
133
|
-
});
|
|
134
|
-
test('does not alter the array when checking', async () => {
|
|
135
|
-
const receivedArray = createMockElementArray(2);
|
|
136
|
-
const result = await toBeElementsArrayOfSize.call({}, receivedArray, 2);
|
|
137
|
-
expect(result.pass).toBe(true);
|
|
138
|
-
expect(receivedArray.length).toBe(2);
|
|
139
|
-
});
|
|
140
|
-
});
|
|
141
|
-
});
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import { vi, test, describe, expect } from 'vitest';
|
|
2
|
-
import { toBeRequested } from '../../../src/matchers/mock/toBeRequested.js';
|
|
3
|
-
import { getExpected, getExpectMessage, getReceived, removeColors } from '../../__fixtures__/utils.js';
|
|
4
|
-
vi.mock('@wdio/globals');
|
|
5
|
-
class TestMock {
|
|
6
|
-
constructor() {
|
|
7
|
-
this.on = vi.fn();
|
|
8
|
-
this._calls = [];
|
|
9
|
-
}
|
|
10
|
-
get calls() {
|
|
11
|
-
return this._calls;
|
|
12
|
-
}
|
|
13
|
-
abort() { return Promise.resolve(); }
|
|
14
|
-
abortOnce() { return Promise.resolve(); }
|
|
15
|
-
respond() { return Promise.resolve(); }
|
|
16
|
-
respondOnce() { return Promise.resolve(); }
|
|
17
|
-
clear() { return Promise.resolve(); }
|
|
18
|
-
restore() { return Promise.resolve(); }
|
|
19
|
-
waitForResponse() { return Promise.resolve(true); }
|
|
20
|
-
}
|
|
21
|
-
const mockMatch = {
|
|
22
|
-
body: 'foo',
|
|
23
|
-
url: '/foo/bar',
|
|
24
|
-
method: 'POST',
|
|
25
|
-
headers: {},
|
|
26
|
-
responseHeaders: {},
|
|
27
|
-
statusCode: 200,
|
|
28
|
-
initialPriority: 'Low',
|
|
29
|
-
referrerPolicy: 'origin'
|
|
30
|
-
};
|
|
31
|
-
describe('toBeRequested', () => {
|
|
32
|
-
test('wait for success', async () => {
|
|
33
|
-
const mock = new TestMock();
|
|
34
|
-
const result = await toBeRequested(mock);
|
|
35
|
-
expect(result.pass).toBe(false);
|
|
36
|
-
setTimeout(() => {
|
|
37
|
-
mock.calls.push(mockMatch);
|
|
38
|
-
mock.calls.push(mockMatch);
|
|
39
|
-
}, 10);
|
|
40
|
-
const beforeAssertion = vi.fn();
|
|
41
|
-
const afterAssertion = vi.fn();
|
|
42
|
-
const result2 = await toBeRequested(mock, { beforeAssertion, afterAssertion });
|
|
43
|
-
expect(result2.pass).toBe(true);
|
|
44
|
-
expect(beforeAssertion).toBeCalledWith({
|
|
45
|
-
matcherName: 'toBeRequestedTimes',
|
|
46
|
-
expectedValue: { gte: 1 },
|
|
47
|
-
options: { beforeAssertion, afterAssertion }
|
|
48
|
-
});
|
|
49
|
-
expect(afterAssertion).toBeCalledWith({
|
|
50
|
-
matcherName: 'toBeRequestedTimes',
|
|
51
|
-
expectedValue: { gte: 1 },
|
|
52
|
-
options: { beforeAssertion, afterAssertion },
|
|
53
|
-
result: result2
|
|
54
|
-
});
|
|
55
|
-
});
|
|
56
|
-
test('not to be called', async () => {
|
|
57
|
-
const mock = new TestMock();
|
|
58
|
-
const result = await toBeRequested.call({ isNot: true }, mock);
|
|
59
|
-
expect(result.pass).toBe(false);
|
|
60
|
-
mock.calls.push(mockMatch);
|
|
61
|
-
const result4 = await toBeRequested.call({ isNot: true }, mock);
|
|
62
|
-
expect(result4.pass).toBe(true);
|
|
63
|
-
});
|
|
64
|
-
test('message', async () => {
|
|
65
|
-
const mock = new TestMock();
|
|
66
|
-
const message = removeColors((await toBeRequested(mock)).message());
|
|
67
|
-
expect(getExpectMessage(message)).toBe('Expect mock to be called');
|
|
68
|
-
expect(getReceived(message)).toBe('Received: 0');
|
|
69
|
-
expect(getExpected(message)).toBe('Expected: ">= 1"');
|
|
70
|
-
const result2 = await toBeRequested.call({ isNot: true }, mock);
|
|
71
|
-
expect(result2.message()).toContain('Expect mock not to be called');
|
|
72
|
-
});
|
|
73
|
-
});
|
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
import { vi, test, describe, expect } from 'vitest';
|
|
2
|
-
import { toBeRequestedTimes } from '../../../src/matchers/mock/toBeRequestedTimes.js';
|
|
3
|
-
import { removeColors, getReceived, getExpected, getExpectMessage } from '../../__fixtures__/utils.js';
|
|
4
|
-
vi.mock('@wdio/globals');
|
|
5
|
-
class TestMock {
|
|
6
|
-
constructor() {
|
|
7
|
-
this.on = vi.fn();
|
|
8
|
-
this._calls = [];
|
|
9
|
-
}
|
|
10
|
-
get calls() {
|
|
11
|
-
return this._calls;
|
|
12
|
-
}
|
|
13
|
-
abort() { return Promise.resolve(); }
|
|
14
|
-
abortOnce() { return Promise.resolve(); }
|
|
15
|
-
respond() { return Promise.resolve(); }
|
|
16
|
-
respondOnce() { return Promise.resolve(); }
|
|
17
|
-
clear() { return Promise.resolve(); }
|
|
18
|
-
restore() { return Promise.resolve(); }
|
|
19
|
-
waitForResponse() { return Promise.resolve(true); }
|
|
20
|
-
}
|
|
21
|
-
const mockMatch = {
|
|
22
|
-
body: 'foo',
|
|
23
|
-
url: '/foo/bar',
|
|
24
|
-
method: 'POST',
|
|
25
|
-
headers: {},
|
|
26
|
-
responseHeaders: {},
|
|
27
|
-
statusCode: 200,
|
|
28
|
-
initialPriority: 'Low',
|
|
29
|
-
referrerPolicy: 'origin'
|
|
30
|
-
};
|
|
31
|
-
describe('toBeRequestedTimes', () => {
|
|
32
|
-
test('wait for success', async () => {
|
|
33
|
-
const mock = new TestMock();
|
|
34
|
-
setTimeout(() => {
|
|
35
|
-
mock.calls.push(mockMatch);
|
|
36
|
-
}, 10);
|
|
37
|
-
const beforeAssertion = vi.fn();
|
|
38
|
-
const afterAssertion = vi.fn();
|
|
39
|
-
const result = await toBeRequestedTimes.call({}, mock, 1, { beforeAssertion, afterAssertion });
|
|
40
|
-
expect(result.pass).toBe(true);
|
|
41
|
-
expect(beforeAssertion).toBeCalledWith({
|
|
42
|
-
matcherName: 'toBeRequestedTimes',
|
|
43
|
-
expectedValue: 1,
|
|
44
|
-
options: { beforeAssertion, afterAssertion }
|
|
45
|
-
});
|
|
46
|
-
expect(afterAssertion).toBeCalledWith({
|
|
47
|
-
matcherName: 'toBeRequestedTimes',
|
|
48
|
-
expectedValue: 1,
|
|
49
|
-
options: { beforeAssertion, afterAssertion },
|
|
50
|
-
result
|
|
51
|
-
});
|
|
52
|
-
});
|
|
53
|
-
test('wait for success using number options', async () => {
|
|
54
|
-
const mock = new TestMock();
|
|
55
|
-
setTimeout(() => {
|
|
56
|
-
mock.calls.push(mockMatch);
|
|
57
|
-
}, 10);
|
|
58
|
-
const result = await toBeRequestedTimes.call({}, mock, { gte: 1 });
|
|
59
|
-
expect(result.pass).toBe(true);
|
|
60
|
-
const result2 = await toBeRequestedTimes.call({}, mock, { eq: 1 });
|
|
61
|
-
expect(result2.pass).toBe(true);
|
|
62
|
-
});
|
|
63
|
-
test('wait but failure', async () => {
|
|
64
|
-
const mock = new TestMock();
|
|
65
|
-
const result = await toBeRequestedTimes.call({}, mock, 1);
|
|
66
|
-
expect(result.pass).toBe(false);
|
|
67
|
-
setTimeout(() => {
|
|
68
|
-
mock.calls.push(mockMatch);
|
|
69
|
-
mock.calls.push(mockMatch);
|
|
70
|
-
}, 10);
|
|
71
|
-
const result2 = await toBeRequestedTimes.call({}, mock, 1);
|
|
72
|
-
expect(result2.pass).toBe(false);
|
|
73
|
-
const result3 = await toBeRequestedTimes.call({}, mock, 2);
|
|
74
|
-
expect(result3.pass).toBe(true);
|
|
75
|
-
const result4 = await toBeRequestedTimes.call({}, mock, { gte: 2 });
|
|
76
|
-
expect(result4.pass).toBe(true);
|
|
77
|
-
const result5 = await toBeRequestedTimes.call({}, mock, { lte: 2 });
|
|
78
|
-
expect(result5.pass).toBe(true);
|
|
79
|
-
const result6 = await toBeRequestedTimes.call({}, mock, { lte: 3 });
|
|
80
|
-
expect(result6.pass).toBe(true);
|
|
81
|
-
});
|
|
82
|
-
test('not to be called', async () => {
|
|
83
|
-
const mock = new TestMock();
|
|
84
|
-
const result = await toBeRequestedTimes.call({ isNot: true }, mock, 0);
|
|
85
|
-
expect(result.pass).toBe(true);
|
|
86
|
-
const result2 = await toBeRequestedTimes.call({ isNot: true }, mock, 1);
|
|
87
|
-
expect(result2.pass).toBe(false);
|
|
88
|
-
mock.calls.push(mockMatch);
|
|
89
|
-
const result3 = await toBeRequestedTimes.call({ isNot: true }, mock, 0);
|
|
90
|
-
expect(result3.pass).toBe(false);
|
|
91
|
-
const result4 = await toBeRequestedTimes.call({ isNot: true }, mock, 1);
|
|
92
|
-
expect(result4.pass).toBe(true);
|
|
93
|
-
});
|
|
94
|
-
test('message', async () => {
|
|
95
|
-
const mock = new TestMock();
|
|
96
|
-
const result = await toBeRequestedTimes.call({}, mock, 0);
|
|
97
|
-
expect(result.message()).toContain('Expect mock to be called 0 times');
|
|
98
|
-
const result2 = await toBeRequestedTimes.call({}, mock, 1);
|
|
99
|
-
expect(result2.message()).toContain('Expect mock to be called 1 time');
|
|
100
|
-
const result3 = await toBeRequestedTimes.call({}, mock, 2);
|
|
101
|
-
expect(result3.message()).toContain('Expect mock to be called 2 times');
|
|
102
|
-
const result4 = await toBeRequestedTimes.call({}, mock, { gte: 3 });
|
|
103
|
-
const message4 = removeColors(result4.message());
|
|
104
|
-
expect(getExpectMessage(message4)).toBe('Expect mock to be called times');
|
|
105
|
-
expect(getExpected(message4)).toBe('Expected: ">= 3"');
|
|
106
|
-
expect(getReceived(message4)).toBe('Received: 0');
|
|
107
|
-
});
|
|
108
|
-
});
|
|
@@ -1,343 +0,0 @@
|
|
|
1
|
-
import { vi, test, describe, expect, beforeEach, afterEach } from 'vitest';
|
|
2
|
-
import { toBeRequestedWith } from '../../../src/matchers/mock/toBeRequestedWith.js';
|
|
3
|
-
import { removeColors, getExpectMessage, getExpected, getReceived } from '../../__fixtures__/utils.js';
|
|
4
|
-
vi.mock('@wdio/globals');
|
|
5
|
-
class TestMock {
|
|
6
|
-
constructor() {
|
|
7
|
-
this._calls = [];
|
|
8
|
-
}
|
|
9
|
-
get calls() {
|
|
10
|
-
return this._calls;
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
function reduceHeaders(headers) {
|
|
14
|
-
return Object.entries(headers).reduce((acc, [, value]) => {
|
|
15
|
-
acc[value.name] = value.value.value;
|
|
16
|
-
return acc;
|
|
17
|
-
}, {});
|
|
18
|
-
}
|
|
19
|
-
const authKey = 'Bearer ' + '2'.repeat(128);
|
|
20
|
-
const mockGet = {
|
|
21
|
-
request: {
|
|
22
|
-
url: 'http://localhost:8080/api/search?pages=20',
|
|
23
|
-
method: 'GET',
|
|
24
|
-
request: '123',
|
|
25
|
-
headersSize: 123,
|
|
26
|
-
bodySize: 123,
|
|
27
|
-
timings: {},
|
|
28
|
-
cookies: [],
|
|
29
|
-
headers: [{
|
|
30
|
-
name: 'Authorization',
|
|
31
|
-
value: { type: 'string', value: authKey }
|
|
32
|
-
}, {
|
|
33
|
-
name: 'foo',
|
|
34
|
-
value: { type: 'string', value: 'bar' }
|
|
35
|
-
}]
|
|
36
|
-
},
|
|
37
|
-
response: {
|
|
38
|
-
headers: {},
|
|
39
|
-
status: 200,
|
|
40
|
-
},
|
|
41
|
-
};
|
|
42
|
-
const mockPost = {
|
|
43
|
-
request: {
|
|
44
|
-
url: 'https://my-app/api/add-tags',
|
|
45
|
-
method: 'POST',
|
|
46
|
-
request: '123',
|
|
47
|
-
headersSize: 123,
|
|
48
|
-
bodySize: 123,
|
|
49
|
-
timings: {},
|
|
50
|
-
cookies: [],
|
|
51
|
-
headers: [{
|
|
52
|
-
name: 'Authorization',
|
|
53
|
-
value: { type: 'string', value: authKey }
|
|
54
|
-
}, {
|
|
55
|
-
name: 'foo',
|
|
56
|
-
value: { type: 'string', value: 'bar' }
|
|
57
|
-
}, {
|
|
58
|
-
name: 'Accept',
|
|
59
|
-
value: { type: 'string', value: '*' }
|
|
60
|
-
}],
|
|
61
|
-
},
|
|
62
|
-
response: {
|
|
63
|
-
status: 201,
|
|
64
|
-
headers: []
|
|
65
|
-
},
|
|
66
|
-
};
|
|
67
|
-
describe('toBeRequestedWith', () => {
|
|
68
|
-
test('wait for success, exact match', async () => {
|
|
69
|
-
const mock = new TestMock();
|
|
70
|
-
setTimeout(() => {
|
|
71
|
-
mock.calls.push({ ...mockGet });
|
|
72
|
-
}, 5);
|
|
73
|
-
setTimeout(() => {
|
|
74
|
-
mock.calls.push({ ...mockGet }, { ...mockPost });
|
|
75
|
-
}, 15);
|
|
76
|
-
const params = {
|
|
77
|
-
url: mockPost.request.url,
|
|
78
|
-
method: mockPost.request.method,
|
|
79
|
-
requestHeaders: {},
|
|
80
|
-
statusCode: mockPost.response.status,
|
|
81
|
-
responseHeaders: {},
|
|
82
|
-
};
|
|
83
|
-
const beforeAssertion = vi.fn();
|
|
84
|
-
const afterAssertion = vi.fn();
|
|
85
|
-
const result = await toBeRequestedWith.call({}, mock, params, { beforeAssertion, afterAssertion });
|
|
86
|
-
expect(result.pass).toBe(true);
|
|
87
|
-
expect(beforeAssertion).toBeCalledWith({
|
|
88
|
-
matcherName: 'toBeRequestedWith',
|
|
89
|
-
expectedValue: params,
|
|
90
|
-
options: { beforeAssertion, afterAssertion },
|
|
91
|
-
});
|
|
92
|
-
expect(afterAssertion).toBeCalledWith({
|
|
93
|
-
matcherName: 'toBeRequestedWith',
|
|
94
|
-
expectedValue: params,
|
|
95
|
-
options: { beforeAssertion, afterAssertion },
|
|
96
|
-
result
|
|
97
|
-
});
|
|
98
|
-
});
|
|
99
|
-
test('wait for failure', async () => {
|
|
100
|
-
const mock = new TestMock();
|
|
101
|
-
setTimeout(() => {
|
|
102
|
-
mock.calls.push({ ...mockGet }, { ...mockPost });
|
|
103
|
-
}, 15);
|
|
104
|
-
const params = {
|
|
105
|
-
url: 'post.url',
|
|
106
|
-
method: 'post.method',
|
|
107
|
-
requestHeaders: {},
|
|
108
|
-
responseHeaders: {}
|
|
109
|
-
};
|
|
110
|
-
const result = await toBeRequestedWith.call({}, mock, params);
|
|
111
|
-
expect(result.pass).toBe(false);
|
|
112
|
-
});
|
|
113
|
-
test('wait for NOT failure, empty params', async () => {
|
|
114
|
-
const mock = new TestMock();
|
|
115
|
-
mock.calls.push({ ...mockGet }, { ...mockPost });
|
|
116
|
-
setTimeout(() => {
|
|
117
|
-
mock.calls.push({ ...mockGet }, { ...mockPost });
|
|
118
|
-
}, 10);
|
|
119
|
-
const result = await toBeRequestedWith.call({ isNot: true }, mock, {});
|
|
120
|
-
expect(result.pass).toBe(true);
|
|
121
|
-
});
|
|
122
|
-
test('wait for NOT success', async () => {
|
|
123
|
-
const mock = new TestMock();
|
|
124
|
-
setTimeout(() => {
|
|
125
|
-
mock.calls.push({ ...mockGet }, { ...mockPost });
|
|
126
|
-
}, 10);
|
|
127
|
-
const result = await toBeRequestedWith.call({ isNot: true }, mock, { method: 'DELETE' });
|
|
128
|
-
expect(result.pass).toBe(false);
|
|
129
|
-
});
|
|
130
|
-
const scenarios = [
|
|
131
|
-
{
|
|
132
|
-
name: 'success, url only',
|
|
133
|
-
mocks: [{ ...mockPost }],
|
|
134
|
-
pass: true,
|
|
135
|
-
params: {
|
|
136
|
-
url: mockPost.request.url,
|
|
137
|
-
},
|
|
138
|
-
},
|
|
139
|
-
{
|
|
140
|
-
name: 'success, method only',
|
|
141
|
-
mocks: [{ ...mockPost }],
|
|
142
|
-
pass: true,
|
|
143
|
-
params: {
|
|
144
|
-
method: ['DELETE', 'PUT', mockPost.request.method, 'GET'],
|
|
145
|
-
},
|
|
146
|
-
},
|
|
147
|
-
{
|
|
148
|
-
name: 'success, statusCode only',
|
|
149
|
-
mocks: [{ ...mockPost }],
|
|
150
|
-
pass: true,
|
|
151
|
-
params: {
|
|
152
|
-
statusCode: [203, 200, 201],
|
|
153
|
-
},
|
|
154
|
-
},
|
|
155
|
-
{
|
|
156
|
-
name: 'success, requestHeaders only',
|
|
157
|
-
mocks: [{ ...mockPost }],
|
|
158
|
-
pass: true,
|
|
159
|
-
params: {
|
|
160
|
-
requestHeaders: {
|
|
161
|
-
Authorization: authKey,
|
|
162
|
-
foo: 'bar',
|
|
163
|
-
Accept: '*'
|
|
164
|
-
},
|
|
165
|
-
},
|
|
166
|
-
},
|
|
167
|
-
{
|
|
168
|
-
name: 'success, responseHeaders only',
|
|
169
|
-
mocks: [{ ...mockPost }],
|
|
170
|
-
pass: true,
|
|
171
|
-
params: {
|
|
172
|
-
responseHeaders: {},
|
|
173
|
-
},
|
|
174
|
-
},
|
|
175
|
-
{
|
|
176
|
-
name: 'failure, url only',
|
|
177
|
-
mocks: [{ ...mockPost }],
|
|
178
|
-
pass: false,
|
|
179
|
-
params: {
|
|
180
|
-
url: '/api/api',
|
|
181
|
-
},
|
|
182
|
-
},
|
|
183
|
-
{
|
|
184
|
-
name: 'failure, method only',
|
|
185
|
-
mocks: [{ ...mockPost }],
|
|
186
|
-
pass: false,
|
|
187
|
-
params: {
|
|
188
|
-
method: ['DELETE', 'PUT'],
|
|
189
|
-
},
|
|
190
|
-
},
|
|
191
|
-
{
|
|
192
|
-
name: 'failure, statusCode only',
|
|
193
|
-
mocks: [{ ...mockPost }],
|
|
194
|
-
pass: false,
|
|
195
|
-
params: {
|
|
196
|
-
statusCode: [400, 401],
|
|
197
|
-
},
|
|
198
|
-
},
|
|
199
|
-
{
|
|
200
|
-
name: 'failure, requestHeaders only',
|
|
201
|
-
mocks: [{ ...mockPost }],
|
|
202
|
-
pass: false,
|
|
203
|
-
params: {
|
|
204
|
-
requestHeaders: { Cache: 'false' },
|
|
205
|
-
},
|
|
206
|
-
},
|
|
207
|
-
{
|
|
208
|
-
name: 'failure, responseHeaders only',
|
|
209
|
-
mocks: [{ ...mockPost }],
|
|
210
|
-
pass: false,
|
|
211
|
-
params: {
|
|
212
|
-
responseHeaders: { Cache: 'false' },
|
|
213
|
-
},
|
|
214
|
-
},
|
|
215
|
-
{
|
|
216
|
-
name: 'special matcher, url',
|
|
217
|
-
mocks: [{ ...mockPost }],
|
|
218
|
-
pass: true,
|
|
219
|
-
params: {
|
|
220
|
-
url: expect.stringMatching(/.*\/API\/.*/i),
|
|
221
|
-
},
|
|
222
|
-
},
|
|
223
|
-
{
|
|
224
|
-
name: 'special matcher, headers',
|
|
225
|
-
mocks: [{ ...mockPost }],
|
|
226
|
-
pass: true,
|
|
227
|
-
params: {
|
|
228
|
-
requestHeaders: expect.objectContaining({
|
|
229
|
-
Authorization: expect.stringContaining('Bearer '),
|
|
230
|
-
}),
|
|
231
|
-
},
|
|
232
|
-
},
|
|
233
|
-
{
|
|
234
|
-
name: 'special matcher, postData',
|
|
235
|
-
mocks: [{ ...mockPost }],
|
|
236
|
-
pass: true,
|
|
237
|
-
params: {
|
|
238
|
-
postData: expect.stringMatching('"search"'),
|
|
239
|
-
},
|
|
240
|
-
},
|
|
241
|
-
{
|
|
242
|
-
name: 'special matcher, response',
|
|
243
|
-
mocks: [{ ...mockPost }],
|
|
244
|
-
pass: true,
|
|
245
|
-
params: {
|
|
246
|
-
response: expect.arrayContaining([expect.objectContaining({ id: 2 })]),
|
|
247
|
-
},
|
|
248
|
-
},
|
|
249
|
-
{
|
|
250
|
-
name: 'function, url',
|
|
251
|
-
mocks: [{ ...mockPost }],
|
|
252
|
-
pass: true,
|
|
253
|
-
params: {
|
|
254
|
-
url: (url) => url.startsWith('https'),
|
|
255
|
-
},
|
|
256
|
-
},
|
|
257
|
-
{
|
|
258
|
-
name: 'function, headers',
|
|
259
|
-
mocks: [{ ...mockPost }],
|
|
260
|
-
pass: true,
|
|
261
|
-
params: {
|
|
262
|
-
requestHeaders: (headers) => headers.foo === 'bar',
|
|
263
|
-
},
|
|
264
|
-
},
|
|
265
|
-
{
|
|
266
|
-
name: 'function, postData',
|
|
267
|
-
mocks: [{ ...mockPost }],
|
|
268
|
-
pass: true,
|
|
269
|
-
params: {
|
|
270
|
-
postData: (r) => JSON.parse(r).length === 2,
|
|
271
|
-
},
|
|
272
|
-
},
|
|
273
|
-
{
|
|
274
|
-
name: 'function, response',
|
|
275
|
-
mocks: [{ ...mockPost }],
|
|
276
|
-
pass: true,
|
|
277
|
-
params: {
|
|
278
|
-
response: (r) => r.includes('id') && r.includes('name'),
|
|
279
|
-
},
|
|
280
|
-
},
|
|
281
|
-
];
|
|
282
|
-
scenarios.forEach((scenario) => {
|
|
283
|
-
test(scenario.name, async () => {
|
|
284
|
-
const mock = new TestMock();
|
|
285
|
-
mock.calls.push(...scenario.mocks);
|
|
286
|
-
const result = await toBeRequestedWith.call({}, mock, scenario.params);
|
|
287
|
-
expect(result.pass).toBe(scenario.pass);
|
|
288
|
-
});
|
|
289
|
-
});
|
|
290
|
-
describe('error messages', () => {
|
|
291
|
-
const consoleError = global.console.error;
|
|
292
|
-
beforeEach(() => {
|
|
293
|
-
global.console.error = vi.fn();
|
|
294
|
-
});
|
|
295
|
-
test('unsupported method', async () => {
|
|
296
|
-
const mock = new TestMock();
|
|
297
|
-
mock.calls.push({ ...mockGet });
|
|
298
|
-
const result = await toBeRequestedWith.call({}, mock, { method: 1234 });
|
|
299
|
-
expect(result.pass).toBe(false);
|
|
300
|
-
expect(global.console.error).toBeCalledWith('expect.toBeRequestedWith: unsupported value passed to method 1234');
|
|
301
|
-
});
|
|
302
|
-
afterEach(() => {
|
|
303
|
-
global.console.error = consoleError;
|
|
304
|
-
});
|
|
305
|
-
});
|
|
306
|
-
test('message', async () => {
|
|
307
|
-
const mock = new TestMock();
|
|
308
|
-
const requested = await toBeRequestedWith.call({}, mock, {
|
|
309
|
-
url: () => false,
|
|
310
|
-
method: ['DELETE', 'PUT'],
|
|
311
|
-
requestHeaders: reduceHeaders(mockPost.request.headers),
|
|
312
|
-
responseHeaders: reduceHeaders(mockPost.response.headers),
|
|
313
|
-
postData: expect.anything(),
|
|
314
|
-
response: [...Array(50).keys()].map((_, id) => ({ id, name: `name_${id}` })),
|
|
315
|
-
});
|
|
316
|
-
const wasNotCalled = removeColors(requested.message());
|
|
317
|
-
expect(getExpectMessage(wasNotCalled)).toBe('Expect mock to be called with');
|
|
318
|
-
expect(getExpected(wasNotCalled)).toBe('Expected: {' +
|
|
319
|
-
'"method": ["DELETE", "PUT"], ' +
|
|
320
|
-
'"postData": "Anything ", ' +
|
|
321
|
-
'"requestHeaders": {"Accept": "*", "Authorization": "Bearer ..2222222", "foo": "bar"}, ' +
|
|
322
|
-
'"response": [{"id": 0, "name": "name_0"}, "... 49 more items"], ' +
|
|
323
|
-
'"responseHeaders": {}, ' +
|
|
324
|
-
'"url": "() => false"}');
|
|
325
|
-
expect(getReceived(wasNotCalled)).toBe('Received: "was not called"');
|
|
326
|
-
mock.calls.push(mockPost);
|
|
327
|
-
const notRequested = await toBeRequestedWith.call({ isNot: true }, mock, {
|
|
328
|
-
url: () => true,
|
|
329
|
-
method: mockPost.request.method,
|
|
330
|
-
});
|
|
331
|
-
const wasCalled = removeColors(notRequested.message());
|
|
332
|
-
expect(wasCalled).toBe(`Expect mock not to be called with
|
|
333
|
-
|
|
334
|
-
- Expected [not] - 1
|
|
335
|
-
+ Received + 1
|
|
336
|
-
|
|
337
|
-
Object {
|
|
338
|
-
"method": "POST",
|
|
339
|
-
- "url": "() => true",
|
|
340
|
-
+ "url": "https://my-app/api/add-tags",
|
|
341
|
-
}`);
|
|
342
|
-
});
|
|
343
|
-
});
|