Unit tests seems to call unrealted code
When running the npm run test I'm getting the following output from my tests. Reading through the error log it seems to be bringing up unrelated code that isn't even called or connected by a relation on the model or repo. I have another test working perfectly fine, but whenever I go to add a second test this happens. I'm guessing its something messed up with the configuration of the test. I can provide more information as needed.
node version = 12.13.0 @loopback/cli version: 1.25.1
error
\Work\monotto\monotto-ts\node_modules\@loopback\repository\src\decorators\repository.decorator.ts:145
typeof modelOrRepo !== 'string' && !modelOrRepo.prototype.getId
^
TypeError: Cannot read property 'prototype' of undefined
at Object.repository (D:\Work\monotto\monotto-ts\node_modules\@loopback\repository\src\decorators\repository.decorator.ts:145:53)
at Object.<anonymous> (D:\Work\monotto\monotto-ts\src\services\finastra.service.ts:25:6)
at Module._compile (internal/modules/cjs/loader.js:956:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
at Module.load (internal/modules/cjs/loader.js:812:32)
at Function.Module._load (internal/modules/cjs/loader.js:724:14)
at Module.require (internal/modules/cjs/loader.js:849:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object.<anonymous> (D:\Work\monotto\monotto-ts\src\services\index.ts:6:1)
at Module._compile (internal/modules/cjs/loader.js:956:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
at Module.load (internal/modules/cjs/loader.js:812:32)
at Function.Module._load (internal/modules/cjs/loader.js:724:14)
at Module.require (internal/modules/cjs/loader.js:849:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object.<anonymous> (D:\Work\monotto\monotto-ts\src\datasources\db.datasource.ts:5:1)
at Module._compile (internal/modules/cjs/loader.js:956:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
at Module.load (internal/modules/cjs/loader.js:812:32)
at Function.Module._load (internal/modules/cjs/loader.js:724:14)
at Module.require (internal/modules/cjs/loader.js:849:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object.<anonymous> (D:\Work\monotto\monotto-ts\src\datasources\index.ts:1:1)
at Module._compile (internal/modules/cjs/loader.js:956:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
at Module.load (internal/modules/cjs/loader.js:812:32)
at Function.Module._load (internal/modules/cjs/loader.js:724:14)
at Module.require (internal/modules/cjs/loader.js:849:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object.<anonymous> (D:\Work\monotto\monotto-ts\src\repositories\ads.repository.ts:3:1)
at Module._compile (internal/modules/cjs/loader.js:956:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
at Module.load (internal/modules/cjs/loader.js:812:32)
at Function.Module._load (internal/modules/cjs/loader.js:724:14)
at Module.require (internal/modules/cjs/loader.js:849:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object.<anonymous> (D:\Work\monotto\monotto-ts\src\repositories\index.ts:1:1)
at Module._compile (internal/modules/cjs/loader.js:956:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
at Module.load (internal/modules/cjs/loader.js:812:32)
at Function.Module._load (internal/modules/cjs/loader.js:724:14)
at Module.require (internal/modules/cjs/loader.js:849:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object.<anonymous> (D:\Work\monotto\monotto-ts\src\entities\vendor.entity.ts:4:1)
at Module._compile (internal/modules/cjs/loader.js:956:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
at Module.load (internal/modules/cjs/loader.js:812:32)
at Function.Module._load (internal/modules/cjs/loader.js:724:14)
at Module.require (internal/modules/cjs/loader.js:849:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object.<anonymous> (D:\Work\monotto\monotto-ts\src\entities\index.ts:1:1)
at Module._compile (internal/modules/cjs/loader.js:956:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
at Module.load (internal/modules/cjs/loader.js:812:32)
at Function.Module._load (internal/modules/cjs/loader.js:724:14)
at Module.require (internal/modules/cjs/loader.js:849:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object.<anonymous> (D:\Work\monotto\monotto-ts\src\__tests__\unit\ads.entity.test.ts:7:1)
at Module._compile (internal/modules/cjs/loader.js:956:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
at Module.load (internal/modules/cjs/loader.js:812:32)
at Function.Module._load (internal/modules/cjs/loader.js:724:14)
at Module.require (internal/modules/cjs/loader.js:849:19)
at require (internal/modules/cjs/helpers.js:74:18)
at D:\Work\monotto\monotto-ts\node_modules\mocha\lib\mocha.js:334:36
at Array.forEach (<anonymous>)
at Mocha.loadFiles (D:\Work\monotto\monotto-ts\node_modules\mocha\lib\mocha.js:331:14)
at Mocha.run (D:\Work\monotto\monotto-ts\node_modules\mocha\lib\mocha.js:809:10)
at Object.exports.singleRun (D:\Work\monotto\monotto-ts\node_modules\mocha\lib\cli\run-helpers.js:108:16)
at exports.runMocha (D:\Work\monotto\monotto-ts\node_modules\mocha\lib\cli\run-helpers.js:142:13)
at Object.exports.handler (D:\Work\monotto\monotto-ts\node_modules\mocha\lib\cli\run.js:292:3)
at Object.runCommand (D:\Work\monotto\monotto-ts\node_modules\yargs\lib\command.js:242:26)
at Object.parseArgs [as _parseArgs] (D:\Work\monotto\monotto-ts\node_modules\yargs\yargs.js:1087:28)
at Object.parse (D:\Work\monotto\monotto-ts\node_modules\yargs\yargs.js:566:25)
at Object.exports.main (D:\Work\monotto\monotto-ts\node_modules\mocha\lib\cli\cli.js:68:6)
at Object.<anonymous> (D:\Work\monotto\monotto-ts\node_modules\mocha\bin\mocha:164:29)
at Module._compile (internal/modules/cjs/loader.js:956:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
at Module.load (internal/modules/cjs/loader.js:812:32)
at Function.Module._load (internal/modules/cjs/loader.js:724:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10)
at internal/main/run_main_module.js:17:11
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] test: `lb-mocha --allow-console-logs "dist/__tests__"`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\deleo\AppData\Roaming\npm-cache\_logs\2019-11-19T18_12_55_137Z-debug.log
ads-test
import {
createStubInstance,
expect,
StubbedInstanceWithSinonAccessor,
} from '@loopback/testlab';
import {Ads} from '../../models';
import {AdsEntity} from '../../entities';
import {AdsRepository} from '../../repositories';
describe('Ads test (unit)', () => {
const testAd = {
header: 'testAd',
body: 'test',
link: 'https://www.google.com',
name: 'test'
} as Ads;
let adsRepo: StubbedInstanceWithSinonAccessor<AdsRepository>;
function givenStubbedRepository() {
adsRepo = createStubInstance(AdsRepository);
}
function setupStubbedServices() {
}
beforeEach(givenStubbedRepository);
beforeEach(setupStubbedServices);
before('setupApplication', async () => {});
after(async () => {});
describe('createAd()', () => {
it('creates an ad', async () => {
adsRepo.stubs.create.resolves(testAd);
const adsEntity = new AdsEntity(adsRepo)
const result = await adsEntity.createAd(testAd);
expect(result).to.containEql(testAd);
})
})
})
ads-entity
import {AdsRepository} from "../repositories";
import {repository, Where, Count, Filter} from "@loopback/repository";
import moment = require("moment");
import {Ads} from "../models";
import {getWhereSchemaFor, param, getFilterSchemaFor, requestBody} from "@loopback/rest";
export class AdsEntity {
constructor(@repository(AdsRepository) public adsRepository: AdsRepository) {}
async createAd(ad: Ads) {
ad.createdAt = moment().utc().format();
ad.updatedAt = moment().utc().format();
return this.adsRepository.create(ad);
}
async countAds(@param.query.object('where', getWhereSchemaFor(Ads)) where?: Where, ): Promise<Count> {
return this.adsRepository.count(where);
}
async findAds(
@param.query.object('filter', getFilterSchemaFor(Ads)) filter?: Filter<Ads>,
): Promise<Ads[]> {
return this.adsRepository.find(filter);
}
async updateAll(
@requestBody() ads: Ads,
@param.query.object('where', getWhereSchemaFor(Ads)) where?: Where,
): Promise<Count> {
return this.adsRepository.updateAll(ads, where);
}
async findById(@param.path.number('id') id: number): Promise<Ads> {
return this.adsRepository.findById(id);
}
async updateById(
@param.path.number('id') id: number,
@requestBody() ads: Ads,
): Promise<void> {
await this.adsRepository.updateById(id, ads);
}
async replaceById(
@param.path.number('id') id: number,
@requestBody() ads: Ads,
): Promise<void> {
await this.adsRepository.replaceById(id, ads);
}
async deleteById(@param.path.number('id') id: number): Promise<void> {
await this.adsRepository.deleteById(id);
}
}
ads repository
import {DefaultCrudRepository} from "@loopback/repository";
import {Ads} from "../models";
import {DbDataSource} from "../datasources";
import {inject} from "@loopback/core";
export class AdsRepository extends DefaultCrudRepository<
Ads,
typeof Ads.prototype.id
> {
constructor(@inject("datasources.db") dataSource: DbDataSource) {
super(Ads, dataSource);
}
}
ads model
import {Entity, model, property} from "@loopback/repository";
@model({
settings: {
idInjection: true,
postgresql: {
schema: "public",
table: "Ads",
},
},
})
export class Ads extends Entity {
@property({
type: Number,
required: false,
generated: true,
id: 1,
postgresql: {
columnName: "id",
dataType: "integer",
dataLength: null,
dataPrecision: null,
dataScale: 0,
nullable: "NO",
},
})
id: number;
@property({
type: String,
required: true,
postgresql: {
columnName: "header",
dataType: "text",
dataLength: null,
dataPrecision: null,
dataScale: null,
nullable: "NO",
},
})
header: string;
@property({
type: String,
required: true,
postgresql: {
columnName: "body",
dataType: "text",
dataLength: null,
dataPrecision: null,
dataScale: null,
nullable: "NO",
},
})
body: string;
@property({
type: String,
required: true,
postgresql: {
columnName: "link",
dataType: "text",
dataLength: null,
dataPrecision: null,
dataScale: null,
nullable: "NO",
},
})
link: string;
@property({
type: String,
required: false,
postgresql: {
columnName: "imageUrl",
dataType: "text",
dataLength: null,
dataPrecision: null,
dataScale: null,
nullable: "YES",
},
})
imageUrl?: string;
@property({
type: String,
required: false,
postgresql: {
columnName: "created_at",
dataType: "timestamp without time zone",
dataLength: null,
dataPrecision: 6,
dataScale: null,
nullable: "NO",
},
})
createdAt: string;
@property({
type: String,
required: false,
postgresql: {
columnName: "updated_at",
dataType: "timestamp without time zone",
dataLength: null,
dataPrecision: 6,
dataScale: null,
nullable: "NO",
},
})
updatedAt: string;
@property({
type: String,
required: true,
postgresql: {
columnName: "name",
dataType: "text",
dataLength: null,
dataPrecision: null,
dataScale: null,
nullable: "NO",
},
})
name: string;
// Define well-known properties here
// Indexer property to allow additional data
// eslint-disable-next-line
[prop: string]: any;
constructor(data?: Partial<Ads>) {
super(data);
}
}
@nabdelgadir , could you please take a look? Thanks!
@joshuaanthonydeleon It's best to have a github repo that contains your LB4 project to reproduce the problem.
@raymondfeng , @nabdelgadir , @dhmlau sorry for the slow response pretty hectic over here. I'll try and get something together. The problem is this is a whole application from our company, so I can't exactly give the entire repo open access or most of the code for that matter.
@joshuaanthonydeleon you can try to create a new application with just enough models/repositories/controllers classes to reproduce the problem
Closing due to inactivity.
I have the same problem only with UserRepository any solution ?
I can do that and works, but what is the problem with that UserRepository?
@repository.getter('UserRepository') public userRepository: UserRepository
Thanks!
Hi @pookdeveloper, would you be able to provide a sample repo so we can reproduce the problem? See here for instructions.
Thanks @nabdelgadir i think my error is that i have imported in the contructor a not repo, like: @repository.getter('User') public user: User i forget the XXrepository