Skip to content

Commit

Permalink
Reduce code duplication while mocking in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-knozderko committed Oct 10, 2023
1 parent e6e3ca1 commit 6144e84
Showing 1 changed file with 31 additions and 78 deletions.
109 changes: 31 additions & 78 deletions test/unit/configuration/configuration_finding_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,8 @@ describe('Configuration finding tests', function () {
.mockFile(configInDriverDirectory, 'random content')
.mockFile(configInHomeDirectory, 'random content')
.mockFile(configInTempDirectory, 'random content');
mock('fs/promises', {
access: async function (path) {
return fsMock.access(path);
},
readFile: async function (path){
return fsMock.readFile(path);
}
});
mock('process', {
env: {
SF_CLIENT_CONFIG_FILE: configFromEnvVariable
}
});
mockFiles(fsMock);
mockClientConfigFileEnvVariable(configFromEnvVariable);
const fsPromises = require('fs/promises');
const process = require('process');
const configUtil = new ConfigurationUtil(fsPromises, process);
Expand All @@ -68,19 +57,8 @@ describe('Configuration finding tests', function () {
.mockFile(configInDriverDirectory, 'random content')
.mockFile(configInHomeDirectory, 'random content')
.mockFile(configInTempDirectory, 'random content');
mock('fs/promises', {
access: async function (path) {
return fsMock.access(path);
},
readFile: async function (path){
return fsMock.readFile(path);
}
});
mock('process', {
env: {
SF_CLIENT_CONFIG_FILE: configFromEnvVariable
}
});
mockFiles(fsMock);
mockClientConfigFileEnvVariable(configFromEnvVariable);
const fsPromises = require('fs/promises');
const process = require('process');
const configUtil = new ConfigurationUtil(fsPromises, process);
Expand All @@ -98,19 +76,8 @@ describe('Configuration finding tests', function () {
.mockFile(configInDriverDirectory, fileContent)
.mockFile(configInHomeDirectory, 'random content')
.mockFile(configInTempDirectory, 'random content');
mock('fs/promises', {
access: async function (path) {
return fsMock.access(path);
},
readFile: async function (path){
return fsMock.readFile(path);
}
});
mock('process', {
env: {
SF_CLIENT_CONFIG_FILE: undefined
}
});
mockFiles(fsMock);
mockClientConfigFileEnvVariable(undefined);
const fsPromises = require('fs/promises');
const process = require('process');
const configUtil = new ConfigurationUtil(fsPromises, process);
Expand All @@ -127,19 +94,8 @@ describe('Configuration finding tests', function () {
const fsMock = new FsMock()
.mockFile(configInHomeDirectory, fileContent)
.mockFile(configInTempDirectory, 'random content');
mock('fs/promises', {
access: async function (path) {
return fsMock.access(path);
},
readFile: async function (path){
return fsMock.readFile(path);
}
});
mock('process', {
env: {
SF_CLIENT_CONFIG_FILE: undefined
}
});
mockFiles(fsMock);
mockClientConfigFileEnvVariable(undefined);
const fsPromises = require('fs/promises');
const process = require('process');
const configUtil = new ConfigurationUtil(fsPromises, process);
Expand All @@ -155,19 +111,8 @@ describe('Configuration finding tests', function () {
// given
const fsMock = new FsMock()
.mockFile(configInTempDirectory, fileContent);
mock('fs/promises', {
access: async function (path) {
return fsMock.access(path);
},
readFile: async function (path){
return fsMock.readFile(path);
}
});
mock('process', {
env: {
SF_CLIENT_CONFIG_FILE: undefined
}
});
mockFiles(fsMock);
mockClientConfigFileEnvVariable(undefined);
const fsPromises = require('fs/promises');
const process = require('process');
const configUtil = new ConfigurationUtil(fsPromises, process);
Expand All @@ -182,19 +127,8 @@ describe('Configuration finding tests', function () {
it('should return null if config could not be found', async function () {
// given
const fsMock = new FsMock();
mock('fs/promises', {
access: async function (path) {
return fsMock.access(path);
},
readFile: async function (path){
return fsMock.readFile(path);
}
});
mock('process', {
env: {
SF_CLIENT_CONFIG_FILE: undefined
}
});
mockFiles(fsMock);
mockClientConfigFileEnvVariable(undefined);
const fsPromises = require('fs/promises');
const process = require('process');
const configUtil = new ConfigurationUtil(fsPromises, process);
Expand All @@ -207,6 +141,25 @@ describe('Configuration finding tests', function () {
});
});

function mockFiles(fsMock) {
mock('fs/promises', {
access: async function (path) {
return fsMock.access(path);
},
readFile: async function (path){
return fsMock.readFile(path);
}
});
}

function mockClientConfigFileEnvVariable(envClientConfigFileValue) {
mock('process', {
env: {
SF_CLIENT_CONFIG_FILE: envClientConfigFileValue
}
});
}

class FsMock {
existingFiles = new Map();

Expand Down

0 comments on commit 6144e84

Please sign in to comment.