44 * This source code is licensed under the MIT license found in the
55 * LICENSE file in the root directory of this source tree.
66 *
7+ * @flow strict-local
78 * @format
9+ * @oncall react_native
810 */
911
1012const { PUBLISH_PACKAGES_TAG } = require ( '../constants' ) ;
1113const {
1214 findAndPublishAllBumpedPackages,
1315 getTagsFromCommitMessage,
1416} = require ( '../find-and-publish-all-bumped-packages' ) ;
15- const forEachPackage = require ( '../for-each-package' ) ;
16- const { spawnSync} = require ( 'child_process' ) ;
1717
18- jest . mock ( 'child_process' , ( ) => ( { spawnSync : jest . fn ( ) } ) ) ;
19- jest . mock ( '../for-each-package' , ( ) => jest . fn ( ) ) ;
18+ const spawnSync = jest . fn ( ) ;
19+ const forEachPackage = jest . fn ( ) ;
20+ const execMock = jest . fn ( ) ;
21+
22+ jest . mock ( 'child_process' , ( ) => ( { spawnSync} ) ) ;
23+ jest . mock ( 'shelljs' , ( ) => ( { exec : execMock } ) ) ;
24+ jest . mock ( '../for-each-package' , ( ) => forEachPackage ) ;
2025
2126describe ( 'findAndPublishAllBumpedPackages' , ( ) => {
2227 beforeEach ( ( ) => {
23- // Silence logs.
2428 jest . spyOn ( console , 'log' ) . mockImplementation ( ( ) => { } ) ;
2529 } ) ;
26- it ( 'throws an error if updated version is not 0.x.y' , ( ) => {
30+
31+ test ( 'should throw an error if updated version is not 0.x.y' , async ( ) => {
2732 const mockedPackageNewVersion = '1.0.0' ;
2833
2934 forEachPackage . mockImplementationOnce ( callback => {
@@ -40,10 +45,64 @@ describe('findAndPublishAllBumpedPackages', () => {
4045 stdout : `This is my commit message\n\n${ PUBLISH_PACKAGES_TAG } ` ,
4146 } ) ) ;
4247
43- expect ( ( ) => findAndPublishAllBumpedPackages ( ) ) . toThrow (
48+ await expect ( findAndPublishAllBumpedPackages ( ) ) . rejects . toThrow (
4449 `Package version expected to be 0.x.y, but received ${ mockedPackageNewVersion } ` ,
4550 ) ;
4651 } ) ;
52+
53+ test ( 'should publish all changed packages' , async ( ) => {
54+ forEachPackage . mockImplementationOnce ( callback => {
55+ callback ( 'absolute/path/to/package-a' , 'to/package-a' , {
56+ version : '0.72.1' ,
57+ } ) ;
58+ callback ( 'absolute/path/to/package-b' , 'to/package-b' , {
59+ version : '0.72.1' ,
60+ } ) ;
61+ callback ( 'absolute/path/to/package-c' , 'to/package-b' , {
62+ version : '0.72.0' ,
63+ } ) ;
64+ } ) ;
65+
66+ spawnSync . mockImplementationOnce ( ( ) => ( {
67+ stdout : `- "version": "0.72.0"\n+ "version": "0.72.1"\n` ,
68+ } ) ) ;
69+ spawnSync . mockImplementationOnce ( ( ) => ( {
70+ stdout : `This is my commit message\n\n${ PUBLISH_PACKAGES_TAG } ` ,
71+ } ) ) ;
72+ spawnSync . mockImplementationOnce ( ( ) => ( {
73+ stdout : `- "version": "0.72.0"\n+ "version": "0.72.1"\n` ,
74+ } ) ) ;
75+ spawnSync . mockImplementationOnce ( ( ) => ( {
76+ stdout : `This is my commit message\n\n${ PUBLISH_PACKAGES_TAG } ` ,
77+ } ) ) ;
78+ spawnSync . mockImplementationOnce ( ( ) => ( {
79+ stdout : '\n' ,
80+ } ) ) ;
81+ spawnSync . mockImplementationOnce ( ( ) => ( {
82+ stdout : `This is my commit message\n\n${ PUBLISH_PACKAGES_TAG } ` ,
83+ } ) ) ;
84+
85+ execMock . mockImplementation ( ( ) => ( { code : 0 } ) ) ;
86+
87+ await findAndPublishAllBumpedPackages ( ) ;
88+
89+ expect ( execMock . mock . calls ) . toMatchInlineSnapshot ( `
90+ Array [
91+ Array [
92+ "npm publish",
93+ Object {
94+ "cwd": "absolute/path/to/package-a",
95+ },
96+ ],
97+ Array [
98+ "npm publish",
99+ Object {
100+ "cwd": "absolute/path/to/package-b",
101+ },
102+ ],
103+ ]
104+ ` ) ;
105+ } ) ;
47106} ) ;
48107
49108describe ( 'getTagsFromCommitMessage' , ( ) => {
0 commit comments