Skip to content

Commit

Permalink
Enable TypeScript strict mode (#356)
Browse files Browse the repository at this point in the history
  • Loading branch information
satyarohith authored and sindresorhus committed Sep 17, 2019
1 parent 72af166 commit af5540f
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 25 deletions.
17 changes: 14 additions & 3 deletions package.json
Expand Up @@ -53,15 +53,15 @@
"jpeg"
],
"dependencies": {
"array-differ": "^2.0.3",
"array-differ": "^2.1.0",
"array-uniq": "^2.0.0",
"capture-website": "^0.3.1",
"date-fns": "^1.30.1",
"filenamify": "^3.0.0",
"filenamify-url": "^1.0.0",
"get-res": "^3.0.0",
"lodash.template": "^4.0.1",
"log-symbols": "^2.1.0",
"log-symbols": "^3.0.0",
"make-dir": "^3.0.0",
"mem": "^4.3.0",
"plur": "^3.0.1",
Expand All @@ -70,10 +70,18 @@
},
"devDependencies": {
"@sindresorhus/tsconfig": "^0.3.0",
"@types/filenamify-url": "1.0.0",
"@types/cookie": "^0.3.2",
"@types/get-res": "^3.0.1",
"@types/image-size": "^0.7.0",
"@types/lodash.template": "^4.4.5",
"@types/node": "^11.13.0",
"@types/pify": "^3.0.2",
"@types/png.js": "^0.2.0",
"@types/sinon": "^7.0.6",
"@types/viewport-list": "^5.1.1",
"@typescript-eslint/eslint-plugin": "^1.3.0",
"@typescript-eslint/parser": "^2.2.0",
"ava": "^1.4.1",
"cookie": "^0.3.1",
"coveralls": "^3.0.0",
Expand All @@ -83,7 +91,7 @@
"get-port": "^4.1.0",
"image-size": "^0.7.1",
"nyc": "^13.2.0",
"path-exists": "^3.0.0",
"path-exists": "^4.0.0",
"pify": "^4.0.1",
"png.js": "^0.2.1",
"sinon": "^7.3.1",
Expand All @@ -107,6 +115,9 @@
"extensions": [
"ts"
],
"parserOptions": {
"project": "./test/tsconfig.json"
},
"rules": {
"import/named": "off",
"import/no-unresolved": "off",
Expand Down
6 changes: 4 additions & 2 deletions source/index.ts
Expand Up @@ -13,11 +13,13 @@ import makeDir from 'make-dir';
import captureWebsite from 'capture-website';
import viewportList from 'viewport-list';
import filenamify from 'filenamify';
import filenamifyUrl from 'filenamify-url';
import template from 'lodash.template';
import plur from 'plur';
import unusedFilename from 'unused-filename';
import * as _filenamifyUrl from 'filenamify-url';

// TODO: Update filenamifyUrl and fix the import after https://github.com/sindresorhus/filenamify-url/issues/4 is resolved.
const filenamifyUrl = _filenamifyUrl.default;
// TODO: Move this to `type-fest`
type Mutable<ObjectType> = {-readonly [KeyType in keyof ObjectType]: ObjectType[KeyType]};

Expand Down Expand Up @@ -115,7 +117,7 @@ export default class Pageres extends EventEmitter {
throw new TypeError('URL required');
}

if (!Array.isArray(sizes)) {
if (!(Array.isArray(sizes) && sizes.length > 0)) {
throw new TypeError('Sizes required');
}

Expand Down
8 changes: 4 additions & 4 deletions test/_server.ts
Expand Up @@ -7,14 +7,14 @@ import pify from 'pify';

export const host = 'localhost';

interface TestServer extends http.Server {
export interface TestServer extends http.Server {
host: string;
port: number;
url: string;
protocol: string;
}

const baseCreateServer = (fn): (() => Promise<TestServer>) => {
const baseCreateServer = (fn: http.RequestListener): (() => Promise<TestServer>) => {
return async (): Promise<TestServer> => {
const port = await getPort();
const server = http.createServer(fn) as TestServer;
Expand All @@ -24,7 +24,7 @@ const baseCreateServer = (fn): (() => Promise<TestServer>) => {
server.url = `http://${host}:${port}`;
server.protocol = 'http';
server.listen(port);
server.close = pify(server.close);
server.close = pify(server.close) as any;

return server;
};
Expand All @@ -36,7 +36,7 @@ export const createServer = baseCreateServer((_request, response) => {
});

export const createCookieServer = baseCreateServer((request, response) => {
const color = cookie.parse(request.headers.cookie).pageresColor || 'white';
const color = cookie.parse(String(request.headers.cookie)).pageresColor || 'white';
response.writeHead(200, {'content-type': 'text/html'});
response.end(`<body><div style="background: ${color}; position: absolute; top: 0; bottom: 0; left: 0; right: 0;"></div></body`);
});
8 changes: 6 additions & 2 deletions test/cookie.ts
@@ -1,11 +1,15 @@
import test from 'ava';
import test, {ExecutionContext} from 'ava';
// eslint-disable-next-line ava/no-import-test-files
import PNG from 'png.js';
import pify from 'pify';
import Pageres from '../source';
import {createCookieServer} from './_server';

async function cookieTest(input, t): Promise<void> {
interface Cookie {
[key: string]: string;
}

async function cookieTest(input: string | Cookie, t: ExecutionContext): Promise<void> {
const server = await createCookieServer();

const screenshots = await new Pageres({cookies: [input]})
Expand Down
28 changes: 18 additions & 10 deletions test/test.ts
Expand Up @@ -10,22 +10,22 @@ import pathExists from 'path-exists';
import sinon from 'sinon';
import fileType from 'file-type';
import Pageres, {Screenshot} from '../source';
import {createServer} from './_server';
import {createServer, TestServer} from './_server';

const fsP = pify(fs);

const hasScreenshotsWithFilenames = (screenshots: readonly Screenshot[], filenames: readonly string[]): boolean => {
return screenshots.some(screenshot => filenames.includes(screenshot.filename));
};

const getPngPixels = async (buffer): Promise<Buffer> => {
const getPngPixels = async (buffer: Buffer): Promise<Buffer> => {
const png = new PNG(buffer);
const {pixels} = await pify(png.parse.bind(png))();
return pixels;
};

let server;
let serverFileName;
let server: TestServer;
let serverFileName: string;
test.before(async () => {
server = await createServer();
serverFileName = server.url
Expand All @@ -52,15 +52,13 @@ test('set destination directory', t => {

test('`.src()` - error if no correct `url` is specified', t => {
t.throws(() => {
// @ts-ignore
new Pageres().src('');
new Pageres().src('', ['1280x1024', '1920x1080']);
}, 'URL required');
});

test('`.src()` - error if no `sizes` is specified', t => {
t.throws(() => {
// @ts-ignore
new Pageres().src(server.url);
new Pageres().src(server.url, []);
}, 'Sizes required');
});

Expand Down Expand Up @@ -246,12 +244,22 @@ test('`scale` option', async t => {

test('support data URL', async t => {
const screenshots = await new Pageres().src('data:text/html;base64,PGgxPkZPTzwvaDE+', ['100x100']).run();
t.is(fileType(screenshots[0]).mime, 'image/png');
const _fileType = fileType(screenshots[0]);
if (_fileType === null) {
throw new Error('Could not detect the file type');
}

t.is((_fileType).mime, 'image/png');
});

test('`format` option', async t => {
const screenshots = await new Pageres().src(server.url, ['100x100'], {format: 'jpg'}).run();
t.is(fileType(screenshots[0]).mime, 'image/jpeg');
const _fileType = fileType(screenshots[0]);
if (_fileType === null) {
throw new Error('Could not detect the file type');
}

t.is((_fileType).mime, 'image/jpeg');
});

test('when a file exists, append an incrementer', async t => {
Expand Down
3 changes: 3 additions & 0 deletions test/tsconfig.json
@@ -0,0 +1,3 @@
{
"extends": "@sindresorhus/tsconfig"
}
5 changes: 1 addition & 4 deletions tsconfig.json
Expand Up @@ -5,10 +5,7 @@
"target": "es2016",
"lib": [
"es2016"
],

// TODO: Remove this
"strict": false
]
},
"include": [
"source"
Expand Down

0 comments on commit af5540f

Please sign in to comment.