Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import merge from 'lodash/merge'
import fs from 'fs-extra'
import DEFAULT_CONFIG from './config.default'

function getUserConfigFile() {
export function getUserConfigFile() {
try {
if (fs.existsSync(path.join(process.cwd(), 'cypress-image-diff.config.cjs'))) {
// eslint-disable-next-line import/no-dynamic-require, global-require
Expand All @@ -16,8 +16,7 @@ function getUserConfigFile() {
}
}

export const userConfig = merge({}, DEFAULT_CONFIG, getUserConfigFile())

export const getUserConfig = () => merge({}, DEFAULT_CONFIG, getUserConfigFile());
export class Paths {
constructor(config) {
this.rootDir = config.ROOT_DIR
Expand Down Expand Up @@ -72,4 +71,4 @@ export class Paths {
}
}

export default new Paths(userConfig)
export default new Paths(getUserConfig())
132 changes: 132 additions & 0 deletions src/getUserConfigFile.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import fs from 'fs-extra';
import path from 'path'
import { getUserConfigFile, getUserConfig} from './config';
import DEFAULT_CONFIG from './config.default'


jest.mock('fs-extra', () => ({
existsSync: jest.fn(),
}));

// eslint-disable-next-line global-require, import/no-dynamic-require
const defaultJsConfig = require(path.join(process.cwd(), 'cypress-image-diff.config'))


describe('getUserConfigFile', () => {
const mockedCustomConfig = {
CYPRESS_SCREENSHOT_OPTIONS: { disableTimersAndAnimations: false },
REPORT_DIR: 'custom-report',
ROOT_DIR: 'dir',
SCREENSHOTS_DIR: 'custom-screenshots',
};

beforeEach(() => {
jest.resetModules();
});

afterEach(() => {
jest.clearAllMocks();
});

it('should return config from cypress-image-diff.config.cjs file if exists', () => {
fs.existsSync.mockReturnValue(true);
jest.doMock(`${process.cwd()}/cypress-image-diff.config.cjs`, () => mockedCustomConfig, { virtual: true });

const config = getUserConfigFile();
expect(config).toEqual(mockedCustomConfig);
});

it('should return config from cypress-image-diff.config.js if exists, but cypress-image-diff.config.cjs does not', () => {
fs.existsSync.mockReturnValue(false);
const config = getUserConfigFile();
expect(config).toEqual(defaultJsConfig);
});

Copy link
Collaborator

@kien-ht kien-ht Dec 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be great if we could have a test case where users have partial config file. We should be able to pick up the custom config as well as the default ones. The rest looks great to me.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea! Will add soon

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kien-ht I haven't found a way to test userConfig as it was a constant before even importing it to the test. It might be just my lack of knowledge, but didn't manage to test it this way.

  • I changed userConfig to a getUserConfig function, although I am not sure if that is even a good idea. Updated in the affected places as well.

Let me know if I should undo the changes (feel free to push back) and skip userConfig/getUserConfig tests. Not sure if using it as a function would affect any behaviour 😢

it('should handle error and return empty object if require fails', () => {
fs.existsSync.mockReturnValue(true);
jest.doMock(`${process.cwd()}/cypress-image-diff.config.cjs`, () => {
throw new Error('Mocked error during require');
}, { virtual: true });

const config = getUserConfigFile();
expect(config).toEqual({});
});
});


describe('getUserConfig', () => {
const mockedCustomConfig = {
ROOT_DIR: 'other',
CYPRESS_SCREENSHOT_OPTIONS: { disableTimersAndAnimations: false },
REPORT_DIR: 'custom-report',
SCREENSHOTS_DIR: 'custom-screenshots',
FAILURE_THRESHOLD: 0.69,
RETRY_OPTIONS: {
log: true,
limit: 50,
timeout: 30000,
delay: 300,
},
FAIL_ON_MISSING_BASELINE: true,
};

beforeEach(() => {
jest.resetModules();
});

afterEach(() => {
jest.clearAllMocks();
});

it('should return the cypress-image-diff.config.cjs config merged with default config if .cjs config exists', () => {
fs.existsSync.mockReturnValue(true);
jest.doMock(`${process.cwd()}/cypress-image-diff.config.cjs`, () => mockedCustomConfig, { virtual: true });

expect(getUserConfig()).toEqual({
COMPARISON_OPTIONS: { threshold: 0.1 },
CYPRESS_SCREENSHOT_OPTIONS: mockedCustomConfig.CYPRESS_SCREENSHOT_OPTIONS,
FAILURE_THRESHOLD: mockedCustomConfig.FAILURE_THRESHOLD,
FAIL_ON_MISSING_BASELINE: mockedCustomConfig.FAIL_ON_MISSING_BASELINE,
JSON_REPORT: {
FILENAME: '',
OVERWRITE: true,
},
REPORT_DIR: mockedCustomConfig.REPORT_DIR,
RETRY_OPTIONS: {
log: true,
limit: 50,
timeout: 30000,
delay: 300,
},
ROOT_DIR: mockedCustomConfig.ROOT_DIR,
SCREENSHOTS_DIR: mockedCustomConfig.SCREENSHOTS_DIR,
});
});

it('should return the cypress-image-diff.config.js config merged with default config if cypress-image-diff.config.cjs does not exist', () => {
fs.existsSync.mockReturnValue(false);

expect(getUserConfig()).toEqual({
ROOT_DIR: '',
REPORT_DIR: 'cypress-image-diff-html-report',
SCREENSHOTS_DIR: 'cypress-image-diff-screenshots',
FAILURE_THRESHOLD: 0,
RETRY_OPTIONS: {},
FAIL_ON_MISSING_BASELINE: false,
COMPARISON_OPTIONS: { threshold: 0.1 },
JSON_REPORT: {
FILENAME: '',
OVERWRITE: true,
},
CYPRESS_SCREENSHOT_OPTIONS: { disableTimersAndAnimations: false }
});
});

it('should return the default config upon getUserConfigFile() error', () => {
jest.doMock(`${process.cwd()}/cypress-image-diff.config.js`, () => {
throw new Error('Mocked error during require');
}, { virtual: true });

expect(getUserConfig()).toEqual(DEFAULT_CONFIG);
});
});
3 changes: 2 additions & 1 deletion src/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ import {
getCleanDate,
writeFileIncrement
} from './utils'
import paths, { userConfig } from './config'
import paths, { getUserConfig } from './config'
import TestStatus from './reporter/test-status'
import { createReport } from './reporter'

let testStatuses = []
const userConfig = getUserConfig()

const setupFolders = () => {
createDir([paths.dir.baseline, paths.dir.comparison, paths.dir.diff, paths.reportDir])
Expand Down