1
1
import axios from 'axios' ;
2
- import * as fs from 'fs' ;
2
+ import * as fs from 'fs-extra ' ;
3
3
import { Test , TestingModule } from '@nestjs/testing' ;
4
4
import {
5
5
mockMainCommands ,
@@ -10,18 +10,15 @@ import { CommandsJsonProvider } from 'src/modules/commands/commands-json.provide
10
10
jest . mock ( 'axios' ) ;
11
11
const mockedAxios = axios as jest . Mocked < typeof axios > ;
12
12
13
- jest . mock ( 'fs' ) ;
13
+ jest . mock ( 'fs-extra ' ) ;
14
14
const mockedFs = fs as jest . Mocked < typeof fs > ;
15
15
16
16
describe ( 'CommandsJsonProvider' , ( ) => {
17
17
let service : CommandsJsonProvider ;
18
18
19
19
beforeEach ( async ( ) => {
20
- jest . mock ( 'fs' , ( ) => mockedFs ) ;
20
+ jest . mock ( 'fs-extra ' , ( ) => mockedFs ) ;
21
21
22
- mockedFs . existsSync . mockReturnValue ( true ) ;
23
- mockedFs . mkdirSync . mockReturnValue ( '' ) ;
24
- mockedFs . writeFileSync . mockReturnValue ( undefined ) ;
25
22
mockedAxios . get . mockResolvedValue ( { data : JSON . stringify ( mockMainCommands ) } ) ;
26
23
27
24
const module : TestingModule = await Test . createTestingModule ( {
@@ -37,57 +34,47 @@ describe('CommandsJsonProvider', () => {
37
34
} ) ;
38
35
39
36
describe ( 'updateLatestJson' , ( ) => {
40
- it ( 'Should create dir and save proper json' , async ( ) => {
41
- mockedFs . existsSync . mockReturnValueOnce ( false ) ;
42
-
43
- await service . updateLatestJson ( ) ;
44
-
45
- // todo: uncomment after enable esModuleInterop in the tsconfig
46
- // expect(mockedFs.mkdirSync).toHaveBeenCalled();
47
- // expect(mockedFs.writeFileSync).toHaveBeenCalled();
48
- } ) ;
49
37
it ( 'should not fail when incorrect data retrieved' , async ( ) => {
50
38
mockedAxios . get . mockResolvedValueOnce ( 'json' ) ;
51
39
await service . updateLatestJson ( ) ;
52
40
53
- // todo: uncomment after enable esModuleInterop in the tsconfig
54
- // expect(mockedFs.writeFileSync).not.toHaveBeenCalled();
41
+ expect ( mockedFs . writeFile ) . not . toHaveBeenCalled ( ) ;
55
42
} ) ;
56
43
} ) ;
57
44
58
45
describe ( 'getCommands' , ( ) => {
59
46
it ( 'should return default config when file was not found' , async ( ) => {
60
- mockedFs . readFileSync . mockImplementationOnce ( ( ) => { throw new Error ( 'No file' ) ; } ) ;
61
- mockedFs . readFileSync . mockReturnValueOnce ( JSON . stringify ( mockMainCommands ) ) ;
47
+ mockedFs . readFile . mockRejectedValueOnce ( new Error ( 'No file' ) ) ;
48
+ mockedFs . readFile . mockResolvedValueOnce ( Buffer . from ( JSON . stringify ( mockMainCommands ) ) ) ;
62
49
63
50
expect ( await service . getCommands ( ) ) . toEqual ( mockMainCommands ) ;
64
51
} ) ;
65
52
it ( 'should return default config when incorrect json received from file' , async ( ) => {
66
- mockedFs . readFileSync . mockReturnValueOnce ( 'incorrect json' ) ;
67
- mockedFs . readFileSync . mockReturnValueOnce ( JSON . stringify ( mockMainCommands ) ) ;
53
+ mockedFs . readFile . mockResolvedValueOnce ( Buffer . from ( 'incorrect json' ) ) ;
54
+ mockedFs . readFile . mockResolvedValueOnce ( Buffer . from ( JSON . stringify ( mockMainCommands ) ) ) ;
68
55
69
56
expect ( await service . getCommands ( ) ) . toEqual ( mockMainCommands ) ;
70
57
} ) ;
71
58
it ( 'should return latest commands' , async ( ) => {
72
- mockedFs . readFileSync . mockReturnValueOnce ( JSON . stringify ( mockRedijsonCommands ) ) ;
59
+ mockedFs . readFile . mockResolvedValue ( Buffer . from ( JSON . stringify ( mockRedijsonCommands ) ) ) ;
73
60
74
61
expect ( await service . getCommands ( ) ) . toEqual ( mockRedijsonCommands ) ;
75
62
} ) ;
76
63
} ) ;
77
64
78
65
describe ( 'getDefaultCommands' , ( ) => {
79
66
it ( 'should return empty object when file was not found' , async ( ) => {
80
- mockedFs . readFileSync . mockImplementationOnce ( ( ) => { throw new Error ( 'No file' ) ; } ) ;
67
+ mockedFs . readFile . mockRejectedValue ( new Error ( 'No file' ) ) ;
81
68
82
69
expect ( await service . getDefaultCommands ( ) ) . toEqual ( { } ) ;
83
70
} ) ;
84
71
it ( 'should return empty object when incorrect json received from file' , async ( ) => {
85
- mockedFs . readFileSync . mockReturnValue ( 'incorrect json' ) ;
72
+ mockedFs . readFile . mockResolvedValue ( Buffer . from ( 'incorrect json' ) ) ;
86
73
87
74
expect ( await service . getDefaultCommands ( ) ) . toEqual ( { } ) ;
88
75
} ) ;
89
76
it ( 'should return default commands' , async ( ) => {
90
- mockedFs . readFileSync . mockReturnValue ( JSON . stringify ( mockRedijsonCommands ) ) ;
77
+ mockedFs . readFile . mockResolvedValue ( Buffer . from ( JSON . stringify ( mockRedijsonCommands ) ) ) ;
91
78
92
79
expect ( await service . getDefaultCommands ( ) ) . toEqual ( mockRedijsonCommands ) ;
93
80
} ) ;
0 commit comments