Skip to content

Commit

Permalink
add whitespace example
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed May 1, 2023
1 parent 3f5387d commit c9886a7
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions docs/recipes/sha-256.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ Using SHA hash code to confirm the element changes is a bad practice in opinion.
.getElementById('load')
.addEventListener('click', () => {
document.getElementById('output').innerHTML = `
<div>Error loading...</div>
`
<div>Error loading...</div>
`
})
</script>
```
Expand Down Expand Up @@ -150,6 +150,15 @@ cy.get('#output')

The page does change and the element's HTML has a new SHA, but is this a good test? Absolutely not. The page shows an error element, not successfully loaded data. The test should confirm one successful output, not try to assertion that a million different possible _other_ outputs do not appear.

SHA code changes even when the only changes are invisible to the user, like whitespace around elements.

```js
expect(
digestMessage('<div>Hello</div>'),
'extra space at the end',
).to.not.equal(digestMessage('<div>Hello</div> '))
```

**Note:** there is also a subtle timing bug in the above test, something that affects every test framework that relies only on promises or `async/await` syntax to execute its commands.

<!-- fiddle-end -->
Expand Down

0 comments on commit c9886a7

Please sign in to comment.