11const { generateChangelog, logger } = require ( '@gitmoji-changelog/core' )
2+ const { manifest } = require ( 'libnpm' )
23const { main } = require ( './cli' )
34
45describe ( 'cli' , ( ) => {
6+ const realExitFunction = process . exit
7+ beforeEach ( ( ) => {
8+ process . exit = jest . fn ( ( ) => { } )
9+ jest . clearAllMocks ( )
10+ } )
11+ afterEach ( ( ) => {
12+ process . exit = realExitFunction
13+ } )
14+
515 it ( 'should throw an error if changelog generation fails' , async ( ) => {
616 generateChangelog . mockRejectedValue ( new Error ( ) )
717
818 await main ( )
919
1020 expect ( logger . error ) . toHaveBeenCalled ( )
1121 } )
22+
23+ it ( 'should call process.exit explicitly so promises are not waited for' , async ( ) => {
24+ await main ( )
25+
26+ expect ( process . exit ) . toHaveBeenCalledTimes ( 1 )
27+ } )
28+
29+ describe ( 'version control' , ( ) => {
30+ const findOutdatedMessage = ( ) => logger . warn . mock . calls . find ( ( [ message ] ) => message . includes ( 'outdated' ) )
31+
32+ it ( 'should print a warning about a new version' , async ( ) => {
33+ manifest . mockReturnValueOnce ( Promise . resolve ( { version : '2.0.0' } ) )
34+ await main ( )
35+
36+ expect ( findOutdatedMessage ( ) ) . toBeTruthy ( )
37+ } )
38+
39+ it ( 'should NOT print a warning about a new version' , async ( ) => {
40+ // older version in npm registry
41+ manifest . mockReturnValueOnce ( Promise . resolve ( { version : '0.5.0' } ) )
42+ await main ( )
43+
44+ // same version in npm registry
45+ manifest . mockReturnValueOnce ( Promise . resolve ( { version : '1.0.0' } ) )
46+ await main ( )
47+
48+ expect ( manifest ) . toHaveBeenCalledTimes ( 2 )
49+ expect ( findOutdatedMessage ( ) ) . toBeFalsy ( )
50+ } )
51+
52+ it ( 'should NOT print a warning about a new version when request took to much time' , async ( ) => {
53+ manifest . mockImplementationOnce ( ( ) => new Promise ( ( resolve ) => { setTimeout ( resolve , 1000 , { version : '2.0.0' } ) } ) )
54+ await main ( )
55+
56+ expect ( findOutdatedMessage ( ) ) . toBeFalsy ( )
57+ } )
58+
59+ it ( 'should NOT print a warning about a new version when request is on error' , async ( ) => {
60+ manifest . mockReturnValueOnce ( Promise . reject ( new Error ( 'faked error (manifest)' ) ) )
61+ await main ( )
62+
63+ expect ( findOutdatedMessage ( ) ) . toBeFalsy ( )
64+ } )
65+ } )
1266} )
1367
1468jest . mock ( '@gitmoji-changelog/core' , ( ) => ( {
@@ -18,5 +72,14 @@ jest.mock('@gitmoji-changelog/core', () => ({
1872 error : jest . fn ( ) ,
1973 success : jest . fn ( ) ,
2074 info : jest . fn ( ) ,
75+ warn : jest . fn ( ) ,
2176 } ,
2277} ) )
78+
79+ jest . mock ( '../package.json' , ( ) => ( {
80+ version : '1.0.0' ,
81+ } ) )
82+
83+ jest . mock ( 'libnpm' , ( ) => ( {
84+ manifest : jest . fn ( ) ,
85+ } ) )
0 commit comments