0

I am looking for a way to share the TestBed between different files in Angular spec files. I understand, that I can always write a simple util class to do so, but am not sure if that is the correct way.

For example, if I have:

parent.module.ts  ---> imports other modules and has providers
- one.component.ts
- two.component.ts
- three.component.ts

Now when writing the spec files for the above components, I need to set the TestBed configuration in each of them. Which is almost the contents of the parent.module.ts for its declarations, imports, providers etc.

This seems like a lot of repetition of code considering a parent module with lots of child components. Like I said above, I can always extract the TestBed.configureTestingModule into a separate file. But I am not sure if it is the correct way.

Also what should be the location of the file and I hope it will not be a part of my release bundle.

georgeawg
  • 48,608
  • 13
  • 72
  • 95
ashish.gd
  • 1,713
  • 1
  • 14
  • 25
  • Please, provide actual specs to show what is exact setup that you want to share. Usually you want to keep them WET with their own TestBed config, because 'Which is **almost** the contents' makes the difference. – Estus Flask Aug 23 '17 at 21:45
  • @estus completely agree with your point of each spec usually having its own TestBed config. My question is in cases when I need the exact same TestBed config (module) in multiple specs. I will soon try to put a more elaborate example. But I hope my explanation works for now. – ashish.gd Aug 23 '17 at 23:49
  • Then you can have a common function in separate file that is used like `beforeEach(commonTestBedSetup)`. – Estus Flask Aug 23 '17 at 23:56
  • Thank you for the quick update @estus. Can I assume that this file will not make it into my release bundle since it is only referenced through spec. My question arises since in JS we keep the spec alongside the component whereas in Java we have a separate `/src/main/test/` location. – ashish.gd Aug 24 '17 at 00:06
  • 1
    Yes, it shouldn't be included. And yes, file organization is the main concern here. It's not a problem when tests are stored in separate folder but is a problem when .spec.ts and .e2e.ts files are located near respective source files. You can have `testing` folders for anything test-related in each module subfolder wherever needed and keep common files (utils, fixtures) there. Take a look at how Angular source code is organized for example. – Estus Flask Aug 24 '17 at 00:11

0 Answers0