or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

configuration.mdindex.mdmock-builder.mdmock-creation.mdmock-instance.mdmock-render.mdtesting-utilities.md

mock-creation.mddocs/

0

# Mock Creation and Management

1

2

Individual mock creation functions for all Angular constructs. Provides precise control over mock behavior and supports both automatic and manual mock configuration.

3

4

## Capabilities

5

6

### Component Mocking

7

8

Creates mock implementations of Angular components with configurable behavior.

9

10

```typescript { .api }

11

/**

12

* Creates a mock component with default behavior

13

* @param component - Component class to mock

14

* @returns Mock component class

15

*/

16

function MockComponent<T>(component: AnyType<T>): AnyType<T>;

17

18

/**

19

* Creates multiple mock components

20

* @param components - Array of component classes to mock

21

* @returns Array of mock component classes

22

*/

23

function MockComponents(...components: Array<AnyType<any>>): Array<AnyType<any>>;

24

25

/**

26

* Type for mocked components

27

*/

28

type MockedComponent<T> = T;

29

```

30

31

**Usage Examples:**

32

33

```typescript

34

import { MockComponent, MockComponents } from "ng-mocks";

35

36

// Mock a single component

37

const MockHeaderComponent = MockComponent(HeaderComponent);

38

39

// Mock multiple components

40

const [MockHeader, MockFooter, MockSidebar] = MockComponents(

41

HeaderComponent,

42

FooterComponent,

43

SidebarComponent

44

);

45

46

// Use in TestBed

47

TestBed.configureTestingModule({

48

declarations: [

49

TestComponent,

50

MockHeaderComponent,

51

MockFooter

52

]

53

});

54

```

55

56

### Service Mocking

57

58

Creates mock implementations of services with optional property overrides.

59

60

```typescript { .api }

61

/**

62

* Creates a mock service instance with optional overrides

63

* @param service - Service class to mock

64

* @param overrides - Partial implementation to override default behavior

65

* @returns Mock service instance

66

*/

67

function MockService<T>(service: AnyType<T>, overrides?: Partial<T>): T;

68

```

69

70

**Usage Examples:**

71

72

```typescript

73

import { MockService } from "ng-mocks";

74

75

// Mock service with default behavior

76

const mockAuthService = MockService(AuthService);

77

78

// Mock service with custom behavior

79

const mockHttpService = MockService(HttpService, {

80

get: jasmine.createSpy('get').and.returnValue(of({ data: 'test' })),

81

post: jasmine.createSpy('post').and.returnValue(of({ success: true })),

82

isAuthenticated: true

83

});

84

85

// Use in providers

86

TestBed.configureTestingModule({

87

providers: [

88

{ provide: AuthService, useValue: mockAuthService },

89

{ provide: HttpService, useValue: mockHttpService }

90

]

91

});

92

```

93

94

### Module Mocking

95

96

Creates mock implementations of Angular modules.

97

98

```typescript { .api }

99

/**

100

* Creates a mock module

101

* @param module - Module class to mock

102

* @returns Mock module class

103

*/

104

function MockModule<T>(module: AnyType<T>): AnyType<T>;

105

106

/**

107

* Type for mocked modules

108

*/

109

type MockedModule<T> = T;

110

```

111

112

**Usage Examples:**

113

114

```typescript

115

import { MockModule } from "ng-mocks";

116

117

// Mock a module

118

const MockSharedModule = MockModule(SharedModule);

119

const MockFeatureModule = MockModule(FeatureModule);

120

121

// Use in imports

122

TestBed.configureTestingModule({

123

imports: [

124

CommonModule,

125

MockSharedModule,

126

MockFeatureModule

127

]

128

});

129

```

130

131

### Directive Mocking

132

133

Creates mock implementations of Angular directives.

134

135

```typescript { .api }

136

/**

137

* Creates a mock directive

138

* @param directive - Directive class to mock

139

* @returns Mock directive class

140

*/

141

function MockDirective<T>(directive: AnyType<T>): AnyType<T>;

142

143

/**

144

* Creates multiple mock directives

145

* @param directives - Array of directive classes to mock

146

* @returns Array of mock directive classes

147

*/

148

function MockDirectives(...directives: Array<AnyType<any>>): Array<AnyType<any>>;

149

150

/**

151

* Type for mocked directives

152

*/

153

type MockedDirective<T> = T;

154

```

155

156

**Usage Examples:**

157

158

```typescript

159

import { MockDirective, MockDirectives } from "ng-mocks";

160

161

// Mock a single directive

162

const MockHighlightDirective = MockDirective(HighlightDirective);

163

164

// Mock multiple directives

165

const mockDirectives = MockDirectives(

166

HighlightDirective,

167

TooltipDirective,

168

DragDropDirective

169

);

170

171

// Use in TestBed

172

TestBed.configureTestingModule({

173

declarations: [

174

TestComponent,

175

MockHighlightDirective,

176

...mockDirectives

177

]

178

});

179

```

180

181

### Pipe Mocking

182

183

Creates mock implementations of Angular pipes with optional transform functions.

184

185

```typescript { .api }

186

/**

187

* Creates a mock pipe with default behavior

188

* @param pipe - Pipe class to mock

189

* @param transform - Optional custom transform implementation

190

* @returns Mock pipe class

191

*/

192

function MockPipe<T>(pipe: AnyType<T>, transform?: any): AnyType<T>;

193

194

/**

195

* Creates multiple mock pipes

196

* @param pipes - Array of pipe classes to mock

197

* @returns Array of mock pipe classes

198

*/

199

function MockPipes(...pipes: Array<AnyType<any>>): Array<AnyType<any>>;

200

201

/**

202

* Type for mocked pipes

203

*/

204

type MockedPipe<T> = T;

205

```

206

207

**Usage Examples:**

208

209

```typescript

210

import { MockPipe, MockPipes } from "ng-mocks";

211

212

// Mock pipe with default behavior (returns input unchanged)

213

const MockCurrencyPipe = MockPipe(CurrencyPipe);

214

215

// Mock pipe with custom transform

216

const MockDatePipe = MockPipe(DatePipe, (value: Date) =>

217

value ? value.toISOString() : ''

218

);

219

220

// Mock multiple pipes

221

const mockPipes = MockPipes(

222

CurrencyPipe,

223

DatePipe,

224

UpperCasePipe

225

);

226

227

// Use in TestBed

228

TestBed.configureTestingModule({

229

declarations: [

230

TestComponent,

231

MockDatePipe,

232

...mockPipes

233

]

234

});

235

```

236

237

### Generic Declaration Mocking

238

239

Creates mocks for any type of Angular declaration.

240

241

```typescript { .api }

242

/**

243

* Creates a mock of any Angular declaration

244

* @param declaration - Declaration to mock (component, directive, pipe, etc.)

245

* @returns Mock declaration

246

*/

247

function MockDeclaration<T>(declaration: AnyType<T>): AnyType<T>;

248

249

/**

250

* Creates multiple mock declarations

251

* @param declarations - Array of declarations to mock

252

* @returns Array of mock declarations

253

*/

254

function MockDeclarations(...declarations: Array<AnyType<any>>): Array<AnyType<any>>;

255

```

256

257

**Usage Examples:**

258

259

```typescript

260

import { MockDeclaration, MockDeclarations } from "ng-mocks";

261

262

// Mock any declaration type

263

const MockSomething = MockDeclaration(SomeDeclaration);

264

265

// Mock multiple mixed declarations

266

const mocks = MockDeclarations(

267

MyComponent,

268

MyDirective,

269

MyPipe,

270

MyService

271

);

272

```

273

274

### Provider Mocking

275

276

Creates mock providers for dependency injection.

277

278

```typescript { .api }

279

/**

280

* Creates mock providers with various overload signatures

281

*/

282

function MockProvider(provider: any): Provider;

283

function MockProvider(token: any, useValue: any): Provider;

284

function MockProvider(token: any, useFactory: Function, deps?: any[]): Provider;

285

function MockProvider(token: any, useClass: any): Provider;

286

function MockProvider(token: any, useExisting: any): Provider;

287

288

/**

289

* Creates multiple mock providers

290

* @param providers - Array of providers to mock

291

* @returns Array of mock providers

292

*/

293

function MockProviders(...providers: any[]): Provider[];

294

```

295

296

**Usage Examples:**

297

298

```typescript

299

import { MockProvider, MockProviders } from "ng-mocks";

300

301

// Mock a service provider

302

const mockAuthProvider = MockProvider(AuthService);

303

304

// Mock with specific value

305

const mockConfigProvider = MockProvider(APP_CONFIG, {

306

apiUrl: 'http://test-api.com',

307

timeout: 5000

308

});

309

310

// Mock with factory

311

const mockFactoryProvider = MockProvider(

312

DatabaseService,

313

() => ({ query: jasmine.createSpy('query') }),

314

[HttpClient]

315

);

316

317

// Mock multiple providers

318

const mockProviders = MockProviders(

319

AuthService,

320

UserService,

321

APP_CONFIG

322

);

323

324

// Use in TestBed

325

TestBed.configureTestingModule({

326

providers: [

327

mockAuthProvider,

328

mockConfigProvider,

329

...mockProviders

330

]

331

});

332

```

333

334

### Mock Behavior Control

335

336

Control default behavior and configuration of mocks.

337

338

**Auto-stubbing with spies:**

339

340

```typescript

341

import { ngMocks } from "ng-mocks";

342

343

// Configure automatic spy creation

344

ngMocks.autoSpy('jasmine'); // For Jasmine

345

ngMocks.autoSpy('jest'); // For Jest

346

347

// Custom spy factory

348

ngMocks.autoSpy((name: string) => {

349

return jasmine.createSpy(name);

350

});

351

```

352

353

**Default mock configurations:**

354

355

```typescript

356

// Set default behavior for all mocks of a service

357

ngMocks.defaultMock(AuthService, (instance) => ({

358

isAuthenticated: true,

359

user: { id: 1, name: 'Test User' }

360

}));

361

362

// Set configuration flags for declarations

363

ngMocks.defaultConfig(MyComponent, { shallow: true });

364

```

365

366

**Global mock settings:**

367

368

```typescript

369

// Always keep certain declarations

370

ngMocks.globalKeep(CommonModule);

371

ngMocks.globalKeep(RouterModule);

372

373

// Always mock certain declarations

374

ngMocks.globalMock(HttpClientModule);

375

376

// Always exclude certain declarations

377

ngMocks.globalExclude(ProblematicModule);

378

```