1 |
|
2 |
|
3 | const la = require('lazy-ass')
|
4 | const is = require('check-more-types')
|
5 |
|
6 | // storage adapter for Cypress E2E testing tool
|
7 |
|
8 | /* global cy, expect, fetch */
|
9 | la(is.fn(fetch), 'missing fetch')
|
10 | const filename = 'snap-shot.json'
|
11 |
|
12 | let snapshots
|
13 |
|
14 | function loadSnapshots () {
|
15 | return snapshots
|
16 | }
|
17 |
|
18 | function saveSnapshots (snapshots) {
|
19 | const text = JSON.stringify(snapshots, null, 2) + '\n'
|
20 | cy.writeFile(filename, text)
|
21 | }
|
22 |
|
23 | function init () {
|
24 | // for now disable
|
25 | return Promise.resolve()
|
26 | // // find out the source for all test -> this spec file
|
27 | // const sites = callsites()
|
28 | // la(sites.length, 'missing callsite')
|
29 | // const specFileUrl = sites[1].filename
|
30 | // la(is.webUrl(specFileUrl), 'missing spec url', specFileUrl)
|
31 | // console.log('loading spec from', specFileUrl)
|
32 |
|
33 | // // specFileUrl is something like
|
34 | // // http://localhost:49829/__cypress/tests?p=cypress/integration/spec.js-438
|
35 | // // we will need to get "true" filename which in this case should be
|
36 | // // cypress/integration/spec.js
|
37 | // const pIndex = specFileUrl.indexOf('?p=')
|
38 | // const dotJsIndex = specFileUrl.indexOf('.js-', pIndex)
|
39 | // const specFile = specFileUrl.substr(pIndex + 3, dotJsIndex - pIndex)
|
40 | // console.log('specFile is "%s"', specFile)
|
41 |
|
42 | // // ignore arguments for now
|
43 | // api.fromCurrentFolder = () => specFile
|
44 |
|
45 | // // cache the fetched source, otherwise every test fetches it
|
46 | // const shouldFetch = api.readFileSync === dummyReadFileSync
|
47 | // if (shouldFetch) {
|
48 | // return fetch(specFileUrl).then(r => r.text())
|
49 | // .then(source => {
|
50 | // // ignores filename for now
|
51 | // api.readFileSync = () => source
|
52 | // })
|
53 | // .then(() => {
|
54 | // return fetch('/__cypress/tests?p=./' + filename)
|
55 | // .then(r => r.text())
|
56 | // .then(function loadedText (text) {
|
57 | // if (text.includes('BUNDLE_ERROR')) {
|
58 | // return Promise.reject(new Error('not found'))
|
59 | // }
|
60 | // cy.log('loaded snapshots', filename)
|
61 | // // the JSON is wrapped in webpack wrapper ;)
|
62 | // const req = eval(text) // eslint-disable-line no-eval
|
63 | // snapshots = req('1')
|
64 | // })
|
65 | // .catch(err => {
|
66 | // console.error(err)
|
67 | // snapshots = {}
|
68 | // })
|
69 | // })
|
70 | // } else {
|
71 | // return Promise.resolve()
|
72 | // }
|
73 | }
|
74 |
|
75 | function raiseIfDifferent ({ value, expected }) {
|
76 | cy.then(() => {
|
77 | expect(value).to.equal(expected)
|
78 | })
|
79 | }
|
80 |
|
81 | function dummyReadFileSync () {
|
82 | throw new Error(`In the browser, please call snapshot.init()
|
83 | before calling tests, like this:
|
84 | const snapshot = require('snap-shot')
|
85 | beforeEach(snapshot.init)
|
86 | `)
|
87 | }
|
88 |
|
89 | // TODO replace exposed API with error methods that wait id:2
|
90 | // Gleb Bahmutov
|
91 | // gleb.bahmutov@gmail.com
|
92 | // https://github.com/bahmutov/snap-shot-core/issues/87
|
93 | // until "init" is called
|
94 | const api = {
|
95 | loadSnapshots,
|
96 | saveSnapshots,
|
97 | init,
|
98 | readFileSync: dummyReadFileSync,
|
99 | raiseIfDifferent
|
100 | }
|
101 | module.exports = api
|