|
| 1 | +describe("emitWarningIfUnsupportedVersion", () => { |
| 2 | + let emitWarningIfUnsupportedVersion; |
| 3 | + const emitWarning = process.emitWarning; |
| 4 | + const supportedVersion = "12.0.0"; |
| 5 | + |
| 6 | + beforeEach(() => { |
| 7 | + const module = require("./emitWarningIfUnsupportedVersion"); |
| 8 | + emitWarningIfUnsupportedVersion = module.emitWarningIfUnsupportedVersion; |
| 9 | + }); |
| 10 | + |
| 11 | + afterEach(() => { |
| 12 | + jest.clearAllMocks(); |
| 13 | + jest.resetModules(); |
| 14 | + process.emitWarning = emitWarning; |
| 15 | + }); |
| 16 | + |
| 17 | + describe(`emits warning for Node.js <${supportedVersion}`, () => { |
| 18 | + const getPreviousMajorVersion = (major: number) => (major === 0 ? 0 : major - 1); |
| 19 | + |
| 20 | + const getPreviousMinorVersion = ([major, minor]: [number, number]) => |
| 21 | + minor === 0 ? [getPreviousMajorVersion(major), 9] : [major, minor - 1]; |
| 22 | + |
| 23 | + const getPreviousPatchVersion = ([major, minor, patch]: [number, number, number]) => |
| 24 | + patch === 0 ? [...getPreviousMinorVersion([major, minor]), 9] : [major, minor, patch - 1]; |
| 25 | + |
| 26 | + const [major, minor, patch] = supportedVersion.split(".").map(Number); |
| 27 | + it.each( |
| 28 | + [ |
| 29 | + getPreviousPatchVersion([major, minor, patch]), |
| 30 | + [...getPreviousMinorVersion([major, minor]), 0], |
| 31 | + [getPreviousMajorVersion(major), 0, 0], |
| 32 | + ].map((arr) => `v${arr.join(".")}`) |
| 33 | + )(`%s`, async (unsupportedVersion) => { |
| 34 | + process.emitWarning = jest.fn(); |
| 35 | + emitWarningIfUnsupportedVersion(unsupportedVersion); |
| 36 | + |
| 37 | + // Verify that the warning was emitted. |
| 38 | + expect(process.emitWarning).toHaveBeenCalledTimes(1); |
| 39 | + expect(process.emitWarning).toHaveBeenCalledWith( |
| 40 | + `The AWS SDK for JavaScript (v3) will\n` + |
| 41 | + `no longer support Node.js ${unsupportedVersion} as of January 1, 2022.\n` + |
| 42 | + `To continue receiving updates to AWS services, bug fixes, and security\n` + |
| 43 | + `updates please upgrade to Node.js 12.x or later.\n\n` + |
| 44 | + `More information can be found at: https://a.co/1l6FLnu`, |
| 45 | + `NodeDeprecationWarning` |
| 46 | + ); |
| 47 | + |
| 48 | + // Verify that the warning emits only once. |
| 49 | + emitWarningIfUnsupportedVersion(unsupportedVersion); |
| 50 | + expect(process.emitWarning).toHaveBeenCalledTimes(1); |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + describe(`emits no warning for Node.js >=${supportedVersion}`, () => { |
| 55 | + const [major, minor, patch] = supportedVersion.split(".").map(Number); |
| 56 | + it.each( |
| 57 | + [ |
| 58 | + [major, minor, patch], |
| 59 | + [major, minor, patch + 1], |
| 60 | + [major, minor + 1, 0], |
| 61 | + [major + 1, 0, 0], |
| 62 | + ].map((arr) => `v${arr.join(".")}`) |
| 63 | + )(`%s`, async (unsupportedVersion) => { |
| 64 | + process.emitWarning = jest.fn(); |
| 65 | + emitWarningIfUnsupportedVersion(unsupportedVersion); |
| 66 | + expect(process.emitWarning).not.toHaveBeenCalled(); |
| 67 | + }); |
| 68 | + }); |
| 69 | +}); |
0 commit comments