Files
react-native/scripts/releases/utils/__tests__/npm-utils-test.js
Alex Hunt fc5e33b582 Reorganise shared script utils (#52473)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/52473

Shared utils that were located in the root of `scripts/` are now colocated closer to their dependencies or moved to `scripts/shared/` — simplifying the root directory layout.

Changelog: [Internal]

Reviewed By: robhogan

Differential Revision: D77873875

fbshipit-source-id: e04dba41a1ef811d32793931033fdfa93afad0cd
2025-07-08 06:10:36 -07:00

102 lines
3.2 KiB
JavaScript

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
*/
const {getNpmInfo, publishPackage} = require('../npm-utils');
const execMock = jest.fn();
const getCurrentCommitMock = jest.fn();
const exitIfNotOnGitMock = jest.fn();
jest.mock('shelljs', () => ({
exec: execMock,
}));
jest.mock('./../scm-utils', () => ({
getCurrentCommit: getCurrentCommitMock,
exitIfNotOnGit: exitIfNotOnGitMock,
}));
describe('npm-utils', () => {
beforeEach(() => {
jest.resetModules();
jest.resetAllMocks();
});
describe('publishPackage', () => {
it('should run publish command', () => {
publishPackage(
'path/to/my-package',
{tags: ['latest'], otp: 'otp'},
{silent: true, cwd: 'i/expect/this/to/be/overriden'},
);
expect(execMock).toHaveBeenCalledWith(
'npm publish --tag latest --otp otp',
{silent: true, cwd: 'path/to/my-package'},
);
});
it('should run publish command when no execOptions', () => {
publishPackage('path/to/my-package', {tags: ['latest'], otp: 'otp'});
expect(execMock).toHaveBeenCalledWith(
'npm publish --tag latest --otp otp',
{cwd: 'path/to/my-package'},
);
});
it('should handle multiple tags', () => {
publishPackage('path/to/my-package', {
tags: ['next', '0.72-stable'],
otp: 'otp',
});
expect(execMock).toHaveBeenCalledWith(
'npm publish --tag next --tag 0.72-stable --otp otp',
{cwd: 'path/to/my-package'},
);
});
it('should handle -no-tag', () => {
publishPackage('path/to/my-package', {tags: ['--no-tag'], otp: 'otp'});
expect(execMock).toHaveBeenCalledWith('npm publish --no-tag --otp otp', {
cwd: 'path/to/my-package',
});
});
});
describe('getNpmInfo', () => {
beforeEach(() => {
process.env.GITHUB_REF = '';
process.env.GITHUB_REF_NAME = '';
});
it('return the expected format for patch-prereleases on GHA', () => {
const isoStringSpy = jest.spyOn(Date.prototype, 'toISOString');
isoStringSpy.mockReturnValue('2023-10-04T15:43:55.123Z');
getCurrentCommitMock.mockImplementation(() => 'abcd1234');
// exitIfNotOnGit takes a function as a param and it:
// 1. checks if we are on git => if not it exits
// 2. run the function passed as a param and return the output to the caller
// For the mock, we are assuming we are on github and we are returning `false`
// as the `getNpmInfo` function will pass a function that checks if the
// current commit is a tagged with 'latest'.
// In the Mock, we are assuming that we are on git (it does not exits) and the
// checkIfLatest function returns `false`
exitIfNotOnGitMock.mockImplementation(() => false);
process.env.GITHUB_REF = 'refs/tags/v0.74.1-rc.0';
process.env.GITHUB_REF_NAME = 'v0.74.1-rc.0';
const returnedValue = getNpmInfo('release');
expect(returnedValue).toMatchObject({
version: `0.74.1-rc.0`,
tag: '--no-tag',
});
});
});
});