Skip to content

Commit

Permalink
Breaking snapshot functionality when running all tests, previous fix …
Browse files Browse the repository at this point in the history
…breaks other functionality (Reopens #10) (Fixes #14)
  • Loading branch information
Meinaart van Straalen committed Jan 11, 2019
1 parent fbefa8f commit f3fbfca
Show file tree
Hide file tree
Showing 12 changed files with 337 additions and 2,211 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning 2.0.0](https://semver.org/spec/v2.0.0.html).

## Releases
### [1.2.5](https://github.com/meinaart/cypress-plugin-snapshots/compare/v1.2.4...v1.2.5) - 2019-01-11
- Breaking snapshot functionality when running all tests, previous fix breaks other functionality (Reopens #10) (Fixes #14)

### [1.2.4](https://github.com/meinaart/cypress-plugin-snapshots/compare/v1.2.3...v1.2.4) - 2019-01-04
- Fix broken `excludedFields` functionality

Expand Down
2 changes: 1 addition & 1 deletion __tests__/plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ describe('plugin', () => {
const { initPlugin } = require('../plugin');

initPlugin(on, globalConfig);
expect(on).toBeCalledTimes(3);
expect(on).toBeCalledTimes(2);
});
});
2,422 changes: 313 additions & 2,109 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
"test": "jest & npm --prefix cypress run cy:run"
},
"dependencies": {
"@cypress/browserify-preprocessor": "^1.1.2",
"bufferutil": "^4.0.0",
"chalk": "^2.4.1",
"diff2html": "^2.5.0",
Expand Down
2 changes: 0 additions & 2 deletions plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
const { initConfig, CONFIG_KEY } = require('./src/config');
const initServer = require('./src/server/initServer');
const tasks = require('./src/tasks/');
const initSetSpecInServer = require('./src/plugin/initSetSpecInServer');

/**
* Initializes the plugin:
Expand All @@ -15,7 +14,6 @@ function initPlugin(on, globalConfig = {
}) {
const config = initConfig(globalConfig.env[CONFIG_KEY]);
initServer(config);
initSetSpecInServer(on, config);

// Adding sub objects/keys to `Cypress.env` that don't exist in `cypress.json` doesn't work.
// That's why the config is stringified and parsed again in `src/utils/commands/getConfig.js#fixConfig`.
Expand Down
42 changes: 0 additions & 42 deletions src/plugin/initSetSpecInServer.js

This file was deleted.

2 changes: 0 additions & 2 deletions src/server/actions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
module.exports = {
GET_SPEC: 'cypress-plugin-snapshot:getSpec',
SAVE_IMAGE: 'cypress-plugin-snapshot:saveImage',
SAVE_TEXT: 'cypress-plugin-snapshot:saveText',
SET_SPEC: 'cypress-plugin-snapshot:setSpec',
};
12 changes: 0 additions & 12 deletions src/server/getSpec.js

This file was deleted.

15 changes: 1 addition & 14 deletions src/server/initServer.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
const http = require('http');
const socketio = require('socket.io');
const { GET_SPEC, SET_SPEC, SAVE_TEXT, SAVE_IMAGE } = require('./actions');
const { SAVE_TEXT, SAVE_IMAGE } = require('./actions');
const { saveSnapshot } = require('../utils/tasks/textSnapshots');
const { saveImageSnapshot } = require('../utils/tasks/imageSnapshots');

function initServer(config) {
const server = http.createServer();
const io = socketio(server);
let spec = {};

io.on('connection', (client) => {
const { token } = client.handshake.query;
Expand All @@ -25,18 +24,6 @@ function initServer(config) {
}
});
}

client.on(GET_SPEC, (fn) => {
if (token === config.token) {
fn(spec);
}
});

client.on(SET_SPEC, (newSpec) => {
if (token === config.token) {
spec = newSpec;
}
})
});

server.listen(config.serverPort, config.serverHost);
Expand Down
9 changes: 0 additions & 9 deletions src/server/setSpec.js

This file was deleted.

33 changes: 18 additions & 15 deletions src/utils/commands/cleanupSnapshots.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { formatNormalizedJson, } = require('../../utils/json');
const getTextSnapshotFilename = require('../text/getSnapshotFilename');
const { snapshotTitleIsUsed } = require('../../utils/snapshotTitles');
const getConfig = require('./getConfig');
const getSpec = require('./getSpec');
const { NO_LOG } = require('../../constants');

// Removes unused snapshots from snapshot file
Expand All @@ -13,23 +14,25 @@ function cleanUpSnapshots() {
return;
}

const filename = getTextSnapshotFilename(Cypress.spec.relative);
cy.readFile(filename, NO_LOG).then((content) => {
if (content) {
const snapshot = JSON.parse(content);
const keys = Object.keys(snapshot);
getSpec().then((spec) => {
const filename = getTextSnapshotFilename(spec.relative);
cy.readFile(filename, NO_LOG).then((content) => {
if (content) {
const snapshot = JSON.parse(content);
const keys = Object.keys(snapshot);

const cleanSnapshot = keys
.filter(snapshotTitleIsUsed)
.reduce((result, key) => {
result[key] = snapshot[key];
return result;
}, {});
const cleanSnapshot = keys
.filter(snapshotTitleIsUsed)
.reduce((result, key) => {
result[key] = snapshot[key];
return result;
}, {});

cy.writeFile(filename,
formatNormalizedJson(cleanSnapshot),
NO_LOG);
}
cy.writeFile(filename,
formatNormalizedJson(cleanSnapshot),
NO_LOG);
}
});
});
}

Expand Down
5 changes: 1 addition & 4 deletions src/utils/commands/getSpec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
const getSpecFromServer = require('../../server/getSpec');
const getConfig = require('./getConfig');

async function getSpec() {
if (Cypress.spec.absolute === '__all') {
return getSpecFromServer(getConfig());
throw new Error(`cypress-plugin-snapshots does not work when running all tests, this will be fixed once this bug is resolved: https://github.com/cypress-io/cypress/issues/3090`);
}

return Promise.resolve(Cypress.spec);
Expand Down

0 comments on commit f3fbfca

Please sign in to comment.