Skip to content

Commit

Permalink
test: fix test on win
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalets committed Mar 7, 2024
1 parent a704386 commit a4e200b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/gen/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
* Helper to format Playwright test file.
*/

import path from 'node:path';
import { PickleStepArgument } from '@cucumber/messages';
import { BDDConfig } from '../config';
import { jsStringWrap } from '../utils/jsStringWrap';
import { TestNode } from './testNode';
import { BddWorldFixtures } from '../run/bddWorld';
import { TestMetaBuilder } from './testMeta';
import { toPosixPath } from '../utils';

export type ImportTestFrom = {
file: string;
Expand All @@ -23,7 +23,7 @@ export class Formatter {
let varName = importTestFrom?.varName || 'test';
if (varName !== 'test') varName = `${varName} as test`;
// always use "/" for imports, see #91
const posixFilePath = file.split(path.sep).join(path.posix.sep);
const posixFilePath = toPosixPath(file);
return [
`/** Generated from: ${uri} */`, // prettier-ignore
// this.quoted() is not possible for 'import from' as backticks not parsed
Expand Down Expand Up @@ -78,7 +78,7 @@ export class Formatter {
...[
'$test: ({}, use) => use(test),',
'$testMetaMap: ({}, use) => use(testMetaMap),',
`$uri: ({}, use) => use(${this.quoted(sourceFile)}),`,
`$uri: ({}, use) => use(${this.quoted(toPosixPath(sourceFile))}),`,
...fixtures,
].map(indent),
'});',
Expand Down
8 changes: 8 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,11 @@ export function omit<T extends object, K extends keyof T>(obj: T, key: K) {
delete res[key];
return res as Omit<T, K>;
}

/**
* 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);
}
4 changes: 3 additions & 1 deletion test/helpers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ export class TestDir {
}

getAbsPath(relativePath) {
return fileURLToPath(new URL(relativePath, this.importMeta.url));
return path.isAbsolute(relativePath)
? relativePath
: fileURLToPath(new URL(relativePath, this.importMeta.url));
}

clearDir(relativePath) {
Expand Down

0 comments on commit a4e200b

Please sign in to comment.