Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

checkNodeVersion.test.ts 2.4 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
  1. import chalk from 'chalk';
  2. import * as fs from 'fs';
  3. import * as path from 'path';
  4. import { checkNodeVersion } from '../src/checkNodeVersion';
  5. import logger from '../src/logger';
  6. jest.mock('fs');
  7. jest.mock('../src/logger');
  8. jest.mock('path');
  9. const mockExit = jest.spyOn(process, 'exit').mockImplementation(() => {
  10. throw new Error('process.exit called');
  11. });
  12. const setNodeVersion = (version: string) => {
  13. Object.defineProperty(process, 'version', {
  14. value: version,
  15. configurable: true,
  16. });
  17. };
  18. describe('checkNodeVersion', () => {
  19. const originalProcessVersion = process.version;
  20. beforeEach(() => {
  21. jest.resetAllMocks();
  22. jest.mocked(path.resolve).mockImplementation(() => 'mocked/path/to/package.json');
  23. jest.mocked(fs.readFileSync).mockImplementation(() =>
  24. JSON.stringify({
  25. engines: { node: '>=18.0.0' },
  26. }),
  27. );
  28. });
  29. afterEach(() => {
  30. setNodeVersion(originalProcessVersion);
  31. });
  32. afterAll(() => {
  33. mockExit.mockRestore();
  34. });
  35. it('should not log a warning if Node.js version is supported', () => {
  36. setNodeVersion('v18.10.0');
  37. expect(() => checkNodeVersion()).not.toThrow();
  38. expect(logger.warn).not.toHaveBeenCalled();
  39. });
  40. it('should log a warning and exit if Node.js version is too low', () => {
  41. setNodeVersion('v16.10.0');
  42. expect(checkNodeVersion()).toBeUndefined();
  43. expect(logger.warn).toHaveBeenCalledWith(
  44. chalk.yellow(
  45. 'You are using Node.js 16.10.0. This version is not supported. Please use Node.js >=18.0.0.',
  46. ),
  47. );
  48. });
  49. it('should log a warning if Node.js version format is unexpected', () => {
  50. setNodeVersion('v16');
  51. expect(() => checkNodeVersion()).not.toThrow(); // To ensure the test does not fail due to process.exit
  52. expect(logger.warn).toHaveBeenCalledWith(
  53. chalk.yellow('Unexpected Node.js version format: v16. Please use Node.js >=18.0.0.'),
  54. );
  55. });
  56. it('should handle version strings correctly and exit if required version is not met', () => {
  57. setNodeVersion('v18.0.0');
  58. jest.mocked(fs.readFileSync).mockImplementation(() =>
  59. JSON.stringify({
  60. engines: { node: '>=18.0.1' },
  61. }),
  62. );
  63. expect(checkNodeVersion()).toBeUndefined();
  64. expect(logger.warn).toHaveBeenCalledWith(
  65. chalk.yellow(
  66. 'You are using Node.js 18.0.0. This version is not supported. Please use Node.js >=18.0.1.',
  67. ),
  68. );
  69. });
  70. });
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...