Skip to content

Commit 84801a6

Browse files
committed
switch to an alternate fs module to maintain testing strategy
1 parent f8b611b commit 84801a6

File tree

3 files changed

+30
-16
lines changed

3 files changed

+30
-16
lines changed

src/core/server/uuid/fs.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
import Fs from 'fs';
21+
import { promisify } from 'util';
22+
23+
export const readFile = promisify(Fs.readFile);
24+
export const writeFile = promisify(Fs.writeFile);

src/core/server/uuid/resolve_uuid.test.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,22 @@
1717
* under the License.
1818
*/
1919

20-
import Fs from 'fs';
2120
import { join } from 'path';
21+
import { readFile, writeFile } from './fs';
2222
import { resolveInstanceUuid } from './resolve_uuid';
2323
import { configServiceMock } from '../config/config_service.mock';
2424
import { loggingServiceMock } from '../logging/logging_service.mock';
2525
import { BehaviorSubject } from 'rxjs';
2626
import { Logger } from '../logging';
2727

28-
const { readFile, writeFile } = Fs;
29-
3028
jest.mock('uuid', () => ({
3129
v4: () => 'NEW_UUID',
3230
}));
3331

34-
jest.mock('fs', () => {
35-
const actual = jest.requireActual('fs');
36-
return {
37-
...actual,
38-
readFile: jest.fn().mockImplementation((...args) => process.nextTick(args.pop())),
39-
writeFile: jest.fn().mockImplementation((...args) => process.nextTick(args.pop())),
40-
};
41-
});
32+
jest.mock('./fs', () => ({
33+
readFile: jest.fn(() => Promise.resolve('')),
34+
writeFile: jest.fn(() => Promise.resolve('')),
35+
}));
4236

4337
const DEFAULT_FILE_UUID = 'FILE_UUID';
4438
const DEFAULT_CONFIG_UUID = 'CONFIG_UUID';

src/core/server/uuid/resolve_uuid.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,14 @@
1818
*/
1919

2020
import uuid from 'uuid';
21-
import Fs from 'fs';
22-
import { promisify } from 'util';
2321
import { join } from 'path';
2422
import { take } from 'rxjs/operators';
23+
import { readFile, writeFile } from './fs';
2524
import { IConfigService } from '../config';
2625
import { PathConfigType, config as pathConfigDef } from '../path';
2726
import { HttpConfigType, config as httpConfigDef } from '../http';
2827
import { Logger } from '../logging';
2928

30-
const readFile = promisify(Fs.readFile);
31-
const writeFile = promisify(Fs.writeFile);
32-
3329
const FILE_ENCODING = 'utf8';
3430
const FILE_NAME = 'uuid';
3531

0 commit comments

Comments
 (0)