[Fixed] Angular: where should you put dummy test data for multiple specs?

Issue

Given that angular spec files should reside in the same folder as the normal files, given that I want to reuse some dummy data between some different specs, where should I put it?

Example:

a
  a.service.ts (I want to have some dummy test data returned by this service)
  a.service.spec.ts (and use it in this test)
b
  b.component.spec.ts (and also use it in this test)
c
  c.component.spec.ts (and this one too)
...

I could have this dummy test data defined in each of those spec files, but that would result in a lot of duplication.

I am not sure if there is a good, proper way to define this kind of data.

Solution

I would just create a ts file and return it as key values with your JSON test data as the value.

export const mockJSON = {
   testkey1: "{\"name\": \"Company List\"}
}

Then you should be able to grab this as an import anywhere else in the code.

Leave a Reply

(*) Required, Your email will not be published