Skip to content

Commit d6aee9d

Browse files
committed
chore: spin out test helpers
1 parent 9d226a3 commit d6aee9d

File tree

3 files changed

+40
-71
lines changed

3 files changed

+40
-71
lines changed

src/api/load_support_spec.ts

+1-35
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,9 @@
1-
import { IdGenerator } from '@cucumber/messages'
21
import { IRunEnvironment } from './types'
32
import path from 'path'
4-
import fs from 'mz/fs'
5-
import { reindent } from 'reindent-template-literals'
6-
import { PassThrough } from 'stream'
73
import { loadSupport } from './load_support'
84
import { loadConfiguration } from './load_configuration'
95
import { expect } from 'chai'
10-
11-
const newId = IdGenerator.uuid()
12-
13-
async function setupEnvironment(): Promise<Partial<IRunEnvironment>> {
14-
const cwd = path.join(__dirname, '..', '..', 'tmp', `loadSupport_${newId()}`)
15-
await fs.mkdir(path.join(cwd, 'features'), { recursive: true })
16-
await fs.writeFile(
17-
path.join(cwd, 'features', 'test.feature'),
18-
reindent(`Feature: test fixture
19-
Scenario: one
20-
Given a step
21-
Then another step`)
22-
)
23-
await fs.writeFile(
24-
path.join(cwd, 'features', 'steps.ts'),
25-
reindent(`import { Given, Then } from '../../../src'
26-
Given('a step', function () {})
27-
Then('another step', function () {})`)
28-
)
29-
await fs.writeFile(
30-
path.join(cwd, 'cucumber.mjs'),
31-
`export default {paths: ['features/test.feature'], requireModule: ['ts-node/register'], require: ['features/steps.ts']}`
32-
)
33-
const stdout = new PassThrough()
34-
return { cwd, stdout }
35-
}
36-
37-
async function teardownEnvironment(environment: IRunEnvironment) {
38-
await fs.rmdir(environment.cwd, { recursive: true })
39-
environment.stdout.end()
40-
}
6+
import { setupEnvironment, teardownEnvironment } from './test_helpers'
417

428
describe('loadSupport', () => {
439
let environment: IRunEnvironment

src/api/run_cucumber_spec.ts

+2-36
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,10 @@
1-
import { Envelope, TestStepResultStatus, IdGenerator } from '@cucumber/messages'
2-
import fs from 'mz/fs'
3-
import path from 'path'
4-
import { reindent } from 'reindent-template-literals'
5-
import { PassThrough } from 'stream'
1+
import { Envelope, TestStepResultStatus } from '@cucumber/messages'
62
import { expect } from 'chai'
73
import { runCucumber } from './run_cucumber'
84
import { IRunEnvironment } from './types'
95
import { loadSupport } from './load_support'
106
import { loadConfiguration } from './load_configuration'
11-
12-
const newId = IdGenerator.uuid()
13-
14-
async function setupEnvironment(): Promise<Partial<IRunEnvironment>> {
15-
const cwd = path.join(__dirname, '..', '..', 'tmp', `runCucumber_${newId()}`)
16-
await fs.mkdir(path.join(cwd, 'features'), { recursive: true })
17-
await fs.writeFile(
18-
path.join(cwd, 'features', 'test.feature'),
19-
reindent(`Feature: test fixture
20-
Scenario: one
21-
Given a step
22-
Then another step`)
23-
)
24-
await fs.writeFile(
25-
path.join(cwd, 'features', 'steps.ts'),
26-
reindent(`import { Given, Then } from '../../../src'
27-
Given('a step', function () {})
28-
Then('another step', function () {})`)
29-
)
30-
await fs.writeFile(
31-
path.join(cwd, 'cucumber.mjs'),
32-
`export default {paths: ['features/test.feature'], requireModule: ['ts-node/register'], require: ['features/steps.ts']}`
33-
)
34-
const stdout = new PassThrough()
35-
return { cwd, stdout }
36-
}
37-
38-
async function teardownEnvironment(environment: IRunEnvironment) {
39-
await fs.rmdir(environment.cwd, { recursive: true })
40-
environment.stdout.end()
41-
}
7+
import { setupEnvironment, teardownEnvironment } from './test_helpers'
428

439
describe('runCucumber', () => {
4410
describe('preloading support code', () => {

src/api/test_helpers.ts

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { IRunEnvironment } from "./types";
2+
import path from "path";
3+
import fs from "mz/fs";
4+
import { reindent } from "reindent-template-literals";
5+
import { PassThrough } from "stream";
6+
import { IdGenerator } from "@cucumber/messages";
7+
8+
const newId = IdGenerator.uuid()
9+
10+
export async function setupEnvironment(): Promise<Partial<IRunEnvironment>> {
11+
const cwd = path.join(__dirname, '..', '..', 'tmp', `api_${newId()}`)
12+
await fs.mkdir(path.join(cwd, 'features'), { recursive: true })
13+
await fs.writeFile(
14+
path.join(cwd, 'features', 'test.feature'),
15+
reindent(`Feature: test fixture
16+
Scenario: one
17+
Given a step
18+
Then another step`)
19+
)
20+
await fs.writeFile(
21+
path.join(cwd, 'features', 'steps.ts'),
22+
reindent(`import { Given, Then } from '../../../src'
23+
Given('a step', function () {})
24+
Then('another step', function () {})`)
25+
)
26+
await fs.writeFile(
27+
path.join(cwd, 'cucumber.mjs'),
28+
`export default {paths: ['features/test.feature'], requireModule: ['ts-node/register'], require: ['features/steps.ts']}`
29+
)
30+
const stdout = new PassThrough()
31+
return { cwd, stdout }
32+
}
33+
34+
export async function teardownEnvironment(environment: IRunEnvironment) {
35+
await fs.rmdir(environment.cwd, { recursive: true })
36+
environment.stdout.end()
37+
}

0 commit comments

Comments
 (0)