Skip to content

Commit

Permalink
chore: generate inline snapshot test instead of using one
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Jun 21, 2022
1 parent f28591c commit 73a722f
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -17,4 +17,4 @@ cypress/videos
cypress/downloads
cypress/screenshots
docs/public/user-avatars
docs/public/sponsors
docs/public/sponsors
3 changes: 2 additions & 1 deletion test/snapshots/package.json
Expand Up @@ -2,7 +2,8 @@
"name": "@vitest/snapshots",
"private": true,
"scripts": {
"test": "pnpm run test:update && pnpm run test:snaps",
"test": "pnpm run test:generate && pnpm run test:update && pnpm run test:snaps",
"test:generate": "node tools/generate-inline-test.mjs",
"test:snaps": "vitest run --dir test",
"test:update": "vitest run -u --dir test-update",
"coverage": "vitest run --coverage"
Expand Down
19 changes: 14 additions & 5 deletions test/snapshots/test-update/shapshots-inline-js.test.js
@@ -1,16 +1,25 @@
import { describe, expect, test } from 'vitest'
import { validateNoSnapshots } from './validate-inline-js'

// when snapshots are generated Vitest reruns `toMatchInlineSnapshot` checks
// please, don't commit generated snapshots
describe('snapshots are generated in correct order', async () => {
await validateNoSnapshots(__filename)

test('first snaphot', () => {
expect({ foo: ['bar'] }).toMatchInlineSnapshot()
expect({ foo: ['bar'] }).toMatchInlineSnapshot(`
Object {
"foo": Array [
"bar",
],
}
`)
})

test('second snapshot', () => {
expect({ foo: ['zed'] }).toMatchInlineSnapshot()
expect({ foo: ['zed'] }).toMatchInlineSnapshot(`
Object {
"foo": Array [
"zed",
],
}
`)
})
})
8 changes: 0 additions & 8 deletions test/snapshots/test-update/validate-inline-js.ts

This file was deleted.

3 changes: 0 additions & 3 deletions test/snapshots/test/__snapshots__/shapshots.test.ts.snap
Expand Up @@ -2,13 +2,10 @@

exports[`js snapshots generated correctly 1`] = `
"import { describe, expect, test } from 'vitest'
import { validateNoSnapshots } from './validate-inline-js'
// when snapshots are generated Vitest reruns \`toMatchInlineSnapshot\` checks
// please, don't commit generated snapshots
describe('snapshots are generated in correct order', async () => {
await validateNoSnapshots(__filename)
test('first snaphot', () => {
expect({ foo: ['bar'] }).toMatchInlineSnapshot(\`
Object {
Expand Down
18 changes: 18 additions & 0 deletions test/snapshots/tools/generate-inline-test.mjs
@@ -0,0 +1,18 @@
import fs from 'fs/promises'
import { fileURLToPath } from 'url'
import pathe from 'pathe'

const dirname = pathe.dirname(fileURLToPath(import.meta.url))

export async function generateInlineTest(templatePath, testpath) {
const template = await fs.readFile(templatePath, 'utf8')
await fs.writeFile(testpath, template)
console.log(`Generated ${testpath}`)
}

const filepath = pathe.resolve(dirname, '../test-update/shapshots-inline-js.test.js')
const template = pathe.resolve(dirname, './inline-test-template.js');

(async () => {
await generateInlineTest(template, filepath)
})()
13 changes: 13 additions & 0 deletions test/snapshots/tools/inline-test-template.js
@@ -0,0 +1,13 @@
import { describe, expect, test } from 'vitest'

// when snapshots are generated Vitest reruns `toMatchInlineSnapshot` checks
// please, don't commit generated snapshots
describe('snapshots are generated in correct order', async () => {
test('first snaphot', () => {
expect({ foo: ['bar'] }).toMatchInlineSnapshot()
})

test('second snapshot', () => {
expect({ foo: ['zed'] }).toMatchInlineSnapshot()
})
})

0 comments on commit 73a722f

Please sign in to comment.