From d1aa78fed65f460e0d5a1f02ec182900fb86fd91 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Sat, 21 Dec 2024 09:09:45 +0800 Subject: [PATCH] feat: support cjs and esm both by tshy BREAKING CHANGE: drop Node.js < 18.19.0 support part of https://github.com/eggjs/egg/issues/3644 https://github.com/eggjs/egg/issues/5257 --- .editorconfig | 13 ----- .eslintrc | 26 ++------- .github/workflows/node.js.yml | 35 ------------ .github/workflows/nodejs.yml | 16 ++++++ .github/workflows/pkg.pr.new.yml | 23 ++++++++ .github/workflows/release.yml | 13 +++++ .gitignore | 4 ++ LICENSE | 1 + README.md | 59 +++++++++++--------- ci/remove-deps-4-old-node.js | 22 -------- package.json | 94 +++++++++++++++++++++----------- {lib => src}/agent.js | 0 index.js => src/index.js | 0 {lib => src}/test.js | 0 tsconfig.json | 10 ++++ 15 files changed, 167 insertions(+), 149 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .github/workflows/node.js.yml create mode 100644 .github/workflows/nodejs.yml create mode 100644 .github/workflows/pkg.pr.new.yml create mode 100644 .github/workflows/release.yml delete mode 100644 ci/remove-deps-4-old-node.js rename {lib => src}/agent.js (100%) rename index.js => src/index.js (100%) rename {lib => src}/test.js (100%) create mode 100644 tsconfig.json diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 5cd5590f..00000000 --- a/.editorconfig +++ /dev/null @@ -1,13 +0,0 @@ -root = true - -[*] -charset = utf-8 -insert_final_newline = true -end_of_line = lf -trim_trailing_whitespace = true -indent_style = space -indent_size = 2 - -[*.{js,json}] -indent_size = 2 -indent_style = space diff --git a/.eslintrc b/.eslintrc index 6492a517..9bcdb468 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,24 +1,6 @@ { - "extends": "airbnb-base/legacy", - "env": { - "node": true, - "mocha": true - }, - "parserOptions": { - "ecmaVersion": 6 - }, - "rules": { - // disabled - disagree with airbnb - "func-names": [0], - "space-before-function-paren": [0], - "consistent-return": [0], - - // Disabled but may want to refactor code eventually - "no-use-before-define": [2, "nofunc"], - "no-underscore-dangle": [0], - - // IMHO, more sensible overrides to existing airbnb error definitions - "max-len": [2, 100, 4, {"ignoreComments": true, "ignoreUrls": true}], - "no-unused-expressions": [2, { "allowShortCircuit": true, "allowTernary": true }] - } + "extends": [ + "eslint-config-egg/typescript", + "eslint-config-egg/lib/rules/enforce-node-prefix" + ] } diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml deleted file mode 100644 index 183dea0f..00000000 --- a/.github/workflows/node.js.yml +++ /dev/null @@ -1,35 +0,0 @@ -# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node -# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions - -name: Node.js CI - -on: [push, pull_request] - -jobs: - build: - - runs-on: ubuntu-latest - - strategy: - matrix: - include: - - node-version: 14.x - - node-version: 16.x - - node-version: 18.x - - steps: - - uses: actions/checkout@v3 - - name: Use Node.js ${{ matrix.node-version }} - uses: actions/setup-node@v3 - with: - node-version: ${{ matrix.node-version }} - path: ~/.npm - key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/yarn.lock') }} - - name: Install Dependencies On Node ${{ matrix.node-version }} - run: yarn install - - run: npm test - - name: Coverage On Node ${{ matrix.node-version }} - run: - npm run coverage - - name: Upload coverage to Codecov - uses: codecov/codecov-action@v3 diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml new file mode 100644 index 00000000..9ed9cf2b --- /dev/null +++ b/.github/workflows/nodejs.yml @@ -0,0 +1,16 @@ +name: CI + +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + +jobs: + Job: + name: Node.js + uses: node-modules/github-actions/.github/workflows/node-test.yml@master + with: + version: '18.19.0, 18, 20, 22, 23' + secrets: + CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} diff --git a/.github/workflows/pkg.pr.new.yml b/.github/workflows/pkg.pr.new.yml new file mode 100644 index 00000000..bac3facc --- /dev/null +++ b/.github/workflows/pkg.pr.new.yml @@ -0,0 +1,23 @@ +name: Publish Any Commit +on: [push, pull_request] + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - run: corepack enable + - uses: actions/setup-node@v4 + with: + node-version: 20 + + - name: Install dependencies + run: npm install + + - name: Build + run: npm run prepublishOnly --if-present + + - run: npx pkg-pr-new publish diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 00000000..a2bf04a7 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,13 @@ +name: Release + +on: + push: + branches: [ master ] + +jobs: + release: + name: Node.js + uses: eggjs/github-actions/.github/workflows/node-release.yml@master + secrets: + NPM_TOKEN: ${{ secrets.NPM_TOKEN }} + GIT_TOKEN: ${{ secrets.GIT_TOKEN }} diff --git a/.gitignore b/.gitignore index c6c5ab13..1dbbee9e 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,7 @@ coverage # Files # ################### *.log +.tshy* +.eslintcache +dist +coverage diff --git a/LICENSE b/LICENSE index a7693b07..bf1000be 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,7 @@ (The MIT License) Copyright (c) 2014 TJ Holowaychuk +Copyright (c) 2024-present eggjs and the contributors. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the diff --git a/README.md b/README.md index 960b0f26..4b0d3cae 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,26 @@ -# [SuperTest](https://ladjs.github.io/superagent/) +# @eggjs/supertest +[![NPM version][npm-image]][npm-url] [![code coverage][coverage-badge]][coverage] -[![Build Status][travis-badge]][travis] -[![Dependencies][dependencies-badge]][dependencies] -[![PRs Welcome][prs-badge]][prs] +[![Node.js CI](https://github.com/eggjs/supertest/actions/workflows/nodejs.yml/badge.svg)](https://github.com/eggjs/supertest/actions/workflows/nodejs.yml) +[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](https://makeapullrequest.com) [![MIT License][license-badge]][license] +[![npm download][download-image]][download-url] +[![Node.js Version](https://img.shields.io/node/v/@eggjs/mock.svg?style=flat)](https://nodejs.org/en/download/) + +[npm-image]: https://img.shields.io/npm/v/@eggjs/supertest.svg?style=flat-square +[npm-url]: https://npmjs.org/package/@eggjs/supertest +[coverage-badge]: https://img.shields.io/codecov/c/github/eggjs/supertest.svg +[coverage]: https://codecov.io/gh/eggjs/supertest +[license-badge]: https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square +[license]: https://github.com/eggjs/supertest/blob/master/LICENSE +[download-image]: https://img.shields.io/npm/dm/@eggjs/supertest.svg?style=flat-square +[download-url]: https://npmjs.org/package/@eggjs/supertest > HTTP assertions made easy via [superagent](http://github.com/ladjs/superagent). Maintained for [Forward Email](https://github.com/forwardemail) and [Lad](https://github.com/ladjs). +> Forked for TypeScript friendly + +Document see [SuperTest](https://ladjs.github.io/superagent/) ## About @@ -18,7 +32,7 @@ HTTP, while still allowing you to drop down to the [lower-level API](https://lad Install SuperTest as an npm module and save it to your package.json file as a development dependency: ```bash -npm install supertest --save-dev +npm install @eggjs/supertest --save-dev ``` Once installed it can now be referenced by simply calling ```require('supertest');``` @@ -33,7 +47,7 @@ SuperTest works with any test framework, here is an example without using any test framework at all: ```js -const request = require('supertest'); +const { request } = require('@eggjs/supertest'); const express = require('express'); const app = express(); @@ -55,7 +69,7 @@ request(app) To enable http2 protocol, simply append an options to `request` or `request.agent`: ```js -const request = require('supertest'); +const { request } = require('@eggjs/supertest'); const express = require('express'); const app = express(); @@ -207,13 +221,13 @@ the same host you may simply re-assign the request variable with the initialization app or url, a new `Test` is created per `request.VERB()` call. ```js -request = request('http://localhost:5555'); +t = request('http://localhost:5555'); -request.get('/').expect(200, function(err){ +t.get('/').expect(200, function(err){ console.log(err); }); -request.get('/').expect('heya', function(err){ +t.get('/').expect('heya', function(err){ console.log(err); }); ``` @@ -221,7 +235,7 @@ request.get('/').expect('heya', function(err){ Here's an example with mocha that shows how to persist a request and its cookies: ```js -const request = require('supertest'); +const { agent } = require('@eggjs/supertest'); const should = require('should'); const express = require('express'); const cookieParser = require('cookie-parser'); @@ -240,16 +254,16 @@ describe('request.agent(app)', function() { else res.send(':(') }); - const agent = request.agent(app); + const testAgent = agent(app); it('should save cookies', function(done) { - agent + testAgent .get('/') .expect('set-cookie', 'cookie=hey; Path=/', done); }); it('should send cookies', function(done) { - agent + testAgent .get('/return') .expect('hey', done); }); @@ -324,15 +338,10 @@ Inspired by [api-easy](https://github.com/flatiron/api-easy) minus vows coupling ## License -MIT +[MIT](LICENSE) -[coverage-badge]: https://img.shields.io/codecov/c/github/ladjs/supertest.svg -[coverage]: https://codecov.io/gh/ladjs/supertest -[travis-badge]: https://travis-ci.org/ladjs/supertest.svg?branch=master -[travis]: https://travis-ci.org/ladjs/supertest -[dependencies-badge]: https://david-dm.org/ladjs/supertest/status.svg -[dependencies]: https://david-dm.org/ladjs/supertest -[prs-badge]: https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square -[prs]: http://makeapullrequest.com -[license-badge]: https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square -[license]: https://github.com/ladjs/supertest/blob/master/LICENSE +## Contributors + +[![Contributors](https://contrib.rocks/image?repo=eggjs/supertest)](https://github.com/eggjs/supertest/graphs/contributors) + +Made with [contributors-img](https://contrib.rocks). diff --git a/ci/remove-deps-4-old-node.js b/ci/remove-deps-4-old-node.js deleted file mode 100644 index ca1368b1..00000000 --- a/ci/remove-deps-4-old-node.js +++ /dev/null @@ -1,22 +0,0 @@ -const fs = require('fs'); -const path = require('path'); -const package = require('../package.json'); - -const UNSUPPORT_DEPS_4_OLD = { - 'eslint': undefined, - 'mocha': '6.x' -}; - -const deps = Object.keys(UNSUPPORT_DEPS_4_OLD); -for (const item in package.devDependencies) { - if (deps.includes(item)) { - package.devDependencies[item] = UNSUPPORT_DEPS_4_OLD[item]; - } -} - -delete package.scripts.lint; - -fs.writeFileSync( - path.join(__dirname, '../package.json'), - JSON.stringify(package, null, 2) -); diff --git a/package.json b/package.json index d4c6387a..bde65e47 100644 --- a/package.json +++ b/package.json @@ -1,53 +1,83 @@ { - "name": "supertest", + "name": "@eggjs/supertest", "description": "SuperAgent driven library for testing HTTP servers", "version": "7.0.0", + "publishConfig": { + "access": "public" + }, "author": "TJ Holowaychuk", - "contributors": [], + "keywords": [ + "bdd", + "http", + "request", + "superagent", + "tdd", + "test", + "testing" + ], + "license": "MIT", + "repository": { + "type": "git", + "url": "https://github.com/eggjs/supertest.git" + }, + "engines": { + "node": ">= 18.19.0" + }, "dependencies": { "methods": "^1.1.2", "superagent": "^9.0.1" }, "devDependencies": { + "@arethetypeswrong/cli": "^0.17.1", + "@eggjs/tsconfig": "1", + "@types/node": "22", + "@types/mocha": "10", + "egg-bin": "6", + "eslint": "8", + "eslint-config-egg": "14", + "tshy": "3", + "tshy-after": "1", + "typescript": "5", "body-parser": "^1.20.2", "cookie-parser": "^1.4.6", - "eslint": "^8.32.0", - "eslint-config-airbnb-base": "^15.0.0", - "eslint-plugin-import": "^2.27.5", "express": "^4.18.2", - "mocha": "^10.2.0", "nock": "^13.3.0", "nyc": "^15.1.0", "proxyquire": "^2.1.3", "should": "^13.2.3" }, - "engines": { - "node": ">=14.18.0" + "scripts": { + "lint": "eslint --cache src test --ext .ts", + "pretest": "npm run lint -- --fix && npm run prepublishOnly", + "test": "egg-bin test", + "preci": "npm run lint && npm run prepublishOnly && attw --pack", + "ci": "egg-bin cov", + "prepublishOnly": "tshy && tshy-after" + }, + "type": "module", + "tshy": { + "exports": { + ".": "./src/index.ts", + "./package.json": "./package.json" + } + }, + "exports": { + ".": { + "import": { + "types": "./dist/esm/index.d.ts", + "default": "./dist/esm/index.js" + }, + "require": { + "types": "./dist/commonjs/index.d.ts", + "default": "./dist/commonjs/index.js" + } + }, + "./package.json": "./package.json" }, "files": [ - "index.js", - "lib" + "dist", + "src" ], - "keywords": [ - "bdd", - "http", - "request", - "superagent", - "tdd", - "test", - "testing" - ], - "license": "MIT", - "main": "index.js", - "repository": { - "type": "git", - "url": "https://github.com/ladjs/supertest.git" - }, - "scripts": { - "coverage": "nyc report --reporter=text-lcov > coverage.lcov", - "lint": "eslint lib/**/*.js test/**/*.js index.js", - "lint:fix": "eslint --fix lib/**/*.js test/**/*.js index.js", - "pretest": "npm run lint --if-present", - "test": "nyc --reporter=html --reporter=text mocha --exit --require should --reporter spec --check-leaks" - } + "types": "./dist/commonjs/index.d.ts", + "main": "./dist/commonjs/index.js" } diff --git a/lib/agent.js b/src/agent.js similarity index 100% rename from lib/agent.js rename to src/agent.js diff --git a/index.js b/src/index.js similarity index 100% rename from index.js rename to src/index.js diff --git a/lib/test.js b/src/test.js similarity index 100% rename from lib/test.js rename to src/test.js diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 00000000..ff41b734 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,10 @@ +{ + "extends": "@eggjs/tsconfig", + "compilerOptions": { + "strict": true, + "noImplicitAny": true, + "target": "ES2022", + "module": "NodeNext", + "moduleResolution": "NodeNext" + } +}