|
1 | | -import { ng } from '../../utils/process'; |
2 | | -import { appendToFile, replaceInFile, readFile } from '../../utils/fs'; |
| 1 | +import { exec, ng, noSilentNg } from '../../utils/process'; |
| 2 | +import { appendToFile, replaceInFile, readFile, writeFile } from '../../utils/fs'; |
3 | 3 | import { applyVitestBuilder } from '../../utils/vitest'; |
4 | 4 | import assert from 'node:assert/strict'; |
5 | 5 | import { stripVTControlCharacters } from 'node:util'; |
| 6 | +import { setTimeout } from 'node:timers/promises'; |
6 | 7 |
|
7 | 8 | export default async function () { |
8 | 9 | // Set up the test project to use the vitest runner |
9 | 10 | await applyVitestBuilder(); |
10 | 11 |
|
11 | 12 | // Add snapshot assertions to the test file |
12 | | - await appendToFile( |
| 13 | + await replaceInFile( |
13 | 14 | 'src/app/app.spec.ts', |
| 15 | + `describe('App', () => {`, |
14 | 16 | ` |
15 | | - it('should match file snapshot', () => { |
16 | | - const fixture = TestBed.createComponent(App); |
17 | | - const app = fixture.componentInstance; |
18 | | - expect((app as any).title()).toMatchSnapshot(); |
19 | | - }); |
| 17 | +describe('App', () => { |
| 18 | + it('should match file snapshot', () => { |
| 19 | + const fixture = TestBed.createComponent(App); |
| 20 | + const app = fixture.componentInstance; |
| 21 | + expect((app as any).title()).toMatchSnapshot(); |
| 22 | + }); |
20 | 23 |
|
21 | | - it('should match inline snapshot', () => { |
22 | | - const fixture = TestBed.createComponent(App); |
23 | | - const app = fixture.componentInstance; |
24 | | - expect((app as any).title()).toMatchInlineSnapshot(); |
25 | | - }); |
26 | | - `, |
| 24 | + it('should match inline snapshot', () => { |
| 25 | + const fixture = TestBed.createComponent(App); |
| 26 | + const app = fixture.componentInstance; |
| 27 | + expect((app as any).title()).toMatchInlineSnapshot(); |
| 28 | + }); |
| 29 | +`, |
| 30 | + ); |
| 31 | + |
| 32 | + let content = await readFile('src/app/app.spec.ts'); |
| 33 | + content = content.replace(/\r\n/g, '\n'); |
| 34 | + content = content.replace(/\r/g, '\n'); |
| 35 | + await writeFile('src/app/app.spec.ts', content); |
| 36 | + |
| 37 | + await replaceInFile( |
| 38 | + 'node_modules/@vitest/snapshot/dist/index.js', |
| 39 | + 'const transformed = s.toString()', |
| 40 | + 'console.log(file, JSON.stringify(snapshots, null, 2));\nconst transformed = s.toString()', |
| 41 | + ); |
| 42 | + |
| 43 | + await replaceInFile( |
| 44 | + 'node_modules/@vitest/snapshot/dist/index.js', |
| 45 | + 'const transformed = s.toString();', |
| 46 | + 'const transformed = s.toString();console.log(transformed);', |
27 | 47 | ); |
28 | 48 |
|
29 | 49 | // First run: create snapshots |
30 | | - const { stdout: firstRunStdout } = await ng('test'); |
| 50 | + const { stdout: firstRunStdout } = await noSilentNg('test'); |
31 | 51 | assert.match( |
32 | 52 | stripVTControlCharacters(firstRunStdout), |
33 | 53 | /Snapshots\s+2 written/, |
34 | 54 | 'Snapshots were not written on the first run.', |
35 | 55 | ); |
36 | 56 |
|
| 57 | + const snapshotContent = await readFile('src/app/__snapshots__/app.spec.ts.snap'); |
| 58 | + assert.match( |
| 59 | + snapshotContent, |
| 60 | + /exports\[`App > should match file snapshot 1`\] = `"test-project"`;/, |
| 61 | + 'File snapshot was not written to disk.', |
| 62 | + ); |
| 63 | + |
37 | 64 | const specContent = await readFile('src/app/app.spec.ts'); |
| 65 | + console.log(specContent); |
38 | 66 | assert.match( |
39 | 67 | specContent, |
40 | 68 | /toMatchInlineSnapshot\(`"test-project"`\)/, |
41 | 69 | 'Inline snapshot was not written to the spec file.', |
42 | 70 | ); |
43 | 71 |
|
44 | | - const snapshotContent = await readFile('src/app/__snapshots__/app.spec.ts.snap'); |
45 | | - assert.match( |
46 | | - snapshotContent, |
47 | | - /exports\[`should match file snapshot 1`\] = `"test-project"`;/, |
48 | | - 'File snapshot was not written to disk.', |
49 | | - ); |
50 | | - |
51 | 72 | // Second run: tests should pass with existing snapshots |
52 | 73 | await ng('test'); |
53 | 74 |
|
|
0 commit comments