Skip to content

Commit 893c134

Browse files
committed
test: add REST steps tests
1 parent aedb97f commit 893c134

File tree

6 files changed

+119
-1
lines changed

6 files changed

+119
-1
lines changed

package-lock.json

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"@types/node": "20.12.7",
4040
"globstar": "1.0.0",
4141
"husky": "9.0.11",
42+
"nock": "^14.0.0-beta.5",
4243
"tsx": "4.7.2"
4344
},
4445
"lint-staged": {

src/REST.spec.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { describe, it, afterEach } from 'node:test'
2+
import assert from 'node:assert/strict'
3+
import { runFolder } from '@nordicsemiconductor/bdd-markdown'
4+
import path from 'node:path'
5+
import { fileURLToPath } from 'node:url'
6+
import { steps } from './REST.js'
7+
import nock from 'nock'
8+
const __dirname = fileURLToPath(new URL('.', import.meta.url))
9+
10+
void describe('REST', () => {
11+
void it('should implement a REST client', async () => {
12+
const scope = nock(`https://api.example.com`)
13+
.get('/foo', undefined, {
14+
reqheaders: {
15+
Accept: 'application/json',
16+
},
17+
})
18+
.reply(
19+
200,
20+
{
21+
'@context':
22+
'https://github.com/hello-nrfcloud/bdd-markdown-steps/tests',
23+
success: true,
24+
},
25+
{
26+
'content-type': 'application/json; charset=utf-8',
27+
},
28+
)
29+
30+
const runner = await runFolder({
31+
folder: path.join(__dirname, 'test-data', 'REST'),
32+
name: 'REST',
33+
})
34+
runner.addStepRunners(...steps)
35+
const ctx = {
36+
endpoint: `https://api.example.com`,
37+
}
38+
const result = await runner.run(ctx)
39+
assert.equal(result.ok, true)
40+
assert.equal(scope.isDone(), true)
41+
})
42+
43+
void afterEach(() => nock.cleanAll())
44+
})

src/lib/logObserver.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import type { LogObserver } from '@nordicsemiconductor/bdd-markdown'
2+
3+
const print = (arg: unknown) =>
4+
typeof arg === 'object' ? JSON.stringify(arg) : arg
5+
6+
export const logObserver: LogObserver = {
7+
onDebug: (info, ...args) =>
8+
console.error(
9+
info.step.keyword,
10+
info.step.title,
11+
...args.map((arg) => print(arg)),
12+
),
13+
onError: (info, ...args) =>
14+
console.error(
15+
info.step.keyword,
16+
info.step.title,
17+
...args.map((arg) => print(arg)),
18+
),
19+
onInfo: (info, ...args) =>
20+
console.error(
21+
info.step.keyword,
22+
info.step.title,
23+
...args.map((arg) => print(arg)),
24+
),
25+
onProgress: (info, ...args) =>
26+
console.error(
27+
info.step.keyword,
28+
info.step.title,
29+
...args.map((arg) => print(arg)),
30+
),
31+
}

src/random.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { steps } from './random.js'
77
const __dirname = fileURLToPath(new URL('.', import.meta.url))
88

99
void describe('Random', () => {
10-
void it('should store random in the context', async () => {
10+
void it('should store random strings in the context', async () => {
1111
const runner = await runFolder({
1212
folder: path.join(__dirname, 'test-data', 'random'),
1313
name: 'Random',

src/test-data/REST/REST.feature.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
exampleContext:
3+
endpoint: https://api.example.com/
4+
---
5+
6+
# REST steps
7+
8+
## Simple request
9+
10+
When I `GET` `${endpoint}/foo`
11+
12+
Then I should receive a
13+
`https://github.com/hello-nrfcloud/bdd-markdown-steps/tests` response

0 commit comments

Comments
 (0)