-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
148 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ node_modules/ | |
npm-debug.log | ||
src/.snap-shot/ | ||
.snap-shot | ||
snap-shot.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
'use strict' | ||
|
||
const la = require('lazy-ass') | ||
const is = require('check-more-types') | ||
const callsites = require('callsites') | ||
|
||
// storage adapter for Cypress E2E testing tool | ||
|
||
/* global cy, expect, fetch */ | ||
const filename = 'snap-shot.json' | ||
|
||
let snapshots | ||
|
||
function loadSnapshots () { | ||
return snapshots | ||
} | ||
|
||
function saveSnapshots (snapshots) { | ||
const text = JSON.stringify(snapshots, null, 2) + '\n' | ||
cy.writeFile(filename, text) | ||
} | ||
|
||
function init () { | ||
// find out the source for all test -> this spec file | ||
const sites = callsites() | ||
la(sites.length, 'missing callsite') | ||
const specFileUrl = sites[0].getFileName() | ||
la(is.webUrl(specFileUrl), 'missing spec url', specFileUrl) | ||
console.log('loading spec from', specFileUrl) | ||
|
||
// specFileUrl is something like | ||
// http://localhost:49829/__cypress/tests?p=cypress/integration/spec.js-438 | ||
// we will need to get "true" filename which in this case should be | ||
// cypress/integration/spec.js | ||
const pIndex = specFileUrl.indexOf('?p=') | ||
const dotJsIndex = specFileUrl.indexOf('.js-', pIndex) | ||
const specFile = specFileUrl.substr(pIndex + 3, dotJsIndex - pIndex) | ||
console.log('specFile is "%s"', specFile) | ||
|
||
// ignore arguments for now | ||
api.fromCurrentFolder = () => specFile | ||
|
||
// cache the fetched source, otherwise every test fetches it | ||
const shouldFetch = api.readFileSync === dummyReadFileSync | ||
if (shouldFetch) { | ||
return fetch(specFileUrl).then(r => r.text()) | ||
.then(source => { | ||
// ignores filename for now | ||
api.readFileSync = () => source | ||
}) | ||
.then(() => { | ||
return fetch('/__cypress/tests?p=' + filename) | ||
.then(r => r.text()) | ||
.then(text => { | ||
if (text.includes('BUNDLE_ERROR')) { | ||
return Promise.reject(new Error('not found')) | ||
} | ||
cy.log('loaded snapshots', filename) | ||
// the JSON is wrapped in webpack wrapper ;) | ||
const req = eval(text) // eslint-disable-line no-eval | ||
snapshots = req('1') | ||
}) | ||
.catch(err => { | ||
console.error(err) | ||
snapshots = {} | ||
}) | ||
}) | ||
} else { | ||
return Promise.resolve() | ||
} | ||
} | ||
|
||
function raiseIfDifferent ({value, expected}) { | ||
cy.then(() => { | ||
expect(value).to.equal(expected) | ||
}) | ||
} | ||
|
||
function dummyReadFileSync () { | ||
throw new Error(`In the browser, please call snapshot.init() | ||
before calling tests, like this: | ||
const snapshot = require('snap-shot') | ||
beforeEach(snapshot.init) | ||
`) | ||
} | ||
|
||
// TODO replace exposed API with error methods that wait | ||
// until "init" is called | ||
const api = { | ||
loadSnapshots, | ||
saveSnapshots, | ||
init, | ||
readFileSync: dummyReadFileSync, | ||
raiseIfDifferent | ||
} | ||
module.exports = api |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters