Behaviour driven development for NestJS using jest and jest-cucumber.
$ npm install --save nestjs-bdd
- Create matchers
import { Match, TestingContext } from "nestjs-bdd"
import { Injectable } from "@nestjs/common"
@Injectable()
export class SumMatcher {
@Match(/^(.*) is (.*)$/)
defineNumber(context: TestingContext, name: string, value: string) {
context.setState(name, Number(value))
}
@Match(/^sum of (.*) and (.*) should be (.*)$/)
assertSum(context: TestingContext, a: string, b: string, sum: string) {
expect(context.getState(a) + context.getState(b)).toBe(Number(sum))
}
}
- Write features
Feature: Number Operations
Scenario: sum of two numbers
Given a is 5
And b is 10
Then sum of a and b should be 15
- Create BDD spec file
import { AppModule } from "./AppModule" // Your app module.
import { SumMatcher } from "./test/SumMatcher" // defined in step 1.
import { TestingApp } from "nestjs-bdd"
const app = new TestingApp(AppModule, [SumMatcher])
beforeAll(() => app.start())
app.findInDir("./features") // Scan recursively for *.feature files. (e.g. defined in step 2.)
afterAll(() => app.stop())
- Author - Rahul Kadyan
- Twitter - @znck0
NestJS BDD is MIT licensed.