|
6 | 6 | */ |
7 | 7 |
|
8 | 8 | import * as path from 'path'; |
| 9 | +import * as fs from 'graceful-fs'; |
9 | 10 | import {skipSuiteOnJasmine} from '@jest/test-utils'; |
10 | 11 | import {cleanup, makeTemplate, writeFiles} from '../Utils'; |
11 | 12 | import runJest from '../runJest'; |
@@ -58,6 +59,202 @@ test('works with a single snapshot', () => { |
58 | 59 | } |
59 | 60 | }); |
60 | 61 |
|
| 62 | +test('keeps snapshot counts from other tests when a test retries', () => { |
| 63 | + const filename = 'counts-across-retries.test.js'; |
| 64 | + const template = makeTemplate(` |
| 65 | + test('passing snapshot', () => expect('foo').toMatchSnapshot()); |
| 66 | +
|
| 67 | + describe('with retries', () => { |
| 68 | + let index = 0; |
| 69 | + afterEach(() => { |
| 70 | + index += 1; |
| 71 | + }); |
| 72 | + jest.retryTimes(4); |
| 73 | + test('flaky snapshot', () => expect(index).toMatchSnapshot()); |
| 74 | + }); |
| 75 | + `); |
| 76 | + |
| 77 | + writeFiles(TESTS_DIR, { |
| 78 | + [filename]: template([]), |
| 79 | + [`__snapshots__/${filename}.snap`]: `// Jest Snapshot v1, https://goo.gl/fbAQLP |
| 80 | +
|
| 81 | +exports[\`with retries flaky snapshot 1\`] = \`3\`; |
| 82 | +`, |
| 83 | + }); |
| 84 | + |
| 85 | + const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false', filename]); |
| 86 | + |
| 87 | + expect(exitCode).toBe(0); |
| 88 | + expect(stderr).toMatch('1 snapshot written from 1 test suite.'); |
| 89 | + expect(stderr).toMatch('Snapshots: 1 written, 1 passed, 2 total'); |
| 90 | +}); |
| 91 | + |
| 92 | +test('reports a snapshot added on a discarded attempt as written', () => { |
| 93 | + const filename = 'written-on-retry.test.js'; |
| 94 | + const template = makeTemplate(` |
| 95 | + let index = 0; |
| 96 | + afterEach(() => { |
| 97 | + index += 1; |
| 98 | + }); |
| 99 | + jest.retryTimes(1); |
| 100 | + test('existing', () => expect('old').toMatchSnapshot()); |
| 101 | + test('adds a new snapshot then fails once', () => { |
| 102 | + expect('brand new').toMatchSnapshot(); |
| 103 | + expect(index).toBe(2); |
| 104 | + }); |
| 105 | + `); |
| 106 | + |
| 107 | + writeFiles(TESTS_DIR, { |
| 108 | + [filename]: template([]), |
| 109 | + [`__snapshots__/${filename}.snap`]: `// Jest Snapshot v1, https://goo.gl/fbAQLP |
| 110 | +
|
| 111 | +exports[\`existing 1\`] = \`"old"\`; |
| 112 | +`, |
| 113 | + }); |
| 114 | + |
| 115 | + const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false', filename]); |
| 116 | + |
| 117 | + expect(exitCode).toBe(0); |
| 118 | + expect(stderr).toMatch('1 snapshot written from 1 test suite.'); |
| 119 | + expect(stderr).toMatch('Snapshots: 1 written, 1 passed, 2 total'); |
| 120 | +}); |
| 121 | + |
| 122 | +test('still reports a snapshot only reached on a discarded attempt as obsolete', () => { |
| 123 | + const filename = 'obsolete-across-retries.test.js'; |
| 124 | + const template = makeTemplate(` |
| 125 | + let attempt = 0; |
| 126 | + jest.retryTimes(1); |
| 127 | + test('varies', () => { |
| 128 | + attempt += 1; |
| 129 | + expect('a').toMatchSnapshot(); |
| 130 | + if (attempt === 1) { |
| 131 | + expect('b').toMatchSnapshot(); |
| 132 | + throw new Error('fail once'); |
| 133 | + } |
| 134 | + }); |
| 135 | + `); |
| 136 | + |
| 137 | + writeFiles(TESTS_DIR, { |
| 138 | + [filename]: template([]), |
| 139 | + [`__snapshots__/${filename}.snap`]: `// Jest Snapshot v1, https://goo.gl/fbAQLP |
| 140 | +
|
| 141 | +exports[\`varies 1\`] = \`"a"\`; |
| 142 | +
|
| 143 | +exports[\`varies 2\`] = \`"b"\`; |
| 144 | +`, |
| 145 | + }); |
| 146 | + |
| 147 | + const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false', filename]); |
| 148 | + |
| 149 | + // Matches a run without retries: an obsolete snapshot is reported and fails |
| 150 | + // the run, even though every test passed. |
| 151 | + expect(exitCode).toBe(1); |
| 152 | + expect(stderr).toMatch('Snapshots: 1 obsolete, 1 passed, 1 total'); |
| 153 | +}); |
| 154 | + |
| 155 | +test('keeps counters for colliding full names across an immediate retry', () => { |
| 156 | + const filename = 'counters-across-immediate-retries.test.js'; |
| 157 | + const template = makeTemplate(` |
| 158 | + let attempt = 0; |
| 159 | + jest.retryTimes(1, {retryImmediately: true}); |
| 160 | +
|
| 161 | + describe('a', () => { |
| 162 | + test('b c', () => { |
| 163 | + expect('first').toMatchSnapshot(); |
| 164 | + }); |
| 165 | + }); |
| 166 | +
|
| 167 | + test('flaky', () => { |
| 168 | + attempt += 1; |
| 169 | + expect(attempt).toBe(2); |
| 170 | + }); |
| 171 | +
|
| 172 | + describe('a b', () => { |
| 173 | + test('c', () => { |
| 174 | + expect('second').toMatchSnapshot(); |
| 175 | + }); |
| 176 | + }); |
| 177 | + `); |
| 178 | + |
| 179 | + writeFiles(TESTS_DIR, {[filename]: template([])}); |
| 180 | + |
| 181 | + const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false', filename]); |
| 182 | + const snapshot = fs.readFileSync( |
| 183 | + path.join(TESTS_DIR, '__snapshots__', `${filename}.snap`), |
| 184 | + 'utf8', |
| 185 | + ); |
| 186 | + |
| 187 | + // The two full names collide, so the second test's key must keep counting |
| 188 | + // from the first test's, even though the flaky test was cleared in between. |
| 189 | + expect(exitCode).toBe(0); |
| 190 | + expect(stderr).toMatch('Snapshots: 2 written, 2 total'); |
| 191 | + expect(snapshot).toContain('exports[`a b c 1`] = `"first"`;'); |
| 192 | + expect(snapshot).toContain('exports[`a b c 2`] = `"second"`;'); |
| 193 | +}); |
| 194 | + |
| 195 | +test('scopes retry cleanup to the test, not its enclosing hooks', () => { |
| 196 | + const filename = 'hook-snapshots-on-retry.test.js'; |
| 197 | + const template = makeTemplate(` |
| 198 | + let attempt = 0; |
| 199 | + jest.retryTimes(1); |
| 200 | +
|
| 201 | + describe('suite', () => { |
| 202 | + beforeAll(() => { |
| 203 | + expect('from beforeAll').toMatchSnapshot(); |
| 204 | + }); |
| 205 | + beforeEach(() => { |
| 206 | + expect('from beforeEach').toMatchSnapshot(); |
| 207 | + }); |
| 208 | + test('flaky', () => { |
| 209 | + attempt += 1; |
| 210 | + expect('from test').toMatchSnapshot(); |
| 211 | + expect(attempt).toBe(2); |
| 212 | + }); |
| 213 | + }); |
| 214 | + `); |
| 215 | + |
| 216 | + writeFiles(TESTS_DIR, {[filename]: template([])}); |
| 217 | + |
| 218 | + const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false', filename]); |
| 219 | + |
| 220 | + // `beforeAll` runs outside the test, so its snapshot is written once and |
| 221 | + // survives the retry. The `beforeEach` and test snapshots belong to the test, |
| 222 | + // so the discarded attempt's copies are rolled back and written once each. |
| 223 | + expect(exitCode).toBe(0); |
| 224 | + expect(stderr).toMatch('3 snapshots written from 1 test suite.'); |
| 225 | + expect(stderr).toMatch('Snapshots: 3 written, 3 total'); |
| 226 | +}); |
| 227 | + |
| 228 | +test('keeps concurrent tests independent when one of them retries', () => { |
| 229 | + const filename = 'concurrent-snapshots-on-retry.test.js'; |
| 230 | + const template = makeTemplate(` |
| 231 | + let attempt = 0; |
| 232 | + jest.retryTimes(1); |
| 233 | +
|
| 234 | + test.concurrent('steady', async () => { |
| 235 | + expect('steady value').toMatchSnapshot(); |
| 236 | + }); |
| 237 | + test.concurrent('flaky', async () => { |
| 238 | + attempt += 1; |
| 239 | + expect('flaky value').toMatchSnapshot(); |
| 240 | + expect(attempt).toBe(2); |
| 241 | + }); |
| 242 | + `); |
| 243 | + |
| 244 | + writeFiles(TESTS_DIR, {[filename]: template([])}); |
| 245 | + |
| 246 | + const {stderr, exitCode} = runJest(DIR, [ |
| 247 | + '-w=1', |
| 248 | + '--maxConcurrency=1', |
| 249 | + '--ci=false', |
| 250 | + filename, |
| 251 | + ]); |
| 252 | + |
| 253 | + expect(exitCode).toBe(0); |
| 254 | + expect(stderr).toMatch('2 snapshots written from 1 test suite.'); |
| 255 | + expect(stderr).toMatch('Snapshots: 2 written, 2 total'); |
| 256 | +}); |
| 257 | + |
61 | 258 | test('works when multiple tests have snapshots but only one of them failed multiple times', () => { |
62 | 259 | const filename = 'basic-support.test.js'; |
63 | 260 | const template = makeTemplate(` |
@@ -96,7 +293,7 @@ test('works when multiple tests have snapshots but only one of them failed multi |
96 | 293 | [filename]: template(['index', '4' /* retries */]), |
97 | 294 | }); |
98 | 295 | const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false', filename]); |
99 | | - expect(stderr).toMatch('Snapshots: 1 passed, 1 total'); |
| 296 | + expect(stderr).toMatch('Snapshots: 2 passed, 2 total'); |
100 | 297 | expect(exitCode).toBe(0); |
101 | 298 | } |
102 | 299 | }); |
0 commit comments