From 21ece8a16be20d8512c42ba36b24a9241a927e7b Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 24 Mar 2023 17:02:57 +0100 Subject: [PATCH] test: setup to allow unit tests with stubbing MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- build-requirements.mjs | 3 +- package.json | 2 + pnpm-lock.yaml | 80 ++++++++++++++++++++ src/charging-station/Bootstrap.ts | 4 +- test/charging-station/ChargingStationTest.ts | 0 tsconfig.json | 2 +- 6 files changed, 86 insertions(+), 5 deletions(-) create mode 100644 test/charging-station/ChargingStationTest.ts diff --git a/build-requirements.mjs b/build-requirements.mjs index d3e7fe87..167f3656 100644 --- a/build-requirements.mjs +++ b/build-requirements.mjs @@ -2,12 +2,11 @@ import chalk from 'chalk'; import semVer from 'semver'; import packageJson from './package.json' assert { type: 'json' }; -const enginesNodeVersion = packageJson.engines.node; - /** * Check if the current node version match the required engines version. */ export function checkNodeVersion() { + const enginesNodeVersion = packageJson.engines.node; if (semVer.satisfies(process.version, enginesNodeVersion) === false) { console.error( chalk.red( diff --git a/package.json b/package.json index 8457499a..899dd131 100644 --- a/package.json +++ b/package.json @@ -115,6 +115,7 @@ "@types/mochawesome": "^6.2.1", "@types/node": "^18.15.7", "@types/proper-lockfile": "^4.1.2", + "@types/sinon": "^10.0.13", "@types/tar": "^6.1.4", "@types/ws": "^8.5.4", "@typescript-eslint/eslint-plugin": "^5.56.0", @@ -145,6 +146,7 @@ "rollup-plugin-copy": "^3.4.0", "rollup-plugin-delete": "^2.0.0", "semver": "^7.3.8", + "sinon": "^15.0.2", "ts-node": "^10.9.1", "typescript": "^5.0.2" } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 523ec42b..8421f85a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -16,6 +16,7 @@ specifiers: '@types/mochawesome': ^6.2.1 '@types/node': ^18.15.7 '@types/proper-lockfile': ^4.1.2 + '@types/sinon': ^10.0.13 '@types/tar': ^6.1.4 '@types/ws': ^8.5.4 '@typescript-eslint/eslint-plugin': ^5.56.0 @@ -60,6 +61,7 @@ specifiers: rollup-plugin-copy: ^3.4.0 rollup-plugin-delete: ^2.0.0 semver: ^7.3.8 + sinon: ^15.0.2 source-map-support: ^0.5.21 tar: ^6.1.13 ts-node: ^10.9.1 @@ -111,6 +113,7 @@ devDependencies: '@types/mochawesome': 6.2.1 '@types/node': 18.15.7 '@types/proper-lockfile': 4.1.2 + '@types/sinon': 10.0.13 '@types/tar': 6.1.4 '@types/ws': 8.5.4 '@typescript-eslint/eslint-plugin': 5.56.0_2hcjazgfnbtq42tcc73br2vup4 @@ -141,6 +144,7 @@ devDependencies: rollup-plugin-copy: 3.4.0 rollup-plugin-delete: 2.0.0 semver: 7.3.8 + sinon: 15.0.2 ts-node: 10.9.1_gkjsv6qbk4dkl5zwoikfngqpvy typescript: 5.0.2 @@ -1263,6 +1267,36 @@ packages: engines: {node: '>=14.16'} dev: true + /@sinonjs/commons/2.0.0: + resolution: {integrity: sha512-uLa0j859mMrg2slwQYdO/AkrOfmH+X6LTVmNTS9CqexuE2IvVORIkSpJLqePAbEnKJ77aMmCwr1NUZ57120Xcg==} + dependencies: + type-detect: 4.0.8 + dev: true + + /@sinonjs/commons/3.0.0: + resolution: {integrity: sha512-jXBtWAF4vmdNmZgD5FoKsVLv3rPgDnLgPbU84LIJ3otV44vJlDRokVng5v8NFJdCf/da9legHcKaRuZs4L7faA==} + dependencies: + type-detect: 4.0.8 + dev: true + + /@sinonjs/fake-timers/10.0.2: + resolution: {integrity: sha512-SwUDyjWnah1AaNl7kxsa7cfLhlTYoiyhDAIgyh+El30YvXs/o7OLXpYH88Zdhyx9JExKrmHDJ+10bwIcY80Jmw==} + dependencies: + '@sinonjs/commons': 2.0.0 + dev: true + + /@sinonjs/samsam/7.0.1: + resolution: {integrity: sha512-zsAk2Jkiq89mhZovB2LLOdTCxJF4hqqTToGP0ASWlhp4I1hqOjcfmZGafXntCN7MDC6yySH0mFHrYtHceOeLmw==} + dependencies: + '@sinonjs/commons': 2.0.0 + lodash.get: 4.4.2 + type-detect: 4.0.8 + dev: true + + /@sinonjs/text-encoding/0.7.2: + resolution: {integrity: sha512-sXXKG+uL9IrKqViTtao2Ws6dy0znu9sOaP1di/jKGW1M6VssO8vlpXCQcpZ+jisQ1tTFAC5Jo/EOzFbggBagFQ==} + dev: true + /@szmarczak/http-timer/1.1.2: resolution: {integrity: sha512-XIB2XbzHTN6ieIjfIMV9hlVcfPU26s2vafYWQcZHWXHOxiaRZYEDKEwdl129Zyg50+foYV2jCgtrqSA6qNuNSA==} engines: {node: '>=6'} @@ -1447,6 +1481,16 @@ packages: resolution: {integrity: sha512-21cFJr9z3g5dW8B0CVI9g2O9beqaThGQ6ZFBqHfwhzLDKUxaqTIy3vnfah/UPkfOiF2pLq+tGz+W8RyCskuslw==} dev: true + /@types/sinon/10.0.13: + resolution: {integrity: sha512-UVjDqJblVNQYvVNUsj0PuYYw0ELRmgt1Nt5Vk0pT5f16ROGfcKJY8o1HVuMOJOpD727RrGB9EGvoaTQE5tgxZQ==} + dependencies: + '@types/sinonjs__fake-timers': 8.1.2 + dev: true + + /@types/sinonjs__fake-timers/8.1.2: + resolution: {integrity: sha512-9GcLXF0/v3t80caGs5p2rRfkB+a8VBGLJZVih6CNFkx8IZ994wiKKLSRs9nuFwk1HevWs/1mnUmkApGrSGsShA==} + dev: true + /@types/stack-utils/2.0.1: resolution: {integrity: sha512-Hl219/BT5fLAaz6NDkSuhzasy49dwQS/DSdu4MdggFB8zcXv7vflBI3xp7FEmkmdDkBUI2bPUNeMttp2knYdxw==} dev: true @@ -5990,6 +6034,10 @@ packages: resolution: {integrity: sha512-1IynUYEc/HAwxhi3WDpIpxJbZpMCvvrrmZVqvj9EhpvbH8lls7HhdhiByjL7DkAaWlLIzpC0Xc/VPvy/UxLNjA==} dev: false + /just-extend/4.2.1: + resolution: {integrity: sha512-g3UB796vUFIY90VIv/WX3L2c8CS2MdWUww3CNrYmqza1Fg0DURc2K/O4YrnklBdQarSJ/y8JnJYDGc+1iumQjg==} + dev: true + /just-merge/3.2.0: resolution: {integrity: sha512-cNh5FWt44hx4SpQS1xZU8Tzr/fQA69pqCdjbwxmaYYIOuRfA8EIg+dn1bGmIW03ZUtR2vkMOCjWKc+jIbpauSw==} dev: false @@ -7085,6 +7133,16 @@ packages: resolution: {integrity: sha512-CXdUiJembsNjuToQvxayPZF9Vqht7hewsvy2sOWafLvi2awflj9mOC6bHIg50orX8IJvWKY9wYQ/zB2kogPslQ==} dev: true + /nise/5.1.4: + resolution: {integrity: sha512-8+Ib8rRJ4L0o3kfmyVCL7gzrohyDe0cMFTBa2d364yIrEGMEoetznKJx899YxjybU6bL9SQkYPSBBs1gyYs8Xg==} + dependencies: + '@sinonjs/commons': 2.0.0 + '@sinonjs/fake-timers': 10.0.2 + '@sinonjs/text-encoding': 0.7.2 + just-extend: 4.2.1 + path-to-regexp: 1.8.0 + dev: true + /no-case/2.3.2: resolution: {integrity: sha512-rmTZ9kz+f3rCvK2TD1Ue/oZlns7OGoIWP4fc3llxxRXlOkHKoWPPWJOfFYpITabSow43QJbRIoHQXtt10VldyQ==} dependencies: @@ -7656,6 +7714,12 @@ packages: minipass: 4.2.5 dev: true + /path-to-regexp/1.8.0: + resolution: {integrity: sha512-n43JRhlUKUAlibEJhPeir1ncUID16QnEjNpwzNdO3Lm4ywrBpBZ5oLD0I6br9evr1Y9JTqwRtAh7JLoOzAQdVA==} + dependencies: + isarray: 0.0.1 + dev: true + /path-type/4.0.0: resolution: {integrity: sha512-gDKb8aZMDeD/tZWs9P6+q0J9Mwkdl6xMV8TjnGP3qJVJ06bdMgkbBlLU8IdfOsIsFz2BW1rNVT3XuNEl8zPAvw==} engines: {node: '>=8'} @@ -8647,6 +8711,17 @@ packages: string-width: 1.0.2 dev: true + /sinon/15.0.2: + resolution: {integrity: sha512-PCVP63XZkg0/LOqQH5rEU4LILuvTFMb5tNxTHfs6VUMNnZz2XrnGSTZbAGITjzwQWbl/Bl/8hi4G3zZWjyBwHg==} + dependencies: + '@sinonjs/commons': 3.0.0 + '@sinonjs/fake-timers': 10.0.2 + '@sinonjs/samsam': 7.0.1 + diff: 5.1.0 + nise: 5.1.4 + supports-color: 7.2.0 + dev: true + /sinusoidal-decimal/1.0.0: resolution: {integrity: sha512-KPUi1ZqLocV64n0AuV+g4VDjAM+tEEY66nUd+rYaVBHIfeheGGUvIxe9bf7Mpc1PonDTVW2uRr9nigQa9odvuA==} dev: true @@ -9490,6 +9565,11 @@ packages: resolution: {integrity: sha512-mDZRBQS2yZkwRQKfjJvQ8UIYJeBNNWCq+HBNstl9N5s9jZ4dkVYXEGkVPsSCEh5Ld4JM1kmrZTzjnrqSAIQ7dw==} dev: true + /type-detect/4.0.8: + resolution: {integrity: sha512-0fr/mIH1dlO+x7TlcMy+bIDqKPsw/70tVyeHW787goQjhmqaZe10uwLujubK9q9Lg6Fiho1KUKDYz0Z7k7g5/g==} + engines: {node: '>=4'} + dev: true + /type-fest/0.18.1: resolution: {integrity: sha512-OIAYXk8+ISY+qTOwkHtKqzAuxchoMiD9Udx+FSGQDuiRR+PJKJHc2NJAXlbhkGwTt/4/nKZxELY1w3ReWOL8mw==} engines: {node: '>=10'} diff --git a/src/charging-station/Bootstrap.ts b/src/charging-station/Bootstrap.ts index 6be5d275..00a7df7d 100644 --- a/src/charging-station/Bootstrap.ts +++ b/src/charging-station/Bootstrap.ts @@ -7,7 +7,7 @@ import { type Worker, isMainThread } from 'node:worker_threads'; import chalk from 'chalk'; import { type AbstractUIServer, ChargingStationUtils, UIServerFactory } from './internal'; -import { version } from '../../package.json'; +import packageJson from '../../package.json' assert { type: 'json' }; import { BaseError } from '../exception'; import { type Storage, StorageFactory } from '../performance'; import { @@ -37,7 +37,7 @@ export class Bootstrap { private readonly uiServer!: AbstractUIServer | null; private readonly storage!: Storage; private numberOfStartedChargingStations!: number; - private readonly version: string = version; + private readonly version: string = packageJson.version; private initializedCounters: boolean; private started: boolean; private readonly workerScript: string; diff --git a/test/charging-station/ChargingStationTest.ts b/test/charging-station/ChargingStationTest.ts new file mode 100644 index 00000000..e69de29b diff --git a/tsconfig.json b/tsconfig.json index f30d942d..b8078fc3 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,7 +5,7 @@ /* Basic Options */ // "incremental": true, /* Enable incremental compilation */ "target": "es2022", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */ - "module": "es2022", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */ + "module": "esnext", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */ "lib": ["es2022"], /* Specify library files to be included in the compilation. */ // "allowJs": true, /* Allow javascript files to be compiled. */ // "checkJs": true, /* Report errors in .js files. */ -- 2.34.1