Skip to content

Commit

Permalink
test: check TypeScript type definitions (#373)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 authored Nov 1, 2021
1 parent dd75ea3 commit 9c9c65b
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 21 deletions.
10 changes: 7 additions & 3 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ name: Node.js CI

on:
push:
branches: [ master ]
branches:
- main
- master
pull_request:
branches: [ master ]
branches:
- main
- master
schedule:
- cron: '0 2 * * *'

Expand All @@ -18,7 +22,7 @@ jobs:
strategy:
fail-fast: false
matrix:
node-version: [8, 10, 12, 14]
node-version: [8, 10, 12, 14, 16]
os: [ubuntu-latest, windows-latest, macos-latest]

steps:
Expand Down
8 changes: 1 addition & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
# urllib

[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![Build Status](https://dev.azure.com/eggjs/egg/_apis/build/status/node-modules.urllib)](https://dev.azure.com/eggjs/egg/_build/latest?definitionId=7)
[![Node.js CI](https://github.com/node-modules/urllib/actions/workflows/nodejs.yml/badge.svg)](https://github.com/node-modules/urllib/actions/workflows/nodejs.yml)
[![Test coverage][codecov-image]][codecov-url]
[![David deps][david-image]][david-url]
[![Known Vulnerabilities][snyk-image]][snyk-url]
[![npm download][download-image]][download-url]

[npm-image]: https://img.shields.io/npm/v/urllib.svg?style=flat-square
[npm-url]: https://npmjs.org/package/urllib
[travis-image]: https://img.shields.io/travis/node-modules/urllib.svg?style=flat-square
[travis-url]: https://travis-ci.org/node-modules/urllib
[codecov-image]: https://codecov.io/gh/node-modules/urllib/branch/master/graph/badge.svg
[codecov-url]: https://codecov.io/gh/node-modules/urllib
[david-image]: https://img.shields.io/david/node-modules/urllib.svg?style=flat-square
[david-url]: https://david-dm.org/node-modules/urllib
[snyk-image]: https://snyk.io/test/npm/urllib/badge.svg?style=flat-square
[snyk-url]: https://snyk.io/test/npm/urllib
[download-image]: https://img.shields.io/npm/dm/urllib.svg?style=flat-square
Expand Down
10 changes: 10 additions & 0 deletions lib/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { expectType } from 'tsd';
import { curl } from '..';

// curl
expectType<Buffer>((await curl<Buffer>('http://a.com')).data);
// RequestOptions
expectType<Buffer>((await curl<Buffer>('http://a.com', {})).data);
expectType<string>((await curl<string>('http://a.com', {
method: 'HEAD',
})).data);
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@
"url": "git://github.com/node-modules/urllib.git"
},
"scripts": {
"tsd": "node test/tsd.js",
"test-local": "mocha -t 30000 -r intelli-espower-loader test/*.test.js",
"test": "npm run lint && npm run test-local",
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- -t 30000 -r intelli-espower-loader test/*.test.js",
"ci": "npm run lint && npm run test-cov",
"ci": "npm run lint && npm run tsd && npm run test-cov",
"lint": "jshint .",
"autod": "autod -w --prefix '^' -t test -e examples",
"contributor": "git-contributor"
Expand Down Expand Up @@ -73,7 +74,8 @@
"spy": "^1.0.0",
"tar": "^4.4.8",
"through2": "^2.0.3",
"typescript": "^3.2.2"
"tsd": "^0.18.0",
"typescript": "^4.4.4"
},
"engines": {
"node": ">= 0.10.0"
Expand All @@ -83,7 +85,7 @@
"os": {
"github": "linux, windows, macos"
},
"version": "8, 10, 12, 14"
"version": "8, 10, 12, 14, 16"
},
"license": "MIT"
}
4 changes: 2 additions & 2 deletions test/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ module.exports = process.env.CI ? {
npmHttpRegistry: 'http://registry.npmjs.com',
} : {
npmWeb: 'https://www.npmjs.com',
npmRegistry: 'https://registry.npm.taobao.org',
npmHttpRegistry: 'http://registry.npm.taobao.org',
npmRegistry: 'https://registry.npmmirror.com',
npmHttpRegistry: 'http://registry.npmmirror.com',
};
9 changes: 4 additions & 5 deletions test/redirect.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ var urllib = require('../');

describe('test/redirect.test.js', function() {
it('should redirect `location: /package/pedding` with headers.Host', function(done) {
var url = 'https://r.cnpmjs.org/pedding/-/pedding-1.1.0.tgz';
var url = 'https://registry.npmmirror.com/pedding/-/pedding-1.1.0.tgz';
urllib.request(url, {
timeout: 30000,
headers: {
Host: 'r.cnpmjs.org',
Host: 'registry.npmmirror.com',
},
followRedirect: true,
}, function(err, data, res) {
Expand All @@ -24,8 +24,7 @@ describe('test/redirect.test.js', function() {
});

it('should redirect `location: http://other-domain` with headers.Host', function(done) {
// https://r.cnpmjs.org/pedding/download/pedding-1.0.0.tgz
var domain = 'cnpmjs.org';
var domain = 'npmjs.com';
dns.lookup(domain, function(err, address) {
if (err) {
return done(err);
Expand All @@ -48,7 +47,7 @@ describe('test/redirect.test.js', function() {
});

it('should use formatRedirectUrl', function(done) {
var url = 'https://cnpmjs.org/pedding';
var url = 'https://npmjs.com/pedding';
urllib.request(url, {
timeout: 30000,
followRedirect: true,
Expand Down
6 changes: 6 additions & 0 deletions test/tsd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const { execSync } = require('child_process');

// ignore node < 10, tsd not support them
if (!process.version.startsWith('v8.')) {
execSync('tsd');
}
4 changes: 3 additions & 1 deletion test/urllib.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ describe('test/urllib.test.js', function () {

if (semver.satisfies(process.version, '< 12.0.0')) {
// FXIME: not support rejectUnauthorized = false on Node.js >= 12.0.0
it('should request https with rejectUnauthorized:false success', function (done) {
it.skip('should request https with rejectUnauthorized:false success', function (done) {
urllib.request(config.npmRegistry + '/pedding/latest', {
timeout: 25000,
rejectUnauthorized: false,
Expand Down Expand Up @@ -924,6 +924,7 @@ describe('test/urllib.test.js', function () {
agent: agent,
httpsAgent: httpsAgent,
timeout: 25000,
followRedirect: true,
}, function (err, data, res) {
assert(!err);
assert(data instanceof Buffer);
Expand All @@ -934,6 +935,7 @@ describe('test/urllib.test.js', function () {
agent: agent,
httpsAgent: httpsAgent,
timeout: 1,
followRedirect: true,
}, function (err) {
assert(err);
assert(err.message.indexOf('(connected: true, keepalive socket: true, agent status: {"createSocketCount":') >= 0);
Expand Down
8 changes: 8 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"strict": true,
"target": "ES2017",
"module": "ESNext"
"moduleResolution": "Node"
}
}

0 comments on commit 9c9c65b

Please sign in to comment.