4

I followed Loopback4 datasources documentation and placed sample.datasource.json and sample.test.datasource.json files under src/datasources. Whenever I run npm run test my repository is injected with original datasource but not the test datasource.

My datasource configuration file sample.datasource.json is

{
  "name": "sample",
  "connector": "postgresql",
  "url": "postgres://postgres:user@localhost:5432/somedb",
  "host": "localhost",
  "port": 5432,
  "user": "postgres",
  "password": "****",
  "database": "somedb"
}

and my test configuration file sample.test.datasource.json is

{
  "name": "sample",
  "connector": "memory",
  "localStorage": "sample-test",
  "debug": true
}

Here is my repository file sample.repository.ts placed under src/repositories.

export class SampleRepository extends DefaultCrudRepository<SomeModel,string> {
  constructor(
    @inject('datasources.sample') dataSource: SomeDataSource,
  ) {
    super(SomeModel, dataSource);
  }
}

I exported NODE_ENV to "test" and tried to run npm run test.

Raj B6
  • 41
  • 3
  • What solution did you implement for configuring different environment, since loopback 4 doesn't yet include environment configurations? – Ashutosh Chamoli Dec 17 '18 at 09:42
  • Sorry for late reply. I am manually creating instance of juggler.DataSource and passing it as argument in repository constructor for Test environments. – Raj B6 Jan 22 '19 at 10:57

1 Answers1

5

Hello from the LoopBack team :)

LoopBack 4 does not support environment specific configuration yet. We are discussing this feature in the GitHub issue #1464, there is also a discussion about modifying datasource configuration specifically for tests - see issue #1396.

There are workaround possible, see the following resources for inspiration:

Miroslav Bajtoš
  • 10,667
  • 1
  • 41
  • 99