-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use Cucumber's JS API for tests (#14)
* update dependencies * make `run` an async function * update cucumber again * get everything passing * update node versions for testing * remove increased timeout * revert module interop flag
- Loading branch information
1 parent
21e4fc1
commit 646f2d5
Showing
20 changed files
with
977 additions
and
779 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -1,59 +1,64 @@ | ||
import { execFileSync } from 'child_process' | ||
import { | ||
IConfiguration, | ||
loadConfiguration, | ||
runCucumber, | ||
} from '@cucumber/cucumber/api' | ||
import * as glob from 'glob' | ||
import { join } from 'path' | ||
import { PassThrough } from 'stream' | ||
import * as streamToString from 'stream-to-string' | ||
import { promisify } from 'util' | ||
|
||
import { ThemeStyles } from '../src/theme' | ||
|
||
const cmd = 'node_modules/.bin/cucumber-js' | ||
|
||
type RunOptionalOptions = { | ||
'--name'?: string | ||
'--tags'?: string[] | ||
} | ||
type RunOptions = { | ||
colorsEnabled?: boolean | ||
theme?: Partial<ThemeStyles> | ||
type FormatOptions = { | ||
colorsEnabled: boolean | ||
theme: Partial<ThemeStyles> | ||
} | ||
type FinalRunOptions = RunOptionalOptions & Required<RunOptions> | ||
|
||
export const run = ( | ||
export const run = async ( | ||
fileName: string, | ||
options: RunOptions & RunOptionalOptions = {}, | ||
cucumberOptions: Partial<IConfiguration> = {}, | ||
formatOptions: Partial<FormatOptions> = {}, | ||
throws = false | ||
): string => { | ||
const { colorsEnabled, theme }: FinalRunOptions = { | ||
colorsEnabled: false, | ||
theme: {}, | ||
...options, | ||
} | ||
const args = [ | ||
'--publish-quiet', | ||
'--require', | ||
join(__dirname, 'features'), | ||
'--format', | ||
join(__dirname, '..', 'src'), | ||
'--format-options', | ||
JSON.stringify({ colorsEnabled, theme }), | ||
] | ||
if (options['--name']) args.push('--name', options['--name']) | ||
if (options['--tags']) args.push('--tags', options['--tags'].join(',')) | ||
|
||
return exec(throws, ...args, join('test', 'features', fileName)).replace( | ||
/\d+m\d+\.\d+s/g, | ||
'0m00.000s' | ||
) | ||
} | ||
|
||
const exec = (throws: boolean, ...args: string[]): string => { | ||
if (process.env.LOG_CUCUMBER_RUN) console.log(`${cmd} ${args.join(' ')}`) | ||
): Promise<string> => { | ||
// clear require cache for support code | ||
const matches = await promisify(glob)('features/support/*', { | ||
absolute: true, | ||
cwd: __dirname, | ||
}) | ||
matches.forEach((match) => delete require.cache[match]) | ||
|
||
let stdout: string | ||
const configuration: Partial<IConfiguration> = { | ||
...cucumberOptions, | ||
format: [join(__dirname, '..', 'src')], | ||
formatOptions: { | ||
colorsEnabled: false, | ||
theme: {}, | ||
...formatOptions, | ||
}, | ||
paths: [join('test', 'features', fileName)], | ||
publishQuiet: true, | ||
require: [join(__dirname, 'features')], | ||
} | ||
const { runConfiguration } = await loadConfiguration({ | ||
provided: configuration, | ||
}) | ||
const stdout = new PassThrough() | ||
const stderr = new PassThrough() | ||
try { | ||
stdout = execFileSync(cmd, args, { stdio: 'pipe' }).toString() | ||
} catch (error) { | ||
stdout = error.stdout.toString() | ||
if (throws) throw error | ||
await runCucumber(runConfiguration, { | ||
cwd: join(__dirname, '..', '..'), | ||
stdout, | ||
stderr, | ||
}) | ||
} catch (ex) { | ||
if (throws) { | ||
throw ex | ||
} | ||
} | ||
|
||
if (process.env.LOG_CUCUMBER_RUN) console.log(stdout) | ||
return stdout | ||
stdout.end() | ||
stderr.end() | ||
const result = await streamToString(stdout) | ||
return result.replace(/\d+m\d+\.\d+s/g, '0m00.000s') | ||
} |
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
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
Oops, something went wrong.