From eba871df3e00d4bb392e1b31298d2bad141201b8 Mon Sep 17 00:00:00 2001 From: jbblanchet Date: Wed, 12 Feb 2025 15:49:33 -0500 Subject: [PATCH] Add beforeStep to support cucumber framework (#1759) * Add beforeStep to support cucumber framework * Fix missing name in test * Add @wdio/types as dev dependency --- package-lock.json | 78 ++++++++++++++++++++++++++++++++++++++---- package.json | 1 + src/snapshot.ts | 9 +++++ test/file.feature.snap | 7 ++++ test/snapshot.test.ts | 21 ++++++++++++ 5 files changed, 109 insertions(+), 7 deletions(-) create mode 100644 test/file.feature.snap diff --git a/package-lock.json b/package-lock.json index 95d73a30..8cebd313 100644 --- a/package-lock.json +++ b/package-lock.json @@ -21,6 +21,7 @@ "@types/node": "^22.9.0", "@vitest/coverage-v8": "^3.0.5", "@wdio/eslint": "^0.0.5", + "@wdio/types": "^9.9.0", "c8": "^10.1.2", "eslint": "^9.15.0", "husky": "^9.1.5", @@ -2394,6 +2395,31 @@ "node": ">=18.20.0" } }, + "node_modules/@wdio/config/node_modules/@types/node": { + "version": "20.17.17", + "resolved": "https://registry.npmjs.org/@types/node/-/node-20.17.17.tgz", + "integrity": "sha512-/WndGO4kIfMicEQLTi/mDANUu/iVUhT7KboZPdEqqHQ4aTS+3qT3U5gIqWDFV+XouorjfgGqvKILJeHhuQgFYg==", + "license": "MIT", + "optional": true, + "peer": true, + "dependencies": { + "undici-types": "~6.19.2" + } + }, + "node_modules/@wdio/config/node_modules/@wdio/types": { + "version": "9.2.2", + "resolved": "https://registry.npmjs.org/@wdio/types/-/types-9.2.2.tgz", + "integrity": "sha512-nHZ9Ne9iRQFJ1TOYKUn4Fza69IshTTzk6RYmSZ51ImGs9uMZu0+S0Jm9REdly+VLN3FzxG6g2QSe0/F3uNVPdw==", + "license": "MIT", + "optional": true, + "peer": true, + "dependencies": { + "@types/node": "^20.1.0" + }, + "engines": { + "node": ">=18.20.0" + } + }, "node_modules/@wdio/config/node_modules/glob": { "version": "10.4.5", "resolved": "https://registry.npmjs.org/glob/-/glob-10.4.5.tgz", @@ -2494,6 +2520,20 @@ "undici-types": "~6.19.2" } }, + "node_modules/@wdio/globals/node_modules/@wdio/types": { + "version": "9.2.2", + "resolved": "https://registry.npmjs.org/@wdio/types/-/types-9.2.2.tgz", + "integrity": "sha512-nHZ9Ne9iRQFJ1TOYKUn4Fza69IshTTzk6RYmSZ51ImGs9uMZu0+S0Jm9REdly+VLN3FzxG6g2QSe0/F3uNVPdw==", + "license": "MIT", + "optional": true, + "peer": true, + "dependencies": { + "@types/node": "^20.1.0" + }, + "engines": { + "node": ">=18.20.0" + } + }, "node_modules/@wdio/globals/node_modules/webdriver": { "version": "9.2.8", "resolved": "https://registry.npmjs.org/webdriver/-/webdriver-9.2.8.tgz", @@ -2611,11 +2651,11 @@ } }, "node_modules/@wdio/types": { - "version": "9.2.2", - "resolved": "https://registry.npmjs.org/@wdio/types/-/types-9.2.2.tgz", - "integrity": "sha512-nHZ9Ne9iRQFJ1TOYKUn4Fza69IshTTzk6RYmSZ51ImGs9uMZu0+S0Jm9REdly+VLN3FzxG6g2QSe0/F3uNVPdw==", - "optional": true, - "peer": true, + "version": "9.9.0", + "resolved": "https://registry.npmjs.org/@wdio/types/-/types-9.9.0.tgz", + "integrity": "sha512-Mh7ryL7uWKECStKcF6pWSbYkC51OemOwQR2pmvymP5HOfG74s6RVbJ+Z6Om8ffiJeTI5nZuvNDzYNkUpm7Elzg==", + "dev": true, + "license": "MIT", "dependencies": { "@types/node": "^20.1.0" }, @@ -2627,8 +2667,7 @@ "version": "20.17.7", "resolved": "https://registry.npmjs.org/@types/node/-/node-20.17.7.tgz", "integrity": "sha512-sZXXnpBFMKbao30dUAvzKbdwA2JM1fwUtVEq/kxKuPI5mMwZiRElCpTXb0Biq/LMEVpXDZL5G5V0RPnxKeyaYg==", - "optional": true, - "peer": true, + "dev": true, "dependencies": { "undici-types": "~6.19.2" } @@ -2658,6 +2697,31 @@ "node": ">=18.20.0" } }, + "node_modules/@wdio/utils/node_modules/@types/node": { + "version": "20.17.17", + "resolved": "https://registry.npmjs.org/@types/node/-/node-20.17.17.tgz", + "integrity": "sha512-/WndGO4kIfMicEQLTi/mDANUu/iVUhT7KboZPdEqqHQ4aTS+3qT3U5gIqWDFV+XouorjfgGqvKILJeHhuQgFYg==", + "license": "MIT", + "optional": true, + "peer": true, + "dependencies": { + "undici-types": "~6.19.2" + } + }, + "node_modules/@wdio/utils/node_modules/@wdio/types": { + "version": "9.2.2", + "resolved": "https://registry.npmjs.org/@wdio/types/-/types-9.2.2.tgz", + "integrity": "sha512-nHZ9Ne9iRQFJ1TOYKUn4Fza69IshTTzk6RYmSZ51ImGs9uMZu0+S0Jm9REdly+VLN3FzxG6g2QSe0/F3uNVPdw==", + "license": "MIT", + "optional": true, + "peer": true, + "dependencies": { + "@types/node": "^20.1.0" + }, + "engines": { + "node": ">=18.20.0" + } + }, "node_modules/@zip.js/zip.js": { "version": "2.7.53", "resolved": "https://registry.npmjs.org/@zip.js/zip.js/-/zip.js-2.7.53.tgz", diff --git a/package.json b/package.json index f6c9cfd8..ae73d8fc 100644 --- a/package.json +++ b/package.json @@ -68,6 +68,7 @@ "@types/jest": "^29.5.12", "@types/lodash.isequal": "^4.5.8", "@types/node": "^22.9.0", + "@wdio/types": "^9.9.0", "@vitest/coverage-v8": "^3.0.5", "@wdio/eslint": "^0.0.5", "c8": "^10.1.2", diff --git a/src/snapshot.ts b/src/snapshot.ts index 01050ce9..f6a5b3e6 100644 --- a/src/snapshot.ts +++ b/src/snapshot.ts @@ -82,6 +82,15 @@ export class SnapshotService implements Services.ServiceInstance { await this.#snapshotClient.startCurrentRun(test.file, test.fullTitle, this.#options) } + async beforeStep(step: Frameworks.PickleStep, scenario: Frameworks.Scenario) { + const file = scenario.uri + const testName = `${scenario.name} > ${step.text}` + + this.#currentFilePath = file + this.#currentTestName = testName + await this.#snapshotClient.startCurrentRun(file, testName, this.#options) + } + async after() { const result = await this.#snapshotClient.finishCurrentRun() if (!result) { diff --git a/test/file.feature.snap b/test/file.feature.snap new file mode 100644 index 00000000..b543ec14 --- /dev/null +++ b/test/file.feature.snap @@ -0,0 +1,7 @@ +// Snapshot v1 + +exports[`Fake scenario > Fake step 1`] = ` +{ + "cucum": "ber", +} +`; diff --git a/test/snapshot.test.ts b/test/snapshot.test.ts index 85cdae95..521f03d0 100644 --- a/test/snapshot.test.ts +++ b/test/snapshot.test.ts @@ -33,3 +33,24 @@ test('supports snapshot testing', async () => { .then(() => true, () => false) expect(expectedSnapfileExist).toBe(true) }) + +test('supports cucumber snapshot testing', async () => { + await service.beforeStep({ + text: 'Fake step', + } as Frameworks.PickleStep, { + name: 'Fake scenario', + uri: `${__dirname}/file.feature`, + } as Frameworks.Scenario) + + const exp = expectExport + expect(exp).toBeDefined() + expect(exp({}).toMatchSnapshot).toBeDefined() + expect(exp({}).toMatchInlineSnapshot).toBeDefined() + await exp({ cucum: 'ber' }).toMatchSnapshot() + await service.after() + + const expectedSnapfileExist = await fs.access(path.resolve(__dirname, 'file.feature.snap')) + .then(() => true, () => false) + expect(expectedSnapfileExist).toBe(true) +}) +