• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

TotalTechGeek / pineapple / 5215358360

pending completion
5215358360

Pull #26

github

web-flow
Merge 232737772 into 5bafc7bbf
Pull Request #26: Add more arbitraries and omissions

2119 of 2290 branches covered (92.53%)

Branch coverage included in aggregate %.

179 of 179 new or added lines in 9 files covered. (100.0%)

9030 of 9407 relevant lines covered (95.99%)

1489.38 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

16.05
/inputs.js
1
import chalk from 'chalk'
12✔
2
import { serialize } from './snapshot.js'
12✔
3
import { diff } from './utils.js'
12✔
4
import { flush } from './run.js'
12✔
5
import { getDiff } from 'json-difference'
12✔
6

12✔
7
async function getConfirmation (message, choices = ['Yes', 'No '], defaultChoice = null) {
×
8
  if (typeof prompt !== 'undefined') {
×
9
    const result = prompt(`${message} (Y/N)`).toLowerCase()
×
10
    console.log()
×
11
    return result === 'y' || result === 'yes' ? 'Yes' : 'No '
×
12
  }
×
13
  return new Promise(resolve => {
×
14
    const currentMode = process.stdin.isRaw
×
15
    if (process.stdin.setRawMode) process.stdin.setRawMode(true)
×
16

×
17
    let position = choices.indexOf(defaultChoice)
×
18
    if (position === -1) position = 0
×
19

×
20
    process.stdout.write(message + ' ')
×
21
    let choice = choices[position]
×
22
    process.stdout.write(choices[position])
×
23

×
24
    const func = data => {
×
25
      if (data[0] === 13) {
×
26
        if (process.stdin.setRawMode) process.stdin.setRawMode(currentMode)
×
27
        process.stdin.removeListener('data', func)
×
28
        console.log()
×
29
        return resolve(choice)
×
30
      }
×
31

×
32
      data = data.toString()
×
33

×
34
      if (data === '\x1B[B') {
×
35
        process.stdout.write('\r')
×
36
        process.stdout.write(message + ' ')
×
37
        position++
×
38
        if (position >= choices.length) position = 0
×
39
        process.stdout.write(choices[position])
×
40
        choice = choices[position]
×
41
      }
×
42

×
43
      if (data === '\x1B[A') {
×
44
        process.stdout.write('\r')
×
45
        process.stdout.write(message + ' ')
×
46
        position--
×
47
        if (position < 0) position = choices.length - 1
×
48
        process.stdout.write(choices[position])
×
49
        choice = choices[position]
×
50
      }
×
51
    }
×
52

×
53
    process.stdin.on('data', func)
×
54
  })
×
55
}
×
56

12✔
57
/**
12✔
58
 * It asks the user if they want to accept the snapshot
12✔
59
 * @param {{ item: any, rule: string, id: string, file: string }} data
12✔
60
 * @returns A function that returns a boolean.
12✔
61
 */
12✔
62
export async function askSnapshot ({ item, rule, id, file }) {
3✔
63
  if (process.env.CI) return false
×
64
  if (process.env.ACCEPT_ALL) return true
×
65
  if (process.env.OUTPUT_FORMAT === 'JSON') {
×
66
    console.log(JSON.stringify({
×
67
      type: 'Request Snapshot',
×
68
      item: serialize(item),
×
69
      input: rule,
×
70
      id,
×
71
      file
×
72
    }))
×
73
    return false
×
74
  }
×
75
  console.log(`On test (${id.split('(')[0]}):`, rule)
×
76
  flush()
×
77

×
78
  console.log(chalk.green(serialize(item)))
×
79
  const result = await getConfirmation(`${chalk.magenta('?')} Accept this snapshot?`).catch(err => console.log(err))
×
80
  return result
×
81
}
×
82

12✔
83
// Used by the askSnapshotUpdate function to store the user's choice
12✔
84
// this is ugly code, but it's stored within the inputs.js file so it's not too bad
12✔
85
let snapshotChoice
12✔
86
let snapshotChoiceMode
12✔
87

12✔
88
/**
12✔
89
 * It asks the user if they want to update the snapshot
12✔
90
 * @param {{ item: any, rule: string, id: string, value: any, file: string }} data
12✔
91
 * @returns A function that returns a boolean.
12✔
92
 */
12✔
93
export async function askSnapshotUpdate ({ item, value, rule, id, file }) {
3✔
94
  if (process.env.CI) return false
×
95
  if (process.env.UPDATE_ALL) return true
×
96
  if (process.env.OUTPUT_FORMAT === 'JSON') {
×
97
    console.log(JSON.stringify({
×
98
      type: 'Request Snapshot Update',
×
99
      new: serialize(item),
×
100
      old: serialize(value),
×
101
      input: rule,
×
102
      id,
×
103
      file
×
104
    }))
×
105
    return false
×
106
  }
×
107
  console.log(`On test (${id.split('(')[0]}):`, rule)
×
108
  flush()
×
109

×
110
  console.log(diff(value, item))
×
111

×
112
  if (typeof value === typeof item && value && item && typeof value === 'object') {
×
113
    const differences = getDiff(value, item, true)
×
114
    if (differences.added.length === 0 && differences.removed.length === 0) {
×
115
      // This means that fields were modified, which means we can offer to omit the fields
×
116
      console.log(chalk.yellow('\nSome of the fields have been modified, you can choose to omit them.'))
×
117

×
118
      const result = await getConfirmation(`${chalk.magenta('?')} Do you wish to update to this snapshot?`, ['Yes ', 'No  ', 'Omit'], snapshotChoice)
×
119
      snapshotChoice = result
×
120
      if (result === 'Omit') {
×
121
        const transform = { omitDeep: [{ var: '' }, differences.edited.map(i => i[0])] }
×
122
        const mode = await getConfirmation(`${chalk.magenta('?')} What checks do you want to perform on the omitted fields?`, ['Type  ', 'None  ', 'Truthy'], snapshotChoiceMode)
×
123
        snapshotChoiceMode = mode
×
124

×
125
        if (mode === 'Type  ') {
×
126
          return {
×
127
            transform,
×
128
            check: {
×
129
              and: differences.edited.map(i => ({
×
130
                '===': [
×
131
                  { typeof: { var: `actual.${i[0]}` } },
×
132
                  { typeof: { var: `expected.${i[0]}` } }
×
133
                ]
×
134
              }))
×
135
            }
×
136
          }
×
137
        }
×
138

×
139
        if (mode === 'Truthy') {
×
140
          return {
×
141
            transform,
×
142
            check: {
×
143
              and: differences.edited.map(i => ({
×
144
                '===': [
×
145
                  { '!!': { var: `actual.${i[0]}` } },
×
146
                  { '!!': { var: `expected.${i[0]}` } }
×
147
                ]
×
148
              }))
×
149
            }
×
150
          }
×
151
        }
×
152

×
153
        return { transform }
×
154
      }
×
155
      return result === 'Yes '
×
156
    }
×
157
  }
×
158

×
159
  const result = await getConfirmation(`${chalk.magenta('?')} Do you wish to update to this snapshot?`)
×
160
  return result === 'Yes'
×
161
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc