问题
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
.
回答1:
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:
- https://loopback.io/doc/en/lb4/Deploying-to-IBM-Cloud.html#updating-application
- https://itnext.io/loopback-4-database-configuration-8f085399268
来源:https://stackoverflow.com/questions/53364273/loopback-4-test-configurations-are-not-picked-up