Skip to content

Commit 96fcf13

Browse files
committed
rewrite to TS
1 parent 94fbe45 commit 96fcf13

File tree

13 files changed

+112
-112
lines changed

13 files changed

+112
-112
lines changed

template/eslint.config.js

Lines changed: 0 additions & 42 deletions
This file was deleted.

template/eslint.config.mjs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// @ts-check
2+
3+
import eslint from "@eslint/js";
4+
import tseslint from "typescript-eslint";
5+
import eslintPluginPrettierRecommended from "eslint-plugin-prettier/recommended";
6+
import globals from "globals";
7+
8+
export default tseslint.config(
9+
eslint.configs.recommended,
10+
tseslint.configs.recommended,
11+
eslintPluginPrettierRecommended,
12+
{
13+
languageOptions: {
14+
globals: {
15+
...globals.node
16+
}
17+
},
18+
rules: {
19+
"@/no-var": ["error"],
20+
"@/no-console": ["warn"],
21+
"@typescript-eslint/no-unused-vars": ["warn"],
22+
"@/no-trailing-spaces": ["error"],
23+
"@/no-process-exit": ["off"],
24+
"@/object-curly-spacing": ["warn", "always"]
25+
}
26+
},
27+
{
28+
files: ["test/**/*.js"],
29+
rules: {
30+
"@/no-unused-vars": ["off"]
31+
}
32+
}
33+
);

template/jest.config.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

template/moleculer.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { BrokerOptions, MetricRegistry, ServiceBroker } from "moleculer";
22
import { Errors } from "moleculer";
33

44
{{#needChannels}}
5-
import { Middleware as ChannelMiddleware } from "@moleculer/channels");
5+
import { Middleware as ChannelMiddleware } from "@moleculer/channels";
66
{{#tracing}}import { Tracing as ChannelTracing } from "@moleculer/channels";{{/tracing}}
77
{{/needChannels}}
88

template/package.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
"dev": "tsx ./node_modules/moleculer/bin/moleculer-runner.js --config moleculer.config.ts --hot --repl services/**/*.service.ts",
88
"start": "moleculer-runner --config dist/moleculer.config.js",
99
"cli": "moleculer connect {{transporter}}",
10-
"ci": "jest --watch",
11-
"test": "jest --coverage"{{#lint}},
10+
"ci": "vitest --watch",
11+
"test": "vitest --run --coverage"{{#lint}},
1212
"lint": "eslint",
1313
"lint:fix": "eslint --fix"{{/lint}}{{#docker}},
1414
"dc:up": "docker compose up --build -d",
@@ -21,10 +21,13 @@
2121
],
2222
"author": "",
2323
"devDependencies": {
24+
"@types/node": "^22.15.29",
2425
{{#lint}}
26+
"@eslint/js": "^9.28.0",
2527
"eslint": "^9.28.0",
2628
"eslint-config-prettier": "^10.1.5",
2729
"eslint-plugin-prettier": "^5.4.1",
30+
"typescript-eslint": "^8.33.0",
2831
"prettier": "^3.5.3",
2932
{{/lint}}
3033
"jest": "^29.7.0",
@@ -38,7 +41,10 @@
3841
{{#apiGQL}}
3942
"graphql-request": "^6.1.0",
4043
{{/apiGQL}}
41-
"moleculer-repl": "^0.7.4"
44+
"moleculer-repl": "^0.7.4",
45+
"tsx": "^4.19.4",
46+
"typescript": "^5.8.3",
47+
"vitest": "^3.1.4"
4248
},
4349
"dependencies": {
4450
{{#apiGW}}

template/test/integration/api.spec.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
import { afterAll, beforeAll, describe, expect, test } from "@jest/globals";
1+
import { afterAll, beforeAll, describe, it, expect, vi } from "vitest";
22

33

44
process.env.PORT = 0; // Use random ports during tests
55

6-
const HTTPrequest = require("supertest");
6+
import HTTPrequest from "supertest";
77
{{#apiIO}}
8-
const io = require("socket.io-client");
8+
import io from "socket.io-client";
99
{{/apiIO}}
1010
{{#apiGQL}}
11-
const { request, gql } = require("graphql-request");
11+
import { request, gql } from "graphql-request";
1212
{{/apiGQL}}
1313

14-
const { ServiceBroker } = require("moleculer");
14+
import { Context, ServiceBroker } from "moleculer";
1515
// Load service schemas
16-
const APISchema = require("../../services/api.service");
17-
const GreeterSchema = require("../../services/greeter.service");
16+
import APISchema from "../../../services/api.service";
17+
import GreeterSchema from "../../../services/greeter.service";
1818
{{#dbService}}
19-
const ProductsSchema = require("../../services/products.service");
19+
import ProductsSchema from "../../../services/products.service";
2020
{{/dbService}}
2121

2222
describe("Test HTTP API gateway", () => {
2323
let broker = new ServiceBroker({ logger: false });
24-
broker.sendToChannel = jest.fn();
24+
broker.sendToChannel = vi.fn();
2525

2626
let greeterService = broker.createService(GreeterSchema);
2727
let apiService = broker.createService(APISchema);
@@ -215,7 +215,7 @@ describe("Test HTTP API gateway", () => {
215215
{{#apiIO}}
216216
describe("Test Socket.IO API gateway", () => {
217217
let broker = new ServiceBroker({ logger: false });
218-
broker.sendToChannel = jest.fn();
218+
broker.sendToChannel = vi.fn();
219219

220220
let greeterService = broker.createService(GreeterSchema);
221221
let apiService = broker.createService(APISchema);
@@ -406,7 +406,7 @@ describe("Test Socket.IO API gateway", () => {
406406
{{#apiGQL}}
407407
describe("Test GraphQL API gateway", () => {
408408
let broker = new ServiceBroker({ logger: false });
409-
broker.sendToChannel = jest.fn();
409+
broker.sendToChannel = vi.fn();
410410

411411
let greeterService = broker.createService(GreeterSchema);
412412
let apiService = broker.createService(APISchema);

template/test/integration/products.service.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { afterAll, beforeAll, describe, expect, test } from "@jest/globals";
1+
import { afterAll, beforeAll, describe, it, expect, vi } from "vitest";
22
import { ServiceBroker } from "moleculer";
33
import type { ServiceSchema } from "moleculer";
44
import type { ProductEntity } from "../../services/products.service";
@@ -10,7 +10,7 @@ describe("Test 'products' service", () => {
1010
const service = broker.createService(TestService);
1111
service.seedDB = null; // Disable seeding
1212

13-
broker.sendToChannel = jest.fn();
13+
broker.sendToChannel = vi.fn();
1414

1515
beforeAll(() => broker.start());
1616
afterAll(() => broker.stop());

template/test/unit/mixins/db.mixin.spec.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { afterAll, beforeAll, describe, expect, test } from "@jest/globals";
1+
import { afterAll, beforeAll, describe, it, expect, vi } from "vitest";
22

3-
const { ServiceBroker } = require("moleculer");
4-
const DbMixin = require("../../../mixins/db.mixin");
3+
import { Context, ServiceBroker } from "moleculer";
4+
import type { Service, ServiceAsyncLifecycleHandler, ServiceEventHandler } from "moleculer";
5+
import DbMixin from "../../../mixins/db.mixin";
56

67
describe("Test DB mixin", () => {
78
describe("Test schema generator", () => {
@@ -19,7 +20,7 @@ describe("Test DB mixin", () => {
1920
});
2021

2122
it("check cache event handler", async () => {
22-
jest.spyOn(broker.cacher, "clean");
23+
vi.spyOn(broker.cacher, "clean");
2324

2425
const schema = DbMixin("my-collection");
2526

@@ -37,12 +38,12 @@ describe("Test DB mixin", () => {
3738
const schema = DbMixin("my-collection");
3839

3940
const adapterMock = {
40-
count: jest.fn(async () => 10)
41+
count: vi.fn(async () => 10)
4142
};
42-
let getAdapterMock = jest.fn(() => {
43+
let getAdapterMock = vi.fn(() => {
4344
return Promise.resolve(adapterMock);
4445
});
45-
const seedDBFn = jest.fn();
46+
const seedDBFn = vi.fn();
4647

4748
await schema.started.call({
4849
broker,
@@ -61,12 +62,12 @@ describe("Test DB mixin", () => {
6162
const schema = DbMixin("my-collection");
6263

6364
const adapterMock = {
64-
count: jest.fn(async () => 0)
65+
count: vi.fn(async () => 0)
6566
};
66-
let getAdapterMock = jest.fn(() => {
67+
let getAdapterMock = vi.fn(() => {
6768
return Promise.resolve(adapterMock);
6869
});
69-
const seedDBFn = jest.fn();
70+
const seedDBFn = vi.fn();
7071

7172
await schema.started.call({
7273
broker,
@@ -87,7 +88,7 @@ describe("Test DB mixin", () => {
8788
const schema = DbMixin("my-collection");
8889

8990
const ctx = {
90-
broadcast: jest.fn()
91+
broadcast: vi.fn()
9192
};
9293

9394
await schema.methods.entityChanged(null, null, null, ctx);

template/test/unit/services/greeter.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { afterAll, beforeAll, describe, expect, test } from "@jest/globals";
1+
import { afterAll, beforeAll, describe, it, expect, vi } from "vitest";
22

3-
const { ServiceBroker } = require("moleculer");
4-
const { ValidationError } = require("moleculer").Errors;
5-
const TestService = require("../../../services/greeter.service");
3+
import { Errors, ServiceBroker } from "moleculer";
4+
import type { ServiceSchema } from "moleculer";
5+
import TestService from "../../../services/greeter.service";
66

77
describe("Test 'greeter' service", () => {
88
let broker = new ServiceBroker({ logger: false });
@@ -29,7 +29,7 @@ describe("Test 'greeter' service", () => {
2929
try {
3030
await broker.call("greeter.welcome");
3131
} catch (err) {
32-
expect(err).toBeInstanceOf(ValidationError);
32+
expect(err).toBeInstanceOf(Errors.ValidationError);
3333
}
3434
});
3535
});

template/test/unit/services/inventory.spec.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { afterAll, beforeAll, describe, expect, test } from "@jest/globals";
1+
import { afterAll, beforeAll, describe, it, expect, vi } from "vitest";
22

3-
const { ServiceBroker, Context } = require("moleculer");
4-
const ChannelMiddleware = require("@moleculer/channels").Middleware;
5-
const TestService = require("../../../services/inventory.service");
3+
import { Context, Errors, ServiceBroker } from "moleculer";
4+
import type { ServiceSchema } from "moleculer";
5+
import TestService from "../../../services/inventory.service";
6+
import { Middleware as ChannelMiddleware } from "@moleculer/channels";
67

78
describe("Test 'inventory' service", () => {
89
const broker = new ServiceBroker({
@@ -16,20 +17,20 @@ describe("Test 'inventory' service", () => {
1617
]
1718
});
1819
// Mock adapter methods
19-
broker.channelAdapter.init = jest.fn();
20-
broker.channelAdapter.connect = jest.fn();
21-
broker.channelAdapter.disconnect = jest.fn();
22-
broker.channelAdapter.subscribe = jest.fn();
23-
broker.channelAdapter.unsubscribe = jest.fn();
24-
broker.channelAdapter.publish = jest.fn();
20+
broker.channelAdapter.init = vi.fn();
21+
broker.channelAdapter.connect = vi.fn();
22+
broker.channelAdapter.disconnect = vi.fn();
23+
broker.channelAdapter.subscribe = vi.fn();
24+
broker.channelAdapter.unsubscribe = vi.fn();
25+
broker.channelAdapter.publish = vi.fn();
2526

26-
broker.emit = jest.fn();
27+
broker.emit = vi.fn();
2728

2829
const service = broker.createService(TestService);
2930

3031
// Store the reference to the original orderProduct method
3132
const ORIGINAL_ORDER_PRODUCT = service.orderProduct;
32-
service.orderProduct = jest.fn();
33+
service.orderProduct = vi.fn();
3334

3435
beforeAll(() => broker.start());
3536
afterAll(() => broker.stop());
@@ -50,7 +51,7 @@ describe("Test 'inventory' service", () => {
5051
});
5152

5253
it("should send msg to 'inventory.reserve' channel", async () => {
53-
broker.sendToChannel = jest.fn();
54+
broker.sendToChannel = vi.fn();
5455

5556
await broker.call("inventory.reserve", {
5657
productId: "123",

0 commit comments

Comments
 (0)