In my Angular project, I have a class set up as follows:
import { USERS } from "./data/users"; // imports an Array of Objects
export class User {
constructor(name: string) {
const user = USERS.find(e => e.name === name);
}
...
}
Everything is working smoothly when I compile and build the website. However, when attempting to run unit tests using Jasmine, an error occurs stating
TypeError: Cannot read property 'find' of undefined
because the test cannot locate the variable USERS
. My spec file looks like this:
import { User } from './users.module';
import { USERS } from "./data/users";
describe('UserModule', () => {
let userModule: UserModule;
beforeEach(() => {
userModule = new UserModule();
});
it('should create a user', () => {
const user = new User("Testuser");
expect(user).toBeTruthy();
});
});
I'm puzzled as to why the User class can't find the USERS variable during testing, while it's able to do so in production. What modifications should I make for the class to locate that variable during testing?