Skip to content

Commit d655ee1

Browse files
authored
Support TypeScript user code (#24)
1 parent 99e3fbd commit d655ee1

File tree

7 files changed

+48
-9
lines changed

7 files changed

+48
-9
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

88
## [Unreleased]
9+
### Added
10+
- Support TypeScript user code ([#24](https://github.com/cucumber/cucumber-node/pull/24))
911

1012
## [0.1.0] - 2025-02-18
1113
### Added
1214
- Initial release
1315

1416
[Unreleased]: https://github.com/cucumber/cucumber-node/compare/0.1.0...HEAD
15-
[0.1.0]: https://github.com/cucumber/cucumber-node/compare/2d5d66e...0.1.0
17+
[0.1.0]: https://github.com/cucumber/cucumber-node/compare/2d5d66e...0.1.0

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,20 @@ When you write a step or hook function, the first argument will always be a [`Te
100100
Discovery of your code is based on the following glob (relative to the working directory):
101101

102102
```
103-
features/**/*.{cjs,js,mjs}
103+
features/**/*.{cjs,js,mjs,cts,mts,ts}
104104
```
105105

106106
This isn't configurable ([yet](https://github.com/cucumber/cucumber-node/issues/10)).
107107

108+
### TypeScript
109+
110+
You can write your code in TypeScript. Depending on your project, you might either:
111+
112+
1. Just rely on Node.js built-in type stripping without any other dependencies or configuration
113+
2. Use [tsx](https://www.npmjs.com/package/tsx) to transpile by adding `--import tsx` to your test command
114+
115+
See https://nodejs.org/api/typescript.html for more details.
116+
108117
## Reporters
109118

110119
Some Cucumber formatters are included as Node.js test reporters:

src/runner/loadSupport.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { builder } from './state.js'
66
import { SupportCodeLibrary } from './SupportCodeLibrary.js'
77

88
export async function loadSupport(): Promise<SupportCodeLibrary> {
9-
const paths = await globby('features/**/*.{cjs,js,mjs}')
9+
const paths = await globby('features/**/*.{cjs,js,mjs,cts,mts,ts}')
1010
for (const path of paths) {
1111
await import(pathToFileURL(path).toString())
1212
}

test/cck/cck.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ describe('Cucumber Compatibility Kit', () => {
5757
return this.skip()
5858
}
5959

60-
const harness = await makeTestHarness('cck')
60+
const harness = await makeTestHarness()
6161

6262
await harness.copyDir(path.join(
6363
process.cwd(),

test/integration/modules.spec.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { stripVTControlCharacters } from 'node:util'
2+
import { expect } from 'chai'
3+
import { makeTestHarness } from '../utils.js'
4+
5+
describe('Modules', () => {
6+
it('handles TypeScript code via type stripping', async () => {
7+
const harness = await makeTestHarness()
8+
await harness.writeFile(
9+
'features/first.feature',
10+
`Feature:
11+
Scenario:
12+
Given a step
13+
`
14+
)
15+
await harness.writeFile(
16+
'features/steps.ts',
17+
`import { Given } from '@cucumber/node'
18+
import type { TestCaseContext } from '@cucumber/node'
19+
Given('a step', (t: TestCaseContext) => {
20+
t.assert.strictEqual(2, 2)
21+
})
22+
`
23+
)
24+
const [output] = await harness.run('spec', '--experimental-strip-types')
25+
const sanitised = stripVTControlCharacters(output.trim())
26+
expect(sanitised).to.include('ℹ pass 2')
27+
})
28+
})

test/integration/reporters.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import path from 'node:path'
55

66
describe('Reporters', () => {
77
it('does not emit messages as diagnostics if no cucumber reporters', async () => {
8-
const harness = await makeTestHarness('reporters')
8+
const harness = await makeTestHarness()
99
await harness.writeFile(
1010
'features/first.feature',
1111
`Feature:
@@ -25,7 +25,7 @@ describe('Reporters', () => {
2525
})
2626

2727
it('provides a useful error for an ambiguous step', async () => {
28-
const harness = await makeTestHarness('reporters')
28+
const harness = await makeTestHarness()
2929
await harness.writeFile(
3030
'features/first.feature',
3131
`Feature:
@@ -44,7 +44,7 @@ Given('a step', () => {})`)
4444
})
4545

4646
it('provides a useful error for an undefined step', async () => {
47-
const harness = await makeTestHarness('reporters')
47+
const harness = await makeTestHarness()
4848
await harness.writeFile(
4949
'features/first.feature',
5050
`Feature:

test/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ class TestHarness {
5252
}
5353
}
5454

55-
export async function makeTestHarness(prefix: string) {
55+
export async function makeTestHarness() {
5656
// create temporary directory
57-
const tempDir = await mkdtemp(path.join(tmpdir(), `cucumber-node-${prefix}-`))
57+
const tempDir = await mkdtemp(path.join(tmpdir(), `cucumber-node-integration-`))
5858

5959
// symlink @cucumber/node package into node_modules
6060
await mkdir(path.join(tempDir, 'features'))

0 commit comments

Comments
 (0)