Skip to content

Commit

Permalink
(fix): snapshot tests
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-bromann committed Feb 7, 2024
1 parent 7673d4c commit a742671
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ export class SnapshotService implements Services.ServiceInstance {
return this.#snapshotResults
}

beforeTest(test: Frameworks.Test) {
async beforeTest(test: Frameworks.Test) {
this.#currentFilePath = test.file
this.#currentTestName = `${test.parent} > ${test.title}`
this.#snapshotClient.startCurrentRun(test.file, test.fullTitle, this.#options)
await this.#snapshotClient.startCurrentRun(test.file, test.fullTitle, this.#options)
}

async after() {
Expand Down
7 changes: 7 additions & 0 deletions test/file.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Snapshot v1

exports[`parent > test 1`] = `
{
"a": "a",
}
`;
33 changes: 24 additions & 9 deletions test/snapshot.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,35 @@
import fs from 'node:fs/promises'
import path from 'node:path'
import { test, expect } from 'vitest'
import type { Frameworks } from '@wdio/types'

import { expect as expectExport, SnapshotService } from '../src/index.js'

const service = SnapshotService.initiate()
service.beforeTest({
title: 'test',
parent: 'parent',
file: '/foo/bar/file',
} as Frameworks.Test)
const __dirname = path.dirname(new URL(import.meta.url).pathname)

const service = SnapshotService.initiate({
resolveSnapshotPath: (path, extension) => path + extension
})

test('supports snapshot testing', async () => {
await service.beforeTest({
title: 'test',
parent: 'parent',
file: `${__dirname}/file`,
} as Frameworks.Test)

test('supports snapshot testing', () => {
const exp = expectExport;
expect(exp).toBeDefined()
expect(exp({}).toMatchSnapshot).toBeDefined()
expect(exp({}).toMatchInlineSnapshot).toBeDefined()
exp({ a: 'a' }).toMatchSnapshot()
exp({ a: 'a' }).toMatchInlineSnapshot()
await exp({ a: 'a' }).toMatchSnapshot()
/**
* doesn't work without running in WebdriverIO test runner context
*/
// await exp({ a: 'a' }).toMatchInlineSnapshot()
await service.after()

const expectedSnapfileExist = await fs.access(path.resolve(__dirname, 'file.snap'))
.then(() => true, () => false)
expect(expectedSnapfileExist).toBe(true)
})

0 comments on commit a742671

Please sign in to comment.