Skip to content

Commit

Permalink
Disable ESLint rule conflicting with Prettier (#55)
Browse files Browse the repository at this point in the history
* Move multi-workspace config to root

* Move outDir to package tsconfig.json

* Add Prettier ESLint config to disable rules

* Add Prettier scripts, format all files

* Ignore lib folders, format file
  • Loading branch information
karlhorky committed Oct 12, 2022
1 parent 4a6499b commit 051fe05
Show file tree
Hide file tree
Showing 13 changed files with 189 additions and 134 deletions.
8 changes: 2 additions & 6 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@ module.exports = {
node: true,
jest: true
},
extends: [
'standard'
],
extends: ['standard', 'prettier'],
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module'
},
plugins: [
'@typescript-eslint'
],
plugins: ['@typescript-eslint'],
rules: {},
ignorePatterns: ['**/lib/**/*.js']
}
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
coverage/
packages/*/lib/
package-lock.json
10 changes: 8 additions & 2 deletions examples/metarefresh-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import { IncomingMessage } from 'http'
import { Follow, Stop } from 'tall'

export async function metaRefreshPlugin (url: URL, response: IncomingMessage, previous: Follow | Stop): Promise<Follow | Stop> {
export async function metaRefreshPlugin(
url: URL,
response: IncomingMessage,
previous: Follow | Stop
): Promise<Follow | Stop> {
let html = ''
for await (const chunk of response) {
html += chunk.toString()
}

// note: this is actually not a great idea, it's best to use something like cheerio to parse HTML
// but for the sake of illustrating how to use plugins this is good enough here...
const metaHttpEquivUrl = html.match(/meta +http-equiv="refresh" +content="\d;url=(http[^"]+)"/)?.[1]
const metaHttpEquivUrl = html.match(
/meta +http-equiv="refresh" +content="\d;url=(http[^"]+)"/
)?.[1]

if (metaHttpEquivUrl) {
return new Follow(new URL(metaHttpEquivUrl))
Expand Down
30 changes: 13 additions & 17 deletions packages/tall/jest.config.cjs → jest.config.cjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
/*
* For a detailed explanation regarding each configuration property and type check, visit:
* https://jestjs.io/docs/en/configuration.html
*/

module.exports = {
displayName: 'tall',
const sharedConfig = {
// All imported modules in your tests should be mocked automatically
// automock: false,

Expand All @@ -21,7 +15,7 @@ module.exports = {
// collectCoverage: false,

// An array of glob patterns indicating a set of files for which coverage information should be collected
collectCoverageFrom: ['src/index.ts'],
collectCoverageFrom: ['packages/**/src/*.ts'],

// The directory where Jest should output its coverage files
coverageDirectory: 'coverage',
Expand All @@ -35,12 +29,7 @@ module.exports = {
coverageProvider: 'v8',

// A list of reporter names that Jest uses when writing coverage reports
coverageReporters: [
'json',
'text',
'lcov',
'clover'
],
coverageReporters: ['json', 'text', 'lcov', 'clover'],

// An object that configures minimum threshold enforcement for coverage results
// coverageThreshold: undefined,
Expand Down Expand Up @@ -118,9 +107,7 @@ module.exports = {
// rootDir: undefined,

// A list of paths to directories that Jest should use to search for files in
roots: [
'src'
],
// roots: ['src'],

// Allows you to use a custom runner instead of Jest's default test runner
// runner: 'jest-runner-tsc',
Expand Down Expand Up @@ -193,3 +180,12 @@ module.exports = {
// Whether to use watchman for file crawling
// watchman: true,
}

module.exports = {
projects: [
{
displayName: 'tall',
...sharedConfig
}
]
}
42 changes: 42 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
"packages/*"
],
"scripts": {
"test": "npm test --workspaces",
"format": "prettier --write ./**/*.{cjs,js,ts,json}",
"test:format": "prettier --list-different ./**/*.{cjs,js,ts,json}",
"test:lint": "eslint .",
"test:unit": "jest --coverage --verbose",
"test": "npm run test:format && npm run test:lint && npm run test:unit",
"build": "npm run build --if-present --workspaces",
"build:test": "npm run build:test --if-present --workspaces"
},
Expand All @@ -29,12 +33,14 @@
"@typescript-eslint/parser": "^5.12.0",
"codecov": "^3.8.1",
"eslint": "^8.25.0",
"eslint-config-prettier": "^8.5.0",
"eslint-config-standard": "^17.0.0",
"eslint-plugin-import": "^2.25.4",
"eslint-plugin-n": "^15.3.0",
"eslint-plugin-promise": "^6.0.1",
"jest": "^29.1.2",
"nock": "^13.0.6",
"prettier": "^2.7.1",
"ts-jest": "^29.0.3",
"ts-node": "^10.5.0",
"typescript": "^4.5.5"
Expand Down
5 changes: 1 addition & 4 deletions packages/tall/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@
"build": "tsc -p tsconfig.json && tsc -p tsconfig-cjs.json && echo '{\"type\":\"commonjs\"}' > lib/cjs/package.json",
"build:test": "node test/commonjs.cjs && node test/esm.js",
"prepublish": "npm run build",
"prepare": "npm run build",
"test:lint": "eslint .",
"test:unit": "jest --coverage --verbose",
"test": "npm run test:lint && npm run test:unit"
"prepare": "npm run build"
},
"repository": {
"type": "git",
Expand Down
57 changes: 28 additions & 29 deletions packages/tall/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IncomingMessage } from 'http'
import nock from 'nock'
import { Follow, locationHeaderPlugin, Stop, tall } from '.'
import { Follow, locationHeaderPlugin, Stop, tall } from './'

beforeEach(() => {
nock.cleanAll()
Expand All @@ -11,10 +11,7 @@ test('it should unshorten a link using https', async () => {
.get('/a-link')
.times(1)
.reply(301, 'Moved', { location: 'https://dest.pizza/a-link' })
nock('https://dest.pizza')
.get('/a-link')
.times(1)
.reply(200, 'OK')
nock('https://dest.pizza').get('/a-link').times(1).reply(200, 'OK')

const url = await tall('https://example.com/a-link')

Expand All @@ -26,10 +23,7 @@ test('it should unshorten a link using http', async () => {
.get('/a-link')
.times(1)
.reply(301, 'Moved', { location: 'http://dest.pizza/a-link' })
nock('http://dest.pizza')
.get('/a-link')
.times(1)
.reply(200, 'OK')
nock('http://dest.pizza').get('/a-link').times(1).reply(200, 'OK')

const url = await tall('http://example.com/a-link')

Expand All @@ -51,7 +45,9 @@ test('it should fail if the request times out', async () => {
.delay(1000)
.reply(200, 'OK')

await expect(() => tall('http://example.com/a-link', { timeout: 1 })).rejects.toThrow()
await expect(() =>
tall('http://example.com/a-link', { timeout: 1 })
).rejects.toThrow()
})

test('it should not fail if the request is within the timeout', async () => {
Expand All @@ -60,21 +56,15 @@ test('it should not fail if the request is within the timeout', async () => {
.times(1)
.delay(1)
.reply(301, 'Moved', { location: 'http://dest.pizza/a-link' })
nock('http://dest.pizza')
.get('/a-link')
.times(1)
.reply(200, 'OK')
nock('http://dest.pizza').get('/a-link').times(1).reply(200, 'OK')

const url = await tall('http://example.com/a-link', { timeout: 1000 })

expect(url).toBe('http://dest.pizza/a-link')
})

test('it should return the original url if no redirect', async () => {
nock('https://example.com')
.get('/test')
.times(1)
.reply(200, 'OK')
nock('https://example.com').get('/test').times(1).reply(200, 'OK')

const url = await tall('https://example.com/test')

Expand Down Expand Up @@ -133,10 +123,9 @@ test('it should allow to use a different method', async () => {
})

test('it should support redirects containing querystring parameters (see #17 and #19)', async () => {
nock('http://bit.ly')
.head('/fkWS88')
.times(1)
.reply(301, 'Moved', { location: 'http://news.ycombinator.com/item?id=2025354' })
nock('http://bit.ly').head('/fkWS88').times(1).reply(301, 'Moved', {
location: 'http://news.ycombinator.com/item?id=2025354'
})

nock('http://news.ycombinator.com')
.head('/item')
Expand All @@ -150,10 +139,9 @@ test('it should support redirects containing querystring parameters (see #17 and
})

test('The plugin chains behaves as expected', async () => {
nock('http://bit.ly')
.head('/fkWS88')
.times(1)
.reply(301, 'Moved', { location: 'http://news.ycombinator.com/item?id=2025354' })
nock('http://bit.ly').head('/fkWS88').times(1).reply(301, 'Moved', {
location: 'http://news.ycombinator.com/item?id=2025354'
})

nock('http://news.ycombinator.com')
.head('/item')
Expand All @@ -163,17 +151,28 @@ test('The plugin chains behaves as expected', async () => {

const pluginTrace: string[] = []

const plugin1 = async function plugin1 (url: URL, response: IncomingMessage, previous: Follow | Stop): Promise<Follow | Stop> {
const plugin1 = async function plugin1(
url: URL,
response: IncomingMessage,
previous: Follow | Stop
): Promise<Follow | Stop> {
pluginTrace.push(`plugin 1 ${url.toString()}`)
return previous
}

const plugin2 = async function plugin1 (url: URL, response: IncomingMessage, previous: Follow | Stop): Promise<Follow | Stop> {
const plugin2 = async function plugin1(
url: URL,
response: IncomingMessage,
previous: Follow | Stop
): Promise<Follow | Stop> {
pluginTrace.push(`plugin 2 ${url.toString()}`)
return previous
}

const url = await tall('http://bit.ly/fkWS88', { method: 'HEAD', plugins: [locationHeaderPlugin, plugin1, plugin2] })
const url = await tall('http://bit.ly/fkWS88', {
method: 'HEAD',
plugins: [locationHeaderPlugin, plugin1, plugin2]
})

expect(url).toBe('http://news.ycombinator.com/item?id=2025354')
expect(pluginTrace).toEqual([
Expand Down

0 comments on commit 051fe05

Please sign in to comment.