Skip to content

Commit

Permalink
Reflect cypress 10 changes for getTotalSpecs (#126)
Browse files Browse the repository at this point in the history
Fixes #116.

Co-authored-by: dwentland24 <daniel.wentland@digistore24.team>
  • Loading branch information
orgads and dwentland24 committed Sep 20, 2022
1 parent 46eb7ed commit 0863215
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 51 deletions.
9 changes: 0 additions & 9 deletions lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,10 @@ const reporterEvents = {
SET_LAUNCH_STATUS: 'setLaunchStatus',
};

const DEFAULT_SPEC_CONFIG = {
ignoreTestFiles: '*.hot-update.js',
testFiles: '**/*.*',
integrationFolder: 'cypress/integration',
fixturesFolder: 'cypress/fixtures',
supportFile: 'cypress/support',
};

module.exports = {
testItemStatuses,
logLevels,
entityType,
hookTypesMap,
reporterEvents,
DEFAULT_SPEC_CONFIG,
};
69 changes: 42 additions & 27 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const fs = require('fs');
const glob = require('glob');
const path = require('path');
const minimatch = require('minimatch');
const { entityType, hookTypesMap, testItemStatuses, DEFAULT_SPEC_CONFIG } = require('./constants');
const { entityType, hookTypesMap, testItemStatuses } = require('./constants');
const pjson = require('./../package.json');

const { FAILED, PASSED, SKIPPED } = testItemStatuses;
Expand Down Expand Up @@ -217,44 +217,56 @@ const getHookStartObject = (hook) => {
codeRef: hook.codeRef,
};
};
const getFixtureFolderPattern = (config) => {
return [].concat(config.fixturesFolder ? path.join(config.fixturesFolder, '**', '*') : []);
};

const getTotalSpecs = ({
ignoreTestFiles,
testFiles,
integrationFolder,
fixturesFolder,
supportFile,
}) => {
const specConfig = Object.assign(
{},
DEFAULT_SPEC_CONFIG,
ignoreTestFiles && { ignoreTestFiles },
testFiles && { testFiles },
integrationFolder && { integrationFolder },
fixturesFolder && { fixturesFolder },
supportFile && { supportFile },
);
const getExcludeSpecPattern = (config) => {
// Return cypress >= 10 pattern.
if (config.excludeSpecPattern) {
const excludePattern = Array.isArray(config.excludeSpecPattern)
? config.excludeSpecPattern
: [config.excludeSpecPattern];
return [...excludePattern];
}

// Return cypress <= 9 pattern
const ignoreTestFilesPattern = Array.isArray(config.ignoreTestFiles)
? config.ignoreTestFiles
: [config.ignoreTestFiles] || [];

return [...ignoreTestFilesPattern];
};

const fixturesFolderPath = path.join(specConfig.fixturesFolder, '**', '*');
const getSpecPattern = (config) => {
if (config.specPattern) return [].concat(config.specPattern);

const supportFilePath = specConfig.supportFile || [];
return Array.isArray(config.testFiles)
? config.testFiles.map((file) => path.join(config.integrationFolder, file))
: [].concat(path.join(config.integrationFolder, config.testFiles));
};

const getTotalSpecs = (config) => {
if (!config.testFiles && !config.specPattern)
throw new Error('Configuration property not set! Neither for cypress <= 9 nor cypress >= 10');

const specPattern = getSpecPattern(config);

const excludeSpecPattern = getExcludeSpecPattern(config);

const options = {
sort: true,
absolute: true,
nodir: true,
cwd: specConfig.integrationFolder,
ignore: [supportFilePath, fixturesFolderPath],
ignore: [config.supportFile, path.join(config.fixturesFolder, '**', '*')],
};

const ignorePatterns = [].concat(specConfig.ignoreTestFiles);

const doesNotMatchAllIgnoredPatterns = (file) =>
ignorePatterns.every((pattern) => !minimatch(file, pattern, { dot: true, matchBase: true }));

const testFilesPatterns = [].concat(specConfig.testFiles);
excludeSpecPattern.every(
(pattern) => !minimatch(file, pattern, { dot: true, matchBase: true }),
);

const globResult = testFilesPatterns.reduce(
const globResult = specPattern.reduce(
(files, pattern) => files.concat(glob.sync(pattern, options) || []),
[],
);
Expand All @@ -279,4 +291,7 @@ module.exports = {
getHookStartObject,
getTotalSpecs,
getConfig,
getExcludeSpecPattern,
getFixtureFolderPattern,
getSpecPattern,
};
151 changes: 136 additions & 15 deletions test/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ const {
getCodeRef,
getTotalSpecs,
getConfig,
getFixtureFolderPattern,
getExcludeSpecPattern,
getSpecPattern,
} = require('./../lib/utils');
const pjson = require('./../package.json');

Expand Down Expand Up @@ -755,14 +758,19 @@ describe('utils script', () => {
describe('getTotalSpecs', () => {
beforeEach(() => {
mock({
'example/tests': {
'cypress/tests': {
'example1.spec.js': '',
'example2.spec.js': '',
'example3.spec.js': '',
'example.ignore.js': '',
'example4.spec.ts': '',
'example.ignore.spec.js': '',
},
'example/support': {
'support.js': '',
'cypress/support': {
'index.js': '',
},
'cypress/fixtures': {
'fixtures1.js': '',
'fixtures2.js': '',
},
});
});
Expand All @@ -772,28 +780,141 @@ describe('utils script', () => {
});

it('testFiles, integrationFolder, supportFile are specified: should count all files from integration folder', () => {
const specConfig = {
let specConfig = {
testFiles: '**/*.*',
integrationFolder: 'example/tests',
supportFile: 'example/support',
ignoreTestFiles: '*.hot-update.js',
fixturesFolder: 'cypress/fixtures',
integrationFolder: 'cypress/tests',
supportFile: 'cypress/support/index.js',
};

const specCount = getTotalSpecs(specConfig);
let specCount = getTotalSpecs(specConfig);

expect(specCount).toEqual(4);
expect(specCount).toEqual(5);

specConfig = {
excludeSpecPattern: '*.hot-update.js',
specPattern: 'cypress/tests/**/*.spec.{js,ts}',
supportFile: 'cypress/support/index.js',
fixturesFolder: 'cypress/fixtures',
};

specCount = getTotalSpecs(specConfig);

expect(specCount).toEqual(5);
});

it('ignoreTestFiles are specified: should ignore specified files', () => {
const specConfig = {
ignoreTestFiles: '*.ignore.js',
let specConfig = {
testFiles: '**/*.*',
integrationFolder: 'example/tests',
supportFile: 'example/support',
ignoreTestFiles: ['*.hot-update.js', '*.ignore.*.*'],
fixturesFolder: 'cypress/fixtures',
integrationFolder: 'cypress/tests',
supportFile: 'cypress/support/index.js',
};

let specCount = getTotalSpecs(specConfig);

expect(specCount).toEqual(4);

specConfig = {
specPattern: 'cypress/tests/**/*.spec.{js,ts}',
excludeSpecPattern: ['*.hot-update.js', '*.ignore.spec.*'],
supportFile: 'cypress/support/index.js',
fixturesFolder: 'cypress/fixtures',
};

specCount = getTotalSpecs(specConfig);

expect(specCount).toEqual(4);
});
});

describe('getFixtureFolderPattern', () => {
it('returns a glob pattern forn fixtures folder', () => {
const specConfig = { fixturesFolder: 'cypress/fixtures' };

const specArray = getFixtureFolderPattern(specConfig);
expect(specArray).toHaveLength(1);
expect(specArray).toContain('cypress/fixtures/**/*');
});
});
describe('getExcludeSpecPattern', () => {
it('getExcludeSpecPattern returns required pattern for cypress version <= 9', () => {
const specConfigString = {
integrationFolder: 'cypress/integration',
ignoreTestFiles: '*.hot-update.js',
fixturesFolder: 'cypress/fixtures',
supportFile: 'cypress/support/index.js',
};

const specCount = getTotalSpecs(specConfig);
const specConfigArray = {
integrationFolder: 'cypress/integration',
ignoreTestFiles: ['*.hot-update.js', '*.hot-update.ts'],
fixturesFolder: 'cypress/fixtures',
supportFile: 'cypress/support/index.js',
};

let patternArray = getExcludeSpecPattern(specConfigString);
expect(patternArray).toHaveLength(1);
expect(patternArray).toContain('*.hot-update.js');

patternArray = getExcludeSpecPattern(specConfigArray);
expect(patternArray).toHaveLength(2);
expect(patternArray).toContain('*.hot-update.js');
expect(patternArray).toContain('*.hot-update.ts');
});
});

describe('getSpecPattern', () => {
it('returns the required glob pattern for cypress <=9 config when testFiles is an array', () => {
const specConfig = {
integrationFolder: 'cypress/integration',
testFiles: ['**/*.js', '**/*.ts'],
};

const patternArray = getSpecPattern(specConfig);
expect(patternArray).toHaveLength(2);
expect(patternArray[0]).toEqual(
path.join(specConfig.integrationFolder, specConfig.testFiles[0]),
);
expect(patternArray[1]).toEqual(
path.join(specConfig.integrationFolder, specConfig.testFiles[1]),
);
});

it('getSpecPattern returns the required glob pattern for cypress >= 10 config when specPattern is an array', () => {
const specConfig = {
specPattern: ['cypress/integration/**/*.js', 'cypress/integration/**/*.js'],
};

const patternArray = getSpecPattern(specConfig);
expect(patternArray).toHaveLength(2);
expect(patternArray[0]).toEqual(specConfig.specPattern[0]);
expect(patternArray[1]).toEqual(specConfig.specPattern[1]);
});

it('getSpecPattern returns the required glob pattern for cypress >= 10 config when specPattern is a string', () => {
const specConfig = {
specPattern: 'cypress/integration/**/*.js',
};

const patternArray = getSpecPattern(specConfig);
expect(patternArray).toHaveLength(1);
expect(patternArray[0]).toEqual(specConfig.specPattern);
});

it('getSpecPattern returns the required glob pattern for cypress <= 9 config when testFiles is a string', () => {
const specConfig = {
integrationFolder: 'cypress/integration',
testFiles: '**/*.js',
};

expect(specCount).toEqual(3);
const patternArray = getSpecPattern(specConfig);
expect(patternArray).toHaveLength(1);
expect(patternArray[0]).toEqual(
path.join(specConfig.integrationFolder, specConfig.testFiles),
);
});
});
});

0 comments on commit 0863215

Please sign in to comment.