Skip to content

Commit

Permalink
Find filename for current spec
Browse files Browse the repository at this point in the history
When running all specs, the mocks are recorded in a new file called "All Specs". This tries to find the filename based on the currentTest invocation details.

Remove unused const

cleanup: store fixtures subfolder name
  • Loading branch information
mhssmnn committed Aug 22, 2021
1 parent 6b43ccc commit fe03f68
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,8 @@ const blacklistRoutes = cypressConfig.blacklistRoutes || [];
const whitelistHeaders = cypressConfig.whitelistHeaders || [];
const supportedMethods = ['get', 'GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD'];

const fileName = path.basename(
Cypress.spec.name,
path.extname(Cypress.spec.name),
);
// The replace fixes Windows path handling
const fixturesFolder = Cypress.config('fixturesFolder').replace(/\\/g, '/');
const fixturesFolderSubDirectory = fileName.replace(/\./, '-');
const mocksFolder = path.join(fixturesFolder, '../mocks');

before(function() {
Expand Down Expand Up @@ -55,12 +50,16 @@ module.exports = function autoRecord() {

before(function() {
// Get mock data that relates to this spec file
const fileName = getFileName(this.currentTest);
cy.task('readFile', path.join(mocksFolder, `${fileName}.json`)).then((data) => {
routesByTestId = data === null ? {} : data;
});
});

beforeEach(function() {
const fileName = getFileName(this.currentTest);
const fixturesFolderSubDirectory = getFixturesSubFolder(fileName);

// Reset routes before each test case
routes = [];

Expand Down Expand Up @@ -189,6 +188,9 @@ module.exports = function autoRecord() {
});

afterEach(function() {
const fileName = getFileName(this.currentTest);
const fixturesFolderSubDirectory = getFixturesSubFolder(fileName);

// Check to see if the current test already has mock data or if forceRecord is on
if (
(!routesByTestId[this.currentTest.title]
Expand Down Expand Up @@ -244,6 +246,9 @@ module.exports = function autoRecord() {
});

after(function() {
const fileName = getFileName(this.currentTest);
const fixturesFolderSubDirectory = getFixturesSubFolder(fileName);

// Transfer used mock data to new object to be stored locally
if (isCleanMocks) {
Object.keys(routesByTestId).forEach((testName) => {
Expand All @@ -266,3 +271,14 @@ module.exports = function autoRecord() {
});
});
};

function getFileName(currentTest) {
return path.basename(
currentTest.invocationDetails.relativeFile,
path.extname(currentTest.invocationDetails.relativeFile),
);
}

function getFixturesSubFolder(mockFilename) {
mockFilename.replace(/\./, '-');
}

0 comments on commit fe03f68

Please sign in to comment.