@naturalcycles/dev-lib 16.1.0 → 16.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,8 @@
1
+ import { defineConfig } from 'vitest/config'
2
+ import { sharedConfig } from '@naturalcycles/dev-lib/cfg/vitest.config.mjs'
3
+
4
+ export default defineConfig({
5
+ test: {
6
+ ...sharedConfig,
7
+ },
8
+ })
@@ -0,0 +1,3 @@
1
+ import type { InlineConfig } from 'vitest/node'
2
+
3
+ export const sharedConfig: InlineConfig
@@ -0,0 +1,117 @@
1
+ import fs from 'node:fs'
2
+
3
+ let silent
4
+ let testType = process.env['TEST_TYPE'] || 'unit'
5
+
6
+ const runsInIDE = process.argv.some(
7
+ a => a === '--runTestsByPath' || a.includes('IDEA') || a.includes('Visual Studio'),
8
+ )
9
+
10
+ if (runsInIDE) {
11
+ silent = false
12
+
13
+ if (process.argv.some(a => a.endsWith('.integration.test.ts'))) {
14
+ testType = 'integration'
15
+ } else if (process.argv.some(a => a.endsWith('.manual.test.ts'))) {
16
+ testType = 'manual'
17
+ }
18
+ } else {
19
+ silent = isRunningAllTests()
20
+ }
21
+
22
+ const isCI = !!process.env['CI']
23
+ process.env.TZ = process.env.TZ || 'UTC'
24
+ if (testType === 'unit') {
25
+ process.env['APP_ENV'] = process.env['APP_ENV'] || 'test'
26
+ }
27
+
28
+ // Set 'setupFiles' only if setup files exist
29
+ const setupFiles = []
30
+ if (fs.existsSync(`./src/test/setupVitest.ts`)) {
31
+ setupFiles.push('./src/test/setupVitest.ts')
32
+ }
33
+ if (fs.existsSync(`./src/test/setupVitest.${testType}.ts`)) {
34
+ setupFiles.push(`./src/test/setupVitest.${testType}.ts`)
35
+ }
36
+
37
+ let include
38
+ const exclude = ['**/__exclude/**']
39
+
40
+ if (testType === 'integration') {
41
+ include = ['{src,scripts}/**/*.integration.test.ts']
42
+ } else if (testType === 'manual') {
43
+ include = ['{src,scripts}/**/*.manual.test.ts']
44
+ } else {
45
+ // normal unit test
46
+ include = ['{src,scripts}/**/*.test.ts']
47
+ exclude.push('**/*.{integration,manual}.test.*')
48
+ }
49
+
50
+ if (silent) {
51
+ process.env['TEST_SILENT'] = 'true'
52
+ }
53
+
54
+ console.log('shared vitest config', { testType, silent, isCI, runsInIDE, include, exclude })
55
+
56
+ /**
57
+ * Shared config for Vitest.
58
+ */
59
+ export const sharedConfig = {
60
+ watch: false,
61
+ // dir: 'src',
62
+ restoreMocks: true,
63
+ silent,
64
+ setupFiles,
65
+ logHeapUsage: true,
66
+ testTimeout: 60_000,
67
+ sequence: {
68
+ // todo: make it sort alphabetically
69
+ },
70
+ include,
71
+ exclude,
72
+ coverage: {
73
+ enabled: isCI && testType === 'unit',
74
+ reporter: ['html', 'lcov', 'json', !isCI && 'text'].filter(Boolean),
75
+ include: ['src/**/*.{ts,tsx}'],
76
+ exclude: [
77
+ '**/__exclude/**',
78
+ 'scripts/**',
79
+ 'public/**',
80
+ 'src/index.*',
81
+ 'src/test/**',
82
+ 'src/typings/**',
83
+ 'src/{env,environment,environments}/**',
84
+ 'src/bin/**',
85
+ 'src/vendor/**',
86
+ '**/*.test.*',
87
+ '**/*.script.*',
88
+ '**/*.module.*',
89
+ '**/*.mock.*',
90
+ '**/*.page.*',
91
+ '**/*.component.*',
92
+ '**/*.modal.*',
93
+ ],
94
+ },
95
+ }
96
+
97
+ /**
98
+ * Detects if vitest is run with all tests, or with selected individual tests.
99
+ */
100
+ function isRunningAllTests() {
101
+ let vitestArg = false
102
+ let hasPositionalArgs = false
103
+ process.argv.forEach(a => {
104
+ if (a.includes('.bin/vitest')) {
105
+ vitestArg = true
106
+ return
107
+ }
108
+ if (!vitestArg) return
109
+ if (!a.startsWith('-')) {
110
+ hasPositionalArgs = true
111
+ }
112
+ })
113
+ // console.log({vitestArg, hasPositionalArgs}, process.argv)
114
+
115
+ return !hasPositionalArgs
116
+ }
117
+
@@ -39,12 +39,17 @@ const commands = [
39
39
  {
40
40
  name: 'test-integration',
41
41
  fn: () => (0, test_util_1.runTest)({ integration: true }),
42
- desc: 'Run jest for *.integration.test.ts files.',
42
+ desc: 'Run vitest/jest for *.integration.test.ts files.',
43
+ },
44
+ {
45
+ name: 'test-manual',
46
+ fn: () => (0, test_util_1.runTest)({ manual: true }),
47
+ desc: 'Run vitest/jest for *.manual.test.ts files.',
43
48
  },
44
49
  {
45
50
  name: 'test-leaks',
46
51
  fn: () => (0, test_util_1.runTest)({ leaks: true }),
47
- desc: 'Run jest --detectLeaks for *.test.ts files.',
52
+ desc: 'Run vitest/jest --detectLeaks for *.test.ts files.',
48
53
  },
49
54
  new prompts_1.Separator(), // lint
50
55
  {
package/dist/lint.util.js CHANGED
@@ -182,20 +182,10 @@ function stylelintAll() {
182
182
  });
183
183
  }
184
184
  async function lintStagedCommand() {
185
- // const cwd = process.cwd()
186
185
  const localConfig = `./lint-staged.config.js`;
187
186
  const sharedConfig = `${paths_1.cfgDir}/lint-staged.config.js`;
188
187
  const config = node_fs_1.default.existsSync(localConfig) ? localConfig : sharedConfig;
189
- // if (!config) {
190
- // console.log(`lint-staged is skipped, because no ${localConfig} is found`)
191
- // return
192
- // }
193
- // await execWithArgs(`lint-staged`, [`--config`, config])
194
- // const lintStaged = require('lint-staged')
195
- // lint-staged is ESM since 12.0
196
- // const lintStaged = await import('lint-staged')
197
- // biome-ignore lint/security/noGlobalEval: ok
198
- const { default: lintStaged } = await eval(`import('lint-staged')`);
188
+ const { default: lintStaged } = await import('lint-staged');
199
189
  const success = await lintStaged({
200
190
  configPath: config,
201
191
  });
package/dist/test.util.js CHANGED
@@ -21,13 +21,18 @@ function runVitest(opt) {
21
21
  const { integration, manual } = opt;
22
22
  const processArgs = process.argv.slice(3);
23
23
  const args = [...processArgs];
24
- const { TZ = 'UTC', APP_ENV } = process.env;
24
+ const { TZ = 'UTC' } = process.env;
25
25
  const env = {
26
26
  TZ,
27
27
  };
28
- if (!integration && !manual && !APP_ENV) {
28
+ if (integration) {
29
29
  Object.assign(env, {
30
- APP_ENV: 'test',
30
+ TEST_TYPE: 'integration',
31
+ });
32
+ }
33
+ else if (manual) {
34
+ Object.assign(env, {
35
+ TEST_TYPE: 'manual',
31
36
  });
32
37
  }
33
38
  nodejs_lib_1.exec2.spawn('vitest', {
@@ -2,7 +2,6 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.testOffline = testOffline;
4
4
  exports.jestOnline = jestOnline;
5
- const index_1 = require("./index");
6
5
  const LOCAL_HOSTS = ['localhost', '127.0.0.1'];
7
6
  const detectLeaks = process.argv.some(a => a.includes('detectLeaks'));
8
7
  let mitm;
@@ -11,10 +10,10 @@ let mitm;
11
10
  */
12
11
  function testOffline() {
13
12
  if (detectLeaks) {
14
- (0, index_1.jestLog)('NOT applying testOffline() when --detectLeaks is on');
13
+ console.log('NOT applying testOffline() when --detectLeaks is on');
15
14
  return;
16
15
  }
17
- (0, index_1.jestLog)('test offline mode');
16
+ console.log('test offline mode');
18
17
  const createMitm = require('mitm');
19
18
  mitm ||= createMitm();
20
19
  mitm.on('connect', (socket, opts) => {
@@ -1,3 +1,3 @@
1
1
  export declare function silentConsole(): void;
2
- export declare const jestLogger: import("@naturalcycles/js-lib").CommonLogger;
3
- export declare const jestLog: import("@naturalcycles/js-lib").CommonLogFunction;
2
+ export declare const testLogger: import("@naturalcycles/js-lib").CommonLogger;
3
+ export declare const testLog: import("@naturalcycles/js-lib").CommonLogFunction;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.jestLog = exports.jestLogger = void 0;
3
+ exports.testLog = exports.testLogger = void 0;
4
4
  exports.silentConsole = silentConsole;
5
5
  const js_lib_1 = require("@naturalcycles/js-lib");
6
6
  const nodejs_lib_1 = require("@naturalcycles/nodejs-lib");
@@ -13,8 +13,8 @@ function silentConsole() {
13
13
  console.time = () => { };
14
14
  console.table = () => { };
15
15
  }
16
- exports.jestLogger = (0, js_lib_1.commonLoggerCreate)((_level, args) => {
17
- if (process.env['JEST_SILENT'])
16
+ exports.testLogger = (0, js_lib_1.commonLoggerCreate)((_level, args) => {
17
+ if (process.env['JEST_SILENT'] || process.env['TEST_SILENT'])
18
18
  return; // no-op
19
19
  process.stdout.write(args
20
20
  .map(a => (0, nodejs_lib_1._inspect)(a, {
@@ -22,4 +22,4 @@ exports.jestLogger = (0, js_lib_1.commonLoggerCreate)((_level, args) => {
22
22
  }))
23
23
  .join(' ') + '\n');
24
24
  });
25
- exports.jestLog = exports.jestLogger.log.bind(exports.jestLogger);
25
+ exports.testLog = exports.testLogger.log.bind(exports.testLogger);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/dev-lib",
3
- "version": "16.1.0",
3
+ "version": "16.2.1",
4
4
  "scripts": {
5
5
  "prepare": "husky",
6
6
  "tsn-debug": "tsn testScript.ts",
@@ -12,6 +12,7 @@
12
12
  "test": "tsn ./src/bin/dev-lib.ts test",
13
13
  "test-leaks": "tsn ./src/bin/dev-lib.ts test-leaks",
14
14
  "test-integration": "tsn ./src/bin/dev-lib.ts test-integration",
15
+ "test-manual": "tsn ./src/bin/dev-lib.ts test-manual",
15
16
  "lint": "tsn ./src/bin/dev-lib.ts lint",
16
17
  "up": "tsn ./src/bin/up.ts",
17
18
  "upnc": "tsn ./src/bin/upnc.ts"
@@ -1,3 +0,0 @@
1
- module.exports = {
2
- ...require('@naturalcycles/dev-lib/cfg/jest.config'),
3
- }
@@ -1,3 +0,0 @@
1
- module.exports = {
2
- ...require('@naturalcycles/dev-lib/cfg/jest.integration-test.config'),
3
- }