test(cypress-github-backend): optionally record tests and run using recorded data (#2776)

This commit is contained in:
Erez Rokah
2019-10-22 19:59:13 +03:00
committed by Shawn Erquhart
parent 0f60a559c1
commit b869ce05ae
59 changed files with 57725 additions and 146 deletions

27
cypress/utils/config.js Normal file
View File

@ -0,0 +1,27 @@
const fs = require('fs-extra');
const path = require('path');
const yaml = require('js-yaml');
const devTestDirectory = path.join(__dirname, '..', '..', 'dev-test');
const backendsDirectory = path.join(devTestDirectory, 'backends');
async function copyBackendFiles(backend) {
await Promise.all(
['config.yml', 'index.html'].map(file => {
return fs.copyFile(
path.join(backendsDirectory, backend, file),
path.join(devTestDirectory, file),
);
}),
);
}
async function updateConfig(configModifier) {
const configFile = path.join(devTestDirectory, 'config.yml');
const configContent = await fs.readFile(configFile);
const config = yaml.safeLoad(configContent);
await configModifier(config);
await fs.writeFileSync(configFile, yaml.safeDump(config));
}
module.exports = { copyBackendFiles, updateConfig };

View File

@ -0,0 +1,49 @@
const { mockServerClient } = require('mockserver-client');
const mockserver = require('mockserver-node');
const PROXY_PORT = 1080;
const PROXY_HOST = 'localhost';
const start = () =>
mockserver.start_mockserver({
serverPort: PROXY_PORT,
});
const stop = () =>
mockserver.stop_mockserver({
serverPort: PROXY_PORT,
});
const retrieveRecordedExpectations = async () => {
const promise = new Promise((resolve, reject) => {
mockServerClient(PROXY_HOST, PROXY_PORT)
.retrieveRecordedExpectations({})
.then(resolve, reject);
});
let recorded = await promise;
recorded = recorded.filter(({ httpRequest }) => {
const { Host = [] } = httpRequest.headers;
return Host.includes('api.github.com');
});
return recorded;
};
const resetMockServerState = async () => {
const promise = new Promise((resolve, reject) => {
mockServerClient(PROXY_HOST, PROXY_PORT)
.reset()
.then(resolve, reject);
});
await promise;
};
module.exports = {
start,
stop,
resetMockServerState,
retrieveRecordedExpectations,
};

5
cypress/utils/regexp.js Normal file
View File

@ -0,0 +1,5 @@
const escapeRegExp = string => {
return string.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&');
};
module.exports = { escapeRegExp };

View File

@ -5,6 +5,8 @@ function login(user) {
if (user) {
cy.visit('/', {
onBeforeLoad: () => {
// https://github.com/cypress-io/cypress/issues/1208
window.indexedDB.deleteDatabase('localforage');
window.localStorage.setItem('netlify-cms-user', JSON.stringify(user));
},
});
@ -12,6 +14,7 @@ function login(user) {
cy.visit('/');
cy.contains('button', 'Login').click();
}
cy.contains('a', 'New Post');
}
function assertNotification(message) {
@ -170,11 +173,22 @@ function populateEntry(entry) {
}
}
cy.get('input')
.first()
.click();
cy.contains('button', 'Save').click();
assertNotification(notifications.saved);
cy.clock().then(clock => {
// some input fields are de-bounced thus require advancing the clock
if (clock) {
// https://github.com/cypress-io/cypress/issues/1273
clock.tick(150);
clock.tick(150);
// eslint-disable-next-line cypress/no-unnecessary-waiting
cy.wait(500);
}
cy.get('input')
.first()
.click();
cy.contains('button', 'Save').click();
assertNotification(notifications.saved);
});
}
function createPost(entry) {