-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathjasmine.js
More file actions
37 lines (33 loc) · 1.37 KB
/
jasmine.js
File metadata and controls
37 lines (33 loc) · 1.37 KB
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
import { spyOn, fn } from '@vitest/spy';
/**
* Adds the jasmine interfaces we use in the Karma tests to a Vitest spy.
* Should ultimately be removed and tests updated to use Vitest spies.
* @param {import('@vitest/spy').MockInstance}
*/
function jasmineSpyAdapter(spy) {
Object.defineProperties(spy, {
and: { get: () => spy },
calls: { get: () => spy.mock.calls },
returnValue: { value: () => spy.mockReturnValue() },
// calling mockImplementation() with nothing restores the original
callThrough: { value: () => spy.mockImplementation() },
callFake: { value: (impl) => spy.mockImplementation(impl) },
});
Object.defineProperties(spy.mock.calls, {
// Must be non-enumerable for equality checks to work on array literal expected values
allArgs: { value: () => spy.mock.calls },
count: { value: () => spy.mock.calls.length },
reset: { value: () => spy.mockReset() },
argsFor: { value: (index) => spy.mock.calls.at(index) },
});
return spy;
}
export const jasmineSpyOn = (object, prop) => jasmineSpyAdapter(spyOn(object, prop));
export const jasmine = {
any: expect.any,
arrayWithExactContents: () => {
throw new Error('TODO: jasmine.arrayWithExactContents');
},
createSpy: (name, impl) => jasmineSpyAdapter(fn(impl)),
objectContaining: expect.objectContaining,
};