44// and test only the local mode here.
55
66const fs = jest . requireActual ( "fs" ) ;
7+ const mockReadFile = jest . fn ( ) ;
8+ const mockAccess = jest . fn ( ( cb ) => cb ( ) ) ;
79jest . mock ( "fs" , ( ) => ( {
8- readFile : jest . fn ( ) ,
9- access : jest . fn ( ( cb ) => cb ( ) ) ,
10+ readFile : mockReadFile ,
11+ access : mockAccess ,
1012} ) ) ;
1113
1214const { stateFromInfo } = require ( "../__common__/from-info" ) ;
@@ -42,8 +44,7 @@ describe("local integration test", () => {
4244
4345 describe ( "init" , ( ) => {
4446 test ( "init fails resolving for bad config paths" , async ( ) => {
45- const { readFile : readFileSpy } = require ( "fs" ) ;
46- readFileSpy . mockImplementationOnce ( fs . readFile ) ;
47+ mockReadFile . mockImplementationOnce ( fs . readFile ) ;
4748 await expect ( toggles . initializeFeatures ( { configFile : "fantasy_name" } ) ) . rejects . toMatchInlineSnapshot (
4849 `[FeatureTogglesError: initialization aborted, could not read config file: ENOENT: no such file or directory, open 'fantasy_name']`
4950 ) ;
@@ -57,8 +58,6 @@ describe("local integration test", () => {
5758 } ) ;
5859
5960 test ( "init config precedence" , async ( ) => {
60- const { readFile : readFileSpy } = require ( "fs" ) ;
61-
6261 const configForRuntime = {
6362 [ FEATURE . A ] : {
6463 fallbackValue : "fallbackRuntimeA" ,
@@ -97,7 +96,7 @@ describe("local integration test", () => {
9796 type : "string" ,
9897 } ,
9998 } ;
100- readFileSpy . mockImplementationOnce ( ( filepath , callback ) =>
99+ mockReadFile . mockImplementationOnce ( ( filepath , callback ) =>
101100 callback ( null , Buffer . from ( JSON . stringify ( configForFile ) ) )
102101 ) ;
103102
@@ -232,8 +231,6 @@ describe("local integration test", () => {
232231 test ( "custom module validations just module from CONFIG_DIR" , async ( ) => {
233232 jest . mock ( "./virtual-validator-just-module" , ( ) => jest . fn ( ) , { virtual : true } ) ;
234233 const mockValidator = require ( "./virtual-validator-just-module" ) ;
235- const { readFile : readFileSpy } = require ( "fs" ) ;
236-
237234 const config = {
238235 [ FEATURE . A ] : {
239236 fallbackValue : "fallback" ,
@@ -242,7 +239,7 @@ describe("local integration test", () => {
242239 } ,
243240 } ;
244241 const configBuffer = Buffer . from ( JSON . stringify ( config ) ) ;
245- readFileSpy . mockImplementationOnce ( ( filepath , callback ) => callback ( null , configBuffer ) ) ;
242+ mockReadFile . mockImplementationOnce ( ( filepath , callback ) => callback ( null , configBuffer ) ) ;
246243 await toggles . initializeFeatures ( {
247244 configFile : "./test/integration-local/virtual-config.json" ,
248245 } ) ;
@@ -254,7 +251,6 @@ describe("local integration test", () => {
254251 test ( "custom module validations with call from CONFIG_DIR" , async ( ) => {
255252 jest . mock ( "./virtual-validator-with-call" , ( ) => ( { validator : jest . fn ( ) } ) , { virtual : true } ) ;
256253 const { validator : mockValidator } = require ( "./virtual-validator-with-call" ) ;
257- const { readFile : readFileSpy } = require ( "fs" ) ;
258254
259255 const config = {
260256 [ FEATURE . A ] : {
@@ -264,7 +260,7 @@ describe("local integration test", () => {
264260 } ,
265261 } ;
266262 const configBuffer = Buffer . from ( JSON . stringify ( config ) ) ;
267- readFileSpy . mockImplementationOnce ( ( filepath , callback ) => callback ( null , configBuffer ) ) ;
263+ mockReadFile . mockImplementationOnce ( ( filepath , callback ) => callback ( null , configBuffer ) ) ;
268264 await toggles . initializeFeatures ( {
269265 configFile : "./test/integration-local/virtual-config.json" ,
270266 } ) ;
0 commit comments