From a7426717896394aec905191f64c0e41cd8fdafc9 Mon Sep 17 00:00:00 2001 From: Christian Bromann Date: Tue, 6 Feb 2024 22:47:56 -0800 Subject: [PATCH] (fix): snapshot tests --- src/snapshot.ts | 4 ++-- test/file.snap | 7 +++++++ test/snapshot.test.ts | 33 ++++++++++++++++++++++++--------- 3 files changed, 33 insertions(+), 11 deletions(-) create mode 100644 test/file.snap diff --git a/src/snapshot.ts b/src/snapshot.ts index b5cf75e1..01050ce9 100644 --- a/src/snapshot.ts +++ b/src/snapshot.ts @@ -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() { diff --git a/test/file.snap b/test/file.snap new file mode 100644 index 00000000..7af66be0 --- /dev/null +++ b/test/file.snap @@ -0,0 +1,7 @@ +// Snapshot v1 + +exports[`parent > test 1`] = ` +{ + "a": "a", +} +`; diff --git a/test/snapshot.test.ts b/test/snapshot.test.ts index 1eadba66..f42880eb 100644 --- a/test/snapshot.test.ts +++ b/test/snapshot.test.ts @@ -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) })