Added a nestJS based contract api

- Added an example with trucks and basic fetch in useEffect on page load
  - Added simply test to see whether any data is displayed (and shows the interceptor)
Introduced "CypressStrictMode" which wraps React.StrictMode and checks whether Cypress is involved, if so disable StrictMode.
This commit is contained in:
2022-08-15 11:42:19 +02:00
parent c0a0ea66a6
commit 3db77f96b9
31 changed files with 15567 additions and 35 deletions

View File

@@ -0,0 +1,66 @@
module.exports = {
parser: "@typescript-eslint/parser",
parserOptions: {
project: "tsconfig.json",
tsconfigRootDir: __dirname,
sourceType: "module",
},
plugins: ["@typescript-eslint/eslint-plugin", "import"],
extends: ["plugin:@typescript-eslint/recommended", "plugin:prettier/recommended"],
root: true,
env: {
node: true,
jest: true,
},
ignorePatterns: [".eslintrc.js"],
rules: {
"no-console": [
"error",
{
allow: ["debug", "error"],
},
],
"no-eval": "error",
"import/first": "error",
camelcase: [
"error",
{
ignoreImports: true,
ignoreDestructuring: true,
},
],
"consistent-return": "warn",
"comma-dangle": ["warn", "always-multiline"],
"constructor-super": "error",
curly: "error",
"eol-last": "warn",
eqeqeq: ["error", "smart"],
"import/order": "always",
"new-parens": "error",
"no-debugger": "error",
"no-fallthrough": "off",
"max-len": [
"warn",
{
code: 100,
},
],
"no-shadow": [
"error",
{
hoist: "all",
},
],
"no-trailing-spaces": "warn",
"no-underscore-dangle": "error",
"no-unsafe-finally": "error",
"no-var": "error",
"object-shorthand": "error",
"one-var": ["error", "never"],
"prefer-arrow/prefer-arrow-functions": "off",
"prefer-const": "error",
radix: "off",
"space-in-parens": ["off", "never"],
quotes: [2, "double"],
},
};

35
contracts/api/.gitignore vendored Normal file
View File

@@ -0,0 +1,35 @@
# compiled output
/dist
/node_modules
# Logs
logs
*.log
npm-debug.log*
pnpm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# OS
.DS_Store
# Tests
/coverage
/.nyc_output
# IDEs and editors
/.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace
# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json

11
contracts/api/README.md Normal file
View File

@@ -0,0 +1,11 @@
# Contract api
This API is meant to emulate the different contracts that you have with external systems
## use api based routing
You can use the first part of the URL as your contract and put your resources after that.
e.g, for `fake` you could do:
`URL/fake/trucks` and `URL/fake/containers`.
Then simply make `URL/fake` your root url in the React config

View File

@@ -0,0 +1,5 @@
{
"$schema": "https://json.schemastore.org/nest-cli",
"collection": "@nestjs/schematics",
"sourceRoot": "src"
}

14537
contracts/api/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,73 @@
{
"name": "contract-testing-api",
"version": "0.0.1",
"description": "",
"author": "",
"private": true,
"license": "UNLICENSED",
"scripts": {
"prebuild": "rimraf dist",
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json"
},
"dependencies": {
"@nestjs/common": "^9.0.0",
"@nestjs/core": "^9.0.0",
"@nestjs/platform-express": "^9.0.0",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^7.2.0"
},
"devDependencies": {
"@nestjs/cli": "^9.0.0",
"@nestjs/schematics": "^9.0.0",
"@nestjs/testing": "^9.0.0",
"@types/express": "^4.17.13",
"@types/jest": "28.1.4",
"@types/node": "^16.0.0",
"@types/supertest": "^2.0.11",
"@typescript-eslint/eslint-plugin": "^5.0.0",
"@typescript-eslint/parser": "^5.0.0",
"eslint": "^8.0.1",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^4.0.0",
"inforit-prettier-config": "^1.0.0",
"jest": "28.1.2",
"prettier": "^2.3.2",
"source-map-support": "^0.5.20",
"supertest": "^6.1.3",
"ts-jest": "28.0.5",
"ts-loader": "^9.2.3",
"ts-node": "^10.0.0",
"tsconfig-paths": "4.0.0",
"typescript": "^4.3.5"
},
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
},
"prettier": "inforit-prettier-config"
}

View File

@@ -0,0 +1,3 @@
export const API_CONSTANTS = {
fake: "fake",
};

View File

@@ -0,0 +1,9 @@
import { Controller, Get } from "@nestjs/common";
@Controller()
export class AppController {
@Get()
getHello(): string {
return "Welcome to the contract api";
}
}

View File

@@ -0,0 +1,10 @@
import { Module } from "@nestjs/common";
import { AppController } from "./app.controller";
import { FakeTrucksController } from "./contracts/fake/trucks.controller";
@Module({
imports: [],
controllers: [AppController, FakeTrucksController],
providers: [],
})
export class AppModule {}

View File

@@ -0,0 +1,21 @@
import { Controller, Get } from "@nestjs/common";
import { API_CONSTANTS } from "./../../api.constants";
@Controller(`${API_CONSTANTS.fake}/trucks`)
export class FakeTrucksController {
@Get()
get() {
return [
{
id: "de5ddb70-b2a7-4309-a992-62260a09683a",
licensePlate: "xx-yy-zz",
color: "black",
},
{
id: "087e0b0b-1c13-46e3-8920-762f5738072e",
licensePlate: "xx-yy-zz",
color: "red",
},
];
}
}

12
contracts/api/src/main.ts Normal file
View File

@@ -0,0 +1,12 @@
import { NestFactory } from "@nestjs/core";
import { AppModule } from "./app.module";
async function bootstrap() {
const app = await NestFactory.create(AppModule);
// allow app to be called from everywhere
app.enableCors();
// you can disable etags so that you always get 200's instead of 304s :)
// app.getHttpAdapter().getInstance().set("etag", false);
await app.listen(9600);
}
bootstrap();

View File

@@ -0,0 +1,4 @@
{
"extends": "./tsconfig.json",
"exclude": ["node_modules", "test", "dist", "**/*spec.ts"]
}

View File

@@ -0,0 +1,21 @@
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "es2017",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true,
"skipLibCheck": true,
"strictNullChecks": false,
"noImplicitAny": false,
"strictBindCallApply": false,
"forceConsistentCasingInFileNames": false,
"noFallthroughCasesInSwitch": false
}
}