Skip to content

Commit f9c76d2

Browse files
committed
docs(types): fix typings in tests
1 parent 772451f commit f9c76d2

10 files changed

+54
-38
lines changed

__mocks__/node-notifier.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export const notify = jest.fn(() => void 0);
1+
export const notify: jest.Mock = jest.fn(() => void 0);

index.d.ts

+10-7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Definitions by: Benjamin Lim <https://github.com/bumbleblym>
44
// Piotr Błażejewicz <https://github.com/peterblazejewicz>
55
// Alexandre Germain <https://github.com/gerkindev>
6+
// Gvozdev Viktor <https://github.com/Gvozd>
67
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
78
// TypeScript Version: 3.9
89

@@ -14,18 +15,20 @@ declare const WebpackNotifierPlugin: {new (options?: WebpackNotifierPlugin.Optio
1415

1516
declare namespace WebpackNotifierPlugin {
1617
interface Options {
17-
alwaysNotify?: boolean | undefined;
18-
contentImage?: {[key in 'success' | 'warning' | 'error']: string} | string | undefined;
19-
excludeWarnings?: boolean | undefined;
20-
onlyOnError?: boolean | undefined;
21-
skipFirstNotification?: boolean | undefined;
22-
title?: string | undefined;
18+
alwaysNotify?: boolean;
19+
contentImage?: {[key in 'success' | 'warning' | 'error']: string} | string;
20+
excludeWarnings?: boolean;
21+
onlyOnError?: boolean;
22+
skipFirstNotification?: boolean;
23+
title?: string | TitleGetter;
2324
/**
2425
* Use emoji in notifications
2526
*/
26-
emoji?: boolean | undefined;
27+
emoji?: boolean;
2728
}
2829

2930
/** @deprecated use Options */
3031
type Config = Options;
32+
33+
type TitleGetter = (data: {msg: string,message: string,status: string}) => string;
3134
}

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
},
4242
"devDependencies": {
4343
"@types/jest": "^26.0.15",
44+
"@types/node-notifier": "^8.0.1",
45+
"@types/semver": "^7.3.8",
4446
"@types/webpack": "^4.41.31",
4547
"eslint": "^7.14.0",
4648
"eslint-config-airbnb-base": "^14.2.1",

test-d/index-tests.ts

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ const optionsArray: WebpackNotifierPlugin.Options[] = [
1010
skipFirstNotification: true,
1111
emoji: true,
1212
},
13+
{
14+
title: (data: {msg: string,message: string,status: string}) => 'Webpack',
15+
},
1316
];
1417

1518
const plugins: Plugin[] = optionsArray.map(options => new WebpackNotifierPlugin(options));

test/VerbosityLevelAllVariants.test.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import WebpackNotifierPlugin from '../';
12
import {contentImageSerializer, reduceArraySerializer, testChangesFlow} from './helpers/utils';
23

34
expect.addSnapshotSerializer(reduceArraySerializer);
@@ -8,7 +9,7 @@ describe('VerbosityLevelAllVariants', () => {
89
test.each([...generateSteps(opts)])('%j', testChangesFlow);
910
});
1011

11-
function* generateOptions() {
12+
function* generateOptions(): Generator<WebpackNotifierPlugin.Options> {
1213
for (const excludeWarnings of [false, true]) {
1314
for (const alwaysNotify of [false, true]) {
1415
for (const onlyOnError of [false, true]) {
@@ -19,7 +20,7 @@ describe('VerbosityLevelAllVariants', () => {
1920
}
2021
}
2122
}
22-
function* generateSteps(opts) {
23+
function* generateSteps(opts: WebpackNotifierPlugin.Options): Generator<[string[], WebpackNotifierPlugin.Options]> {
2324
for (const firsStep of ['successful', 'warning', 'error']) {
2425
yield [[firsStep], opts];
2526
for (const secondStep of ['successful', 'warning', 'error']) {

test/WebpackNotifierPlugin.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('WebpackNotifierPlugin', () => {
2121
[['error'], {title}],
2222
[['warning'], {title}],
2323
])('%j %j', testChangesFlow);
24-
function title({msg}) {
24+
function title({msg}: {msg: string}) {
2525
if (msg.startsWith('Error')) return 'build error ❌';
2626
if (msg.startsWith('Warning')) return 'build warning ⚠️';
2727
return 'build complete ✅';
@@ -33,7 +33,7 @@ describe('WebpackNotifierPlugin', () => {
3333
[['error'], {title}],
3434
[['warning'], {title}],
3535
])('%j %j', testChangesFlow);
36-
function title(params) {
36+
function title(params: {status: string, message: string}) {
3737
return `Build status is ${params.status} with message ${params.message}`;
3838
}
3939
});

test/helpers/ChildCompilationPlugin.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {Compiler} from 'webpack';
12
export default class ChildCompilationPlugin {
23
private levelCollectionKey: string;
34

@@ -7,19 +8,19 @@ export default class ChildCompilationPlugin {
78
}
89
}
910

10-
apply(compiler) {
11+
apply(compiler: Compiler) {
1112
if ('hooks' in compiler) {
1213
compiler.hooks.thisCompilation.tap('ChildCompilationPlugin', this.handleHook.bind(this));
1314
} else {
14-
compiler.plugin('this-compilation', this.handleHook.bind(this));
15+
(compiler as any).plugin('this-compilation', this.handleHook.bind(this));
1516
}
1617
}
1718

18-
handleHook(compilation) {
19-
const childCompiler = compilation.createChildCompiler(`CHILD COMPILATION`);
20-
childCompiler.runAsChild((err, entries, compilation) => {
19+
handleHook(compilation: any) {
20+
const childCompiler = (compilation as any).createChildCompiler(`CHILD COMPILATION`);
21+
childCompiler.runAsChild((err: Error, entries: any, compilation: any) => {
2122
if (this.level) {
22-
compilation[this.levelCollectionKey]
23+
(compilation as any)[this.levelCollectionKey]
2324
.push(new Error(`Child Compilation ${this.level}`));
2425
}
2526
});

test/helpers/CustomWarningPlugin.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1+
import {Compiler} from 'webpack';
12
export default class CustomWarningPlugin {
2-
apply(compiler) {
3+
apply(compiler: Compiler) {
34
if ('hooks' in compiler) {
45
compiler.hooks.shouldEmit.tap('CustomWarningPlugin', this.handleHook);
56
} else {
6-
compiler.plugin('should-emit', this.handleHook);
7+
(compiler as any).plugin('should-emit', this.handleHook);
78
}
89
}
9-
handleHook(compilation) {
10-
compilation.warnings.push(Object.assign(
10+
handleHook(compilation: any) {
11+
(compilation.warnings as Error[]).push(Object.assign(
1112
new Error('Custom Warning'),
1213
{warning: 'Custom Warning message'}
1314
));
15+
return false;
1416
}
1517
};

test/helpers/utils.ts

+16-16
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import {createFsFromVolume, Volume} from 'memfs';
55
import {notify} from 'node-notifier';
66
import {satisfies} from 'semver';
77
import fixtures from './fixtures';
8-
import WebpackNotifierPlugin from '../../';
8+
import WebpackNotifierPlugin, {Options} from '../../';
99

10-
function getCompiler(compilerOpts) {
11-
const config = {
10+
function getCompiler(compilerOpts: webpack.Configuration): webpack.Compiler {
11+
const config: webpack.Configuration = {
1212
entry: '/entry.js',
1313
output: {
1414
path: '/',
@@ -22,20 +22,20 @@ function getCompiler(compilerOpts) {
2222

2323
return webpack(config);
2424
}
25-
function patchCompiler(compiler, fs) {
25+
function patchCompiler(compiler: webpack.Compiler, fs: any) {
2626
compiler.inputFileSystem = fs;
2727
compiler.outputFileSystem = fs;
2828
// compiler.watchFileSystem = fs;
2929

3030
if (satisfies(webpackVersion, '<5')) {
31-
compiler['resolvers'].normal.fileSystem = fs;
31+
(compiler as any)['resolvers'].normal.fileSystem = fs;
3232
// compiler['resolvers'].loader.fileSystem = fs;
3333
// compiler['resolvers'].context.fileSystem = fs;
3434
}
3535
}
36-
async function compile(compiler) {
36+
async function compile(compiler: webpack.Compiler): Promise<webpack.Stats> {
3737
return new Promise((resolve, reject) => {
38-
compiler.run((err, res) => {
38+
compiler.run((err?: Error, res?: webpack.Stats) => {
3939
if (err) {
4040
return reject(err);
4141
}
@@ -49,18 +49,18 @@ function prepareFs() {
4949
const fs = createFsFromVolume(vol);
5050

5151
if (satisfies(webpackVersion, '<5')) {
52-
fs['join'] = join;
52+
(fs as any)['join'] = join;
5353
}
5454

5555
return {fs, vol};
5656
}
5757

58-
function updateFs(vol, json) {
58+
function updateFs(vol: any, json: any) {
5959
vol.reset();
6060
vol.fromJSON(json, '/');
6161
}
6262

63-
export const reduceArraySerializer = {
63+
export const reduceArraySerializer: jest.SnapshotSerializerPlugin = {
6464
test(val) {
6565
return Array.isArray(val) &&
6666
val.length === 1 &&
@@ -73,7 +73,7 @@ export const reduceArraySerializer = {
7373
return printer(val[0][0], config, indentation, depth, refs);
7474
},
7575
};
76-
export const contentImageSerializer = {
76+
export const contentImageSerializer: jest.SnapshotSerializerPlugin = {
7777
test(val) {
7878
return typeof val === 'object' &&
7979
val.contentImage &&
@@ -90,7 +90,7 @@ export const contentImageSerializer = {
9090
};
9191

9292
export type Sources = string[];
93-
export type PluginOptions = {} | undefined;
93+
export type PluginOptions = Options | undefined;
9494
export type CompilerOptions = {};
9595
export type TestArguments = [Sources, PluginOptions, CompilerOptions?];
9696

@@ -99,17 +99,17 @@ export function testChangesFlow(...args: TestArguments) {
9999
return runTest(...args);
100100
};
101101

102-
async function runTest(sources, opts, compilerOpts = {}) {
102+
async function runTest(sources: Sources, opts: PluginOptions, compilerOpts = {}) {
103103
const compiler = getCompiler(compilerOpts);
104104
const {fs, vol} = prepareFs();
105105
const plugin = new WebpackNotifierPlugin(opts);
106106
plugin.apply(compiler);
107107
patchCompiler(compiler, fs);
108108

109109
for (const name of sources) {
110-
notify.mockClear();
111-
updateFs(vol, fixtures.simple[name]);
110+
(notify as jest.Mock).mockClear();
111+
updateFs(vol, (fixtures.simple as any)[name]);
112112
await compile(compiler);
113-
expect(notify.mock.calls).toMatchSnapshot(`after "${name}" build`);
113+
expect((notify as jest.Mock).mock.calls).toMatchSnapshot(`after "${name}" build`);
114114
}
115115
}

tsconfig.json

+4
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,9 @@
33
"esModuleInterop": true,
44
"resolveJsonModule": true,
55
"downlevelIteration": true,
6+
"noImplicitAny": true,
7+
"noImplicitThis": true,
8+
"strictNullChecks": true,
9+
"strictFunctionTypes": true
610
}
711
}

0 commit comments

Comments
 (0)