diff --git a/lib/constants.js b/lib/constants.js index 4608a04..60d211e 100644 --- a/lib/constants.js +++ b/lib/constants.js @@ -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, }; diff --git a/lib/utils.js b/lib/utils.js index c1abfeb..7dad605 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -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; @@ -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) || []), [], ); @@ -279,4 +291,7 @@ module.exports = { getHookStartObject, getTotalSpecs, getConfig, + getExcludeSpecPattern, + getFixtureFolderPattern, + getSpecPattern, }; diff --git a/test/utils.test.js b/test/utils.test.js index 033dde1..5ed35e9 100644 --- a/test/utils.test.js +++ b/test/utils.test.js @@ -17,6 +17,9 @@ const { getCodeRef, getTotalSpecs, getConfig, + getFixtureFolderPattern, + getExcludeSpecPattern, + getSpecPattern, } = require('./../lib/utils'); const pjson = require('./../package.json'); @@ -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': '', }, }); }); @@ -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), + ); }); }); });