• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

help-me-mom / ng-mocks / dde53b6d-6b58-46d8-9557-5bd271cd2bf0

pending completion
dde53b6d-6b58-46d8-9557-5bd271cd2bf0

push

circleci

web-flow
Merge pull request #6340 from help-me-mom/renovate/e2e/jest/node-18.16.x

3204 of 3204 branches covered (100.0%)

Branch coverage included in aggregate %.

4754 of 4754 relevant lines covered (100.0%)

7679.54 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/libs/ng-mocks/src/lib/common/core.helpers.ts
1
import { getTestBed } from '@angular/core/testing';
1✔
2

3
import coreDefineProperty from './core.define-property';
1✔
4
import coreReflectParametersResolve from './core.reflect.parameters-resolve';
1✔
5
import { AnyDeclaration, AnyType, Type } from './core.types';
6
import funcGetGlobal from './func.get-global';
1✔
7
import funcGetName from './func.get-name';
1✔
8
import ngMocksUniverse from './ng-mocks-universe';
1✔
9

10
/**
11
 * It will be removed from public interface with the next release: A14
12
 * Use ngMocks.get(token) instead.
13
 *
14
 * @deprecated
15
 * @internal
16
 */
17
export const getTestBedInjection = <I>(token: AnyDeclaration<I>): I | undefined => {
1✔
18
  try {
34✔
19
    // istanbul ignore next
20
    return getInjection(token);
21
  } catch {
22
    return undefined;
23✔
23
  }
24
};
25

26
/**
27
 * It will be removed from public interface with the next release: A14
28
 *
29
 * @deprecated
30
 * @internal
31
 */
32
export const getInjection = <I>(token: AnyDeclaration<I>): I => {
1✔
33
  const testBed: any = getTestBed();
159✔
34

35
  // istanbul ignore next
36
  return testBed.inject ? testBed.inject(token) : testBed.get(token);
37
};
38

39
export const flatten = <T>(values: T | T[], result: T[] = []): T[] => {
360,220✔
40
  if (Array.isArray(values)) {
279,839✔
41
    for (const value of values) {
199,458✔
42
      flatten(value, result);
199,458✔
43
    }
44
  } else {
45
    result.push(values);
167,965✔
46
  }
47

48
  return result;
279,839✔
49
};
50

51
export const mapKeys = <T>(set: Map<T, any>): T[] => {
1✔
52
  const result: T[] = [];
588✔
53
  // eslint-disable-next-line unicorn/no-array-for-each
54
  set.forEach((_, value: T) => result.push(value));
6,177✔
55

56
  return result;
588✔
57
};
58

59
export const mapValues = <T>(set: { forEach(a1: (value: T) => void): void }, destination?: Set<T>): T[] => {
1✔
60
  const result: T[] = [];
27,997✔
61
  if (destination) {
27,997✔
62
    // eslint-disable-next-line unicorn/no-array-for-each
63
    set.forEach((value: T) => {
6,811✔
64
      destination.add(value);
4,941✔
65
    });
66
  } else {
67
    // eslint-disable-next-line unicorn/no-array-for-each
68
    set.forEach((value: T) => {
21,186✔
69
      result.push(value);
59,757✔
70
    });
71
  }
72

73
  return result;
27,997✔
74
};
75

76
export const mapEntries = <K, T>(set: Map<K, T>, destination?: Map<K, T>): Array<[K, T]> => {
1✔
77
  const result: Array<[K, T]> = [];
15,305✔
78

79
  if (destination) {
15,305✔
80
    // eslint-disable-next-line unicorn/no-array-for-each
81
    set.forEach((value: T, key: K) => destination.set(key, value));
4,220✔
82
  } else {
83
    // eslint-disable-next-line unicorn/no-array-for-each
84
    set.forEach((value: T, key: K) => result.push([key, value]));
189,513✔
85
  }
86

87
  return result;
15,305✔
88
};
89

90
const extractDependencyArray = (deps: any[], set: Set<any>): void => {
1✔
91
  for (const flag of deps) {
118✔
92
    const name = flag && typeof flag === 'object' ? flag.ngMetadataName : undefined;
118✔
93
    if (name === 'Optional' || name === 'SkipSelf' || name === 'Self') {
118✔
94
      continue;
80✔
95
    }
96
    set.add(flag);
38✔
97
  }
98
};
99

100
// Accepts an array of dependencies from providers, skips injections flags,
101
// and adds the providers to the set.
102
export const extractDependency = (deps: any[], set?: Set<any>): void => {
1✔
103
  if (!set) {
8,139✔
104
    return;
261✔
105
  }
106
  for (const dep of deps) {
11,080✔
107
    if (!Array.isArray(dep)) {
11,080✔
108
      set.add(dep);
11,042✔
109
      continue;
11,042✔
110
    }
111
    extractDependencyArray(dep, set);
38✔
112
  }
113
};
114

115
export const extendClassicClass = <I>(base: AnyType<I>): Type<I> => {
1✔
116
  let child: any;
117
  const index = ngMocksUniverse.index();
3,361✔
118

119
  const glb = funcGetGlobal();
3,361✔
120
  glb.ngMocksParent = base;
3,361✔
121

122
  // First we try to eval es2015 style and if it fails to use es5 transpilation in the catch block.
123
  // The next step is to respect constructor parameters as the parent class via jitReflector.
124
  // istanbul ignore next
125
  try {
126
    eval(`
127
      var glb = typeof window === 'undefined' ? global : window;
128
      class MockMiddleware${index} extends glb.ngMocksParent {};
129
      glb.ngMocksResult = MockMiddleware${index};
130
    `);
131
    child = glb.ngMocksResult;
132
  } catch {
133
    class MockMiddleware extends glb.ngMocksParent {}
134
    child = MockMiddleware;
135
  } finally {
136
    glb.ngMocksResult = undefined;
137
    glb.ngMocksParent = undefined;
138
  }
139

140
  // A16: adding unique property.
141
  coreDefineProperty(child.prototype, `__ngMocks_index_${index}`, undefined, false);
3,361✔
142

143
  return child;
3,361✔
144
};
145

146
export const extendClass = <I>(base: AnyType<I>): Type<I> => {
1✔
147
  const child: Type<I> = extendClassicClass(base);
3,361✔
148
  coreDefineProperty(child, 'name', `MockMiddleware${funcGetName(base)}`, true);
3,361✔
149

150
  const parameters = coreReflectParametersResolve(base);
3,361✔
151
  if (parameters.length > 0) {
3,361✔
152
    coreDefineProperty(child, 'parameters', [...parameters]);
3,203✔
153
  }
154

155
  return child;
3,361✔
156
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc