Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error while run unit test with moleculer-cli generated project #31

Open
JasLin opened this issue Oct 19, 2020 · 0 comments
Open

Error while run unit test with moleculer-cli generated project #31

JasLin opened this issue Oct 19, 2020 · 0 comments

Comments

@JasLin
Copy link

JasLin commented Oct 19, 2020

i am trying to make an es7 template project with moleculer-cli generated project .now it can run with npm run dev without error,
but it can't pass npm test , with errors:

   Test 'products' service  Test hooks  encountered a declaration exception

    ServiceSchemaError: Service name can't be empty! Maybe it is not a valid Service schema. Maybe is it not a service schema?

      4 | const DbMixin = require("../mixins/db.mixin");
      5 | 
    > 6 | @Service({
        | ^
      7 |       name: 'products',
      8 |       // constructOverride: false,
      9 |       // skipHandler: true,

      at Object.parseServiceSchema (../node_modules/moleculer/src/service.js:92:10)
      at new Service (../node_modules/moleculer/src/service.js:63:9)
      at new ProductsService (products.service.js:6:1)
      at new <anonymous> (../node_modules/moleculer-decorators/dist/index.js:128:9)
      at ServiceBroker.createService (../node_modules/moleculer/src/service-broker.js:801:14)
      at Suite.<anonymous> (../test/unit/services/products.spec.js:149:10)
      at Suite.<anonymous> (../test/unit/services/products.spec.js:146:2)
      at Object.<anonymous> (../test/unit/services/products.spec.js:9:1)

the error was begin from :

products.spec.js

...
describe("Test hooks", () => {
		const broker = new ServiceBroker({ logger: false });
		const createActionFn = jest.fn();
		broker.createService(TestService, { // <- error start from here, while it try to merge mock action to TestService Schema
			actions: {
				create: {
					handler: createActionFn
				}
			}
		});
...
}

and createService run into :

...
createService(schema, schemaMods) {
		let service;
		schema = this.normalizeSchemaConstructor(schema);
		if (Object.prototype.isPrototypeOf.call(this.ServiceFactory, schema)) {
			service = new schema(this, schemaMods); // <- run into here to new Decoratored Schema, but it with schemaMods, this is why it throws error , beacause `new service(broker, schemaMods)` will call parseServiceSchema, it will check name of schemaMods , but shemaMods don't have name properties. see bellow code . 
		} else {
			let s = schema;
			
			if (schemaMods)
				s = this.ServiceFactory.mergeSchemas(schema, schemaMods);
			console.log('s', s);
			service = new this.ServiceFactory(this, s);
		}
}
...

i notice that , moleculer-decorators return Service as :

return class extends parentService.constructor {
      constructor(broker, schema) { //  <- new schema(this, schemaMods) 
        super(broker, schema); // <- here, it will call `Moleculer.Service`, with params broker and schemaMods  
        this.parseServiceSchema(base);
      }
    };
  };

ref

class Service {

...
	constructor(broker, schema) {
		if (!isObject(broker))
			throw new ServiceSchemaError("Must set a ServiceBroker instance!");

		this.broker = broker;

		if (broker)
			this.Promise = broker.Promise;

		if (schema)
			this.parseServiceSchema(schema); // <- ran into here 
	}

	/**
	 * Parse Service schema & register as local service
	 *
	 * @param {Object} schema of Service
	 */
	parseServiceSchema(schema) {
		if (!isObject(schema))
			throw new ServiceSchemaError("The service schema can't be null. Maybe is it not a service schema?");

		this.originalSchema = _.cloneDeep(schema);

		if (schema.mixins) {
			schema = Service.applyMixins(schema);
		}

		if (isFunction(schema.merged)) {
			schema.merged.call(this, schema);
		} else if (Array.isArray(schema.merged)) {
			schema.merged.forEach(fn => fn.call(this, schema));
		}

		this.broker.callMiddlewareHookSync("serviceCreating", [this, schema]);

		if (!schema.name) { // <- error throws here
			
			console.error("Service name can't be empty! Maybe it is not a valid Service schema. Maybe is it not a service schema?", { schema });
			throw new ServiceSchemaError("Service name can't be empty! Maybe it is not a valid Service schema. Maybe is it not a service schema?", { schema });
		}
         ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant