-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: add experimental support of steps option (related to #94)
- Loading branch information
Showing
16 changed files
with
168 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* Load steps using Playwright's import instead of Cucumber's tryRequire. | ||
* Based on: https://github.com/cucumber/cucumber-js/blob/main/src/api/support.ts | ||
*/ | ||
import { IdGenerator } from '@cucumber/messages'; | ||
import { supportCodeLibraryBuilder } from '@cucumber/cucumber'; | ||
import { ISupportCodeLibrary } from './types'; | ||
import { resolveFiles } from '../utils/paths'; | ||
import { requireTransform } from '../playwright/transform'; | ||
import { toArray } from '../utils'; | ||
|
||
const newId = IdGenerator.uuid(); | ||
|
||
export async function loadStepsOwn( | ||
cwd: string, | ||
stepPaths: string | string[], | ||
): Promise<ISupportCodeLibrary> { | ||
supportCodeLibraryBuilder.reset(cwd, newId, { | ||
requireModules: [], | ||
requirePaths: [], | ||
importPaths: [], | ||
}); | ||
|
||
const stepFiles = await resolveFiles(cwd, toArray(stepPaths), '{js,mjs,cjs,ts,mts,cts}'); | ||
const { requireOrImport } = requireTransform(); | ||
|
||
for (const file of stepFiles) { | ||
await requireOrImport(file); | ||
} | ||
|
||
return supportCodeLibraryBuilder.finalize(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import path from 'node:path'; | ||
import fg from 'fast-glob'; | ||
|
||
/** | ||
* Returns path with "/" separator on all platforms. | ||
* See: https://stackoverflow.com/questions/53799385/how-can-i-convert-a-windows-path-to-posix-path-using-node-path | ||
*/ | ||
export function toPosixPath(somePath: string) { | ||
return somePath.split(path.sep).join(path.posix.sep); | ||
} | ||
|
||
/** | ||
* Resolves patterns to list of files. | ||
* Extension can be a list: {js,ts} | ||
* See: https://github.com/cucumber/cucumber-js/blob/main/src/paths/paths.ts | ||
*/ | ||
export async function resolveFiles(cwd: string, patterns: string[], extension: string) { | ||
const finalPatterns = patterns.map((pattern) => finalizePattern(pattern, extension)); | ||
return fg.glob(finalPatterns, { cwd, absolute: true, dot: true }); | ||
} | ||
|
||
/** | ||
* Appends file extension(s) to pattern. | ||
* Example: 'path/to/dir' -> 'path/to/dir/** /*.{js,ts}' | ||
*/ | ||
export function finalizePattern(pattern: string, extension: string) { | ||
// On Windows convert path to forward slash. | ||
// Note: pattern must always use forward slash "/", | ||
// but directory can be resolved dynamically via path.xxx methods | ||
// that return backslash on Windows. | ||
if (path.sep === '\\') pattern = toPosixPath(pattern); | ||
switch (true) { | ||
case pattern.endsWith('**'): | ||
return `${pattern}/*.${extension}`; | ||
case pattern.endsWith('*'): | ||
return `${pattern}.${extension}`; | ||
case path.extname(pattern) === '': | ||
return `${pattern.replace(/\/+$/, '')}/**/*.${extension}`; | ||
default: | ||
return pattern; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Feature: load steps | ||
|
||
Scenario: create todos | ||
When I create todo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"description": "This file is required for Playwright to consider this dir as a <package-json dir>. It ensures to load 'playwright-bdd' from './test/node_modules/playwright-bdd' and output './test-results' here to avoid conficts." | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { defineConfig } from '@playwright/test'; | ||
import { defineBddConfig } from 'playwright-bdd'; | ||
|
||
const testDir = defineBddConfig({ | ||
paths: ['features/*.feature'], | ||
steps: 'steps/*', | ||
}); | ||
|
||
export default defineConfig({ | ||
testDir, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { createBdd } from 'playwright-bdd'; | ||
|
||
const { When } = createBdd(); | ||
|
||
When('I create todo', async () => {}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { test, TestDir, execPlaywrightTest } from '../_helpers/index.mjs'; | ||
|
||
const testDir = new TestDir(import.meta); | ||
|
||
test(testDir.name, () => { | ||
execPlaywrightTest(testDir.name); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import path from 'node:path'; | ||
import { test, expect, TestDir } from '../_helpers/index.mjs'; | ||
|
||
const testDir = new TestDir(import.meta); | ||
|
||
test(`${testDir.name} (finalizePattern)`, async () => { | ||
const modulePath = path.resolve('dist/utils/paths.js'); | ||
const { finalizePattern } = await import(modulePath); | ||
|
||
expect(finalizePattern('a/steps', 'js')).toEqual('a/steps/**/*.js'); | ||
expect(finalizePattern('a/steps/', 'js')).toEqual('a/steps/**/*.js'); | ||
expect(finalizePattern('a/steps//', 'js')).toEqual('a/steps/**/*.js'); | ||
expect(finalizePattern('a/steps/**', 'js')).toEqual('a/steps/**/*.js'); | ||
expect(finalizePattern('a/steps/*', 'js')).toEqual('a/steps/*.js'); | ||
expect(finalizePattern('a/steps/*.js', 'js')).toEqual('a/steps/*.js'); | ||
expect(finalizePattern('a/steps/**/*.js', 'js')).toEqual('a/steps/**/*.js'); | ||
|
||
// backslash (on win) | ||
if (path.sep === '\\') { | ||
expect(finalizePattern('a\\steps', 'js')).toEqual('a/steps/**/*.js'); | ||
expect(finalizePattern('a\\steps\\**', 'js')).toEqual('a/steps/**/*.js'); | ||
} | ||
}); |