Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*.snap files content should be the same regardless of test execution order #2311

Closed
ehmicky opened this issue Dec 13, 2019 · 3 comments · Fixed by #2610
Closed

*.snap files content should be the same regardless of test execution order #2311

ehmicky opened this issue Dec 13, 2019 · 3 comments · Fixed by #2610
Labels
bug current functionality does not work as desired help wanted scope:snapshot-management

Comments

@ehmicky
Copy link

ehmicky commented Dec 13, 2019

Description

When a test file has several parallel async tests and each of those tests call t.snapshot(), the resulting *.snap file is non-deterministic. The contents of the *.snap file depends on the order in which tests finished.

In such cases, tests pass correctly. However the *.snap files contents keep being updated, which means they are shown as dirty files by git status and they result in merge conflicts, especially in PRs.

Making *.snap file contents independent of test execution order would solve this issue.

Test Source

test.js

const test = require('ava')

const { promisify } = require('util')
const pSetTimeout = promisify(setTimeout)

test('one', async t => {
  await pSetTimeout(Math.random() * 1e3)
  t.snapshot('one')
})

test('two', async t => {
  await pSetTimeout(Math.random() * 1e3)
  t.snapshot('two')
})

Running ava -u on this file produces different *.snap depending on whether one or two finishes first.

$ ava -u test.js && sha256sum test.js.snap 

  2 tests passed

99b2f48bae8ee408643d0885635d18f1373ab17f2d3988160f5d5fc36e525f47  test.js.snap

$ ava -u test.js && sha256sum test.js.snap 

  2 tests passed

fbaf5758abca960c7b8e1daf7e06785cabed91c641d7daf595fb6939befd7daa  test.js.snap

$ ava -u test.js && sha256sum test.js.snap 

  2 tests passed

99b2f48bae8ee408643d0885635d18f1373ab17f2d3988160f5d5fc36e525f47  test.js.snap

Command-Line Arguments

Copy your npm build scripts or the ava command used:

ava -u test.js

Environment

Tell us which operating system you are using, as well as which versions of Node.js, npm, and AVA. Run the following to get it quickly:

$ node -e "var os=require('os');console.log('Node.js ' + process.version + '\n' + os.platform() + ' ' + os.release())"
Node.js v13.3.0
linux 5.3.0-24-generic
$ ava --version
2.4.0
$ npm --version
6.13.4
@novemberborn
Copy link
Member

Oh interesting, I've never noticed that before. We should sort by test title, like we do for the Markdown files:

const sortedKeys = [...entries.keys()].sort();

@novemberborn
Copy link
Member

We should sort by test title, like we do for the Markdown files:

We're looking at sorting by declaration order instead, see #2324. That said, this issue is primarily about having a stable sort order, and we could still use test titles for that.

@novemberborn novemberborn removed this from the v3 milestone Jan 2, 2020
novemberborn added a commit that referenced this issue Dec 6, 2020
Ensures that *.snap files are deterministic by sorting entry blocks by the hash of their test name or id.

Ensures that *.md snapshot report files are sorted in as close to declaration order as is reasonably possible.

Closes #2311
Closes #2324

Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
Co-authored-by: Mark Wubben <mark@novemberborn.net>
@ehmicky
Copy link
Author

ehmicky commented Dec 7, 2020

Sweet, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug current functionality does not work as desired help wanted scope:snapshot-management
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants