From e1d52d692ed2f5da321ec773186a8d46483c1dba Mon Sep 17 00:00:00 2001 From: chimurai <655241+chimurai@users.noreply.github.com> Date: Mon, 10 May 2021 20:06:55 +0200 Subject: [PATCH] ci(package.json): use make --- .editorconfig | 3 +++ .eslintignore | 1 + .github/workflows/ci.yml | 7 ++++-- Makefile | 53 ++++++++++++++++++++++++++++++++++++++++ README.md | 4 +-- package.json | 18 +++++--------- test/pkg/Makefile | 13 ++++++++++ test/pkg/README.md | 14 +++++++++++ test/pkg/app.js | 18 ++++++++++++++ test/pkg/app.spec.js | 14 +++++++++++ test/pkg/jest.config.js | 3 +++ test/pkg/package.json | 14 +++++++++++ 12 files changed, 146 insertions(+), 16 deletions(-) create mode 100644 Makefile create mode 100644 test/pkg/Makefile create mode 100644 test/pkg/README.md create mode 100644 test/pkg/app.js create mode 100644 test/pkg/app.spec.js create mode 100644 test/pkg/jest.config.js create mode 100644 test/pkg/package.json diff --git a/.editorconfig b/.editorconfig index 9d5a4919..b53bd5df 100644 --- a/.editorconfig +++ b/.editorconfig @@ -16,3 +16,6 @@ trim_trailing_whitespace = false # Matches the exact files either package.json or .travis.yml [{package.json,.travis.yml}] indent_size = 2 + +[Makefile] +indent_style = tab diff --git a/.eslintignore b/.eslintignore index a261f291..b4c156d4 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1 +1,2 @@ dist/* +test/pkg/* diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 16a941f6..107044b5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -59,8 +59,11 @@ jobs: if: steps.yarn-cache.outputs.cache-hit != 'true' # Over here! run: yarn install --frozen-lockfile --ignore-scripts - - name: yarn test - run: yarn test + - name: make test-jest + run: make test-jest + + - name: make test-pkg + run: make test-pkg env: CI: true diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..c88a3ac8 --- /dev/null +++ b/Makefile @@ -0,0 +1,53 @@ +SHELL = /bin/sh + +YARN = yarn +PRETTIER = $(YARN) prettier +ESLINT = $(YARN) eslint +JEST = $(YARN) test +TOUCH = touch + +ARTIFACTS = dist coverage +SRC_TS := $(wildcard ./src/*.ts) +ESLINT_GLOB = "{src,test}/**/*.ts" +PRETTIER_GLOB = "**/*.{js,ts,md,yml,json,html}" + +.DEFAULT_TARGET = all + +.PHONY: clean coverage build lint lint-fix production test + +all: production + +install: node_modules + +node_modules: yarn.lock + $(YARN) install + $(TOUCH) $@ + +production: install clean build + rm dist/tsconfig.tsbuildinfo + +clean: + rm -rf $(ARTIFACTS) + +build: dist + +dist: $(SRC_TS) + $(YARN) tsc + $(TOUCH) $@ + +lint: + $(ESLINT) $(ESLINT_GLOB) + $(PRETTIER) --list-different $(PRETTIER_GLOB) + +lint-fix: + $(ESLINT) --fix $(ESLINT_GLOB) + $(PRETTIER) --write $(PRETTIER_GLOB) + +test-jest: clean build + $(JEST) + +test-pkg: + (cd ./test/pkg && make clean install test) + +coverage: clean build + $(JEST) --coverage --coverageReporters=lcov diff --git a/README.md b/README.md index e214f656..73a5230b 100644 --- a/README.md +++ b/README.md @@ -582,10 +582,10 @@ $ yarn lint:fix $ yarn build # unit tests -$ yarn test +$ yarn build && yarn test # code coverage -$ yarn cover +$ yarn build && yarn coverage ``` ## Changelog diff --git a/package.json b/package.json index 23a32797..919bf31d 100644 --- a/package.json +++ b/package.json @@ -8,20 +8,14 @@ "dist" ], "scripts": { - "clean": "rm -rf dist && rm -rf coverage", - "lint": "yarn prettier && yarn eslint", - "lint:fix": "yarn prettier:fix && yarn eslint:fix", - "eslint": "eslint '{src,test}/**/*.ts'", - "eslint:fix": "yarn eslint --fix", - "prettier": "prettier --list-different \"**/*.{js,ts,md,yml,json,html}\"", - "prettier:fix": "prettier --write \"**/*.{js,ts,md,yml,json,html}\"", - "prebuild": "yarn clean", + "clean": "make clean", + "lint": "make lint", + "lint:fix": "make lint-fix", "build": "tsc", - "pretest": "yarn build", "test": "jest", - "precoverage": "yarn build", - "coverage": "jest --coverage --coverageReporters=lcov", - "prepare": "husky install && yarn build && rm dist/tsconfig.tsbuildinfo" + "coverage": "make coverage", + "prepare": "husky install", + "prepack": "make production" }, "repository": { "type": "git", diff --git a/test/pkg/Makefile b/test/pkg/Makefile new file mode 100644 index 00000000..c1c878c8 --- /dev/null +++ b/test/pkg/Makefile @@ -0,0 +1,13 @@ +.PHONY: clean install test + +clean: + rm -rf node_modules http-proxy-middleware.tgz + +install: + (cd ../.. && make install) + (cd ../.. && npm pack) + (mv ../../http-proxy-middleware-*.tgz ./http-proxy-middleware.tgz) + yarn install + +test: + yarn test diff --git a/test/pkg/README.md b/test/pkg/README.md new file mode 100644 index 00000000..19ef6273 --- /dev/null +++ b/test/pkg/README.md @@ -0,0 +1,14 @@ +# Why this test is needed + +Testing purely with TypeScript doesn't cover this issue: https://github.com/Microsoft/TypeScript/wiki/'this'-in-TypeScript#red-flags-for-this + +https://github.com/chimurai/http-proxy-middleware/blob/c935888ea7135365bea3c4c81e4ffe48f359a670/src/http-proxy-middleware.ts#L45-L46 + +## npm package test + +```shell +make clean install test +``` + +Create `http-proxy-middleware.tgz` package; install and test it locally with a simple use-case to +test for the TypeScript red-flag issue. diff --git a/test/pkg/app.js b/test/pkg/app.js new file mode 100644 index 00000000..8d6606b9 --- /dev/null +++ b/test/pkg/app.js @@ -0,0 +1,18 @@ +// file deepcode ignore DisablePoweredBy: testing purpose only +// file deepcode ignore UseCsurfForExpress: testing purpose only + +const express = require('express'); +const { createProxyMiddleware } = require('http-proxy-middleware'); + +const app = express(); + +app.use( + createProxyMiddleware({ + target: 'https://jsonplaceholder.typicode.com', + changeOrigin: true, + }) +); + +module.exports = { + app, +}; diff --git a/test/pkg/app.spec.js b/test/pkg/app.spec.js new file mode 100644 index 00000000..a8b8d5ba --- /dev/null +++ b/test/pkg/app.spec.js @@ -0,0 +1,14 @@ +const request = require('supertest'); +const { app } = require('./app'); + +describe('package: http-proxy-middleware', () => { + let agent; + + beforeEach(() => { + agent = request(app); + }); + + it('should proxy /users', async () => { + return agent.get(`/users`).expect(200); + }); +}); diff --git a/test/pkg/jest.config.js b/test/pkg/jest.config.js new file mode 100644 index 00000000..25c9bac5 --- /dev/null +++ b/test/pkg/jest.config.js @@ -0,0 +1,3 @@ +module.exports = { + testEnvironment: 'node', +}; diff --git a/test/pkg/package.json b/test/pkg/package.json new file mode 100644 index 00000000..629562e3 --- /dev/null +++ b/test/pkg/package.json @@ -0,0 +1,14 @@ +{ + "name": "dist", + "private": "true", + "description": "http-proxy-middleware package test", + "main": "index.js", + "scripts": { + "test": "jest" + }, + "dependencies": { + "express": "^4.17.1", + "http-proxy-middleware": "file:http-proxy-middleware.tgz", + "jest": "^26.6.3" + } +}