From dc07d829636b01b14d57d3205aeecc6394aff145 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Sat, 26 Oct 2024 11:01:32 +0100 Subject: [PATCH] chore: convert tests to typescript --- packages/beasties-webpack-plugin/package.json | 2 + ...{index.test.js.snap => index.test.ts.snap} | 0 ...e.test.js.snap => standalone.test.ts.snap} | 0 .../test/{_helpers.js => helpers.ts} | 36 +++++++------ .../test/{index.test.js => index.test.ts} | 52 +++++-------------- ...{standalone.test.js => standalone.test.ts} | 12 +++-- pnpm-lock.yaml | 20 +++++++ 7 files changed, 61 insertions(+), 61 deletions(-) rename packages/beasties-webpack-plugin/test/__snapshots__/{index.test.js.snap => index.test.ts.snap} (100%) rename packages/beasties-webpack-plugin/test/__snapshots__/{standalone.test.js.snap => standalone.test.ts.snap} (100%) rename packages/beasties-webpack-plugin/test/{_helpers.js => helpers.ts} (75%) rename packages/beasties-webpack-plugin/test/{index.test.js => index.test.ts} (81%) rename packages/beasties-webpack-plugin/test/{standalone.test.js => standalone.test.ts} (82%) diff --git a/packages/beasties-webpack-plugin/package.json b/packages/beasties-webpack-plugin/package.json index bb9d90f..240bd34 100644 --- a/packages/beasties-webpack-plugin/package.json +++ b/packages/beasties-webpack-plugin/package.json @@ -64,6 +64,8 @@ "webpack-sources": "^1.4.3" }, "devDependencies": { + "@types/jsdom": "21.1.7", + "@types/webpack": "4.41.40", "css-loader": "4.3.0", "documentation": "14.0.3", "file-loader": "6.2.0", diff --git a/packages/beasties-webpack-plugin/test/__snapshots__/index.test.js.snap b/packages/beasties-webpack-plugin/test/__snapshots__/index.test.ts.snap similarity index 100% rename from packages/beasties-webpack-plugin/test/__snapshots__/index.test.js.snap rename to packages/beasties-webpack-plugin/test/__snapshots__/index.test.ts.snap diff --git a/packages/beasties-webpack-plugin/test/__snapshots__/standalone.test.js.snap b/packages/beasties-webpack-plugin/test/__snapshots__/standalone.test.ts.snap similarity index 100% rename from packages/beasties-webpack-plugin/test/__snapshots__/standalone.test.js.snap rename to packages/beasties-webpack-plugin/test/__snapshots__/standalone.test.ts.snap diff --git a/packages/beasties-webpack-plugin/test/_helpers.js b/packages/beasties-webpack-plugin/test/helpers.ts similarity index 75% rename from packages/beasties-webpack-plugin/test/_helpers.js rename to packages/beasties-webpack-plugin/test/helpers.ts index e814e09..5369684 100644 --- a/packages/beasties-webpack-plugin/test/_helpers.js +++ b/packages/beasties-webpack-plugin/test/helpers.ts @@ -14,6 +14,8 @@ * the License. */ +import type { Options } from 'beasties' + import fs from 'node:fs' import path from 'node:path' import { fileURLToPath } from 'node:url' @@ -22,28 +24,28 @@ import { promisify } from 'node:util' import { JSDOM } from 'jsdom' import webpack from 'webpack' -import BeastiesWebpackPlugin from '../src/index.js' +import BeastiesWebpackPlugin from '../src/index' const cwd = fileURLToPath(new URL('.', import.meta.url)) const { window } = new JSDOM() // parse a string into a JSDOM Document -export function parseDom(html) { +export function parseDom(html: string) { return new window.DOMParser().parseFromString(html, 'text/html') } // returns a promise resolving to the contents of a file -export function readFile(file) { +export function readFile(file: string) { return promisify(fs.readFile)(path.resolve(cwd, file), 'utf-8') } // invoke webpack on a given entry module, optionally mutating the default configuration -export function compile(entry, configDecorator) { - return new Promise((resolve, reject) => { +export function compile(entry: string, configDecorator: (config: webpack.Configuration) => webpack.Configuration | void) { + return new Promise((resolve, reject) => { const context = path.dirname(path.resolve(cwd, entry)) entry = path.basename(entry) - let config = { + let config: webpack.Configuration = { context, entry: path.resolve(context, entry), output: { @@ -71,9 +73,9 @@ export function compile(entry, configDecorator) { webpack(config, (err, stats) => { if (err) return reject(err) - const info = stats.toJson() - if (stats.hasErrors()) - return reject(info.errors.join('\n')) + const info = stats!.toJson() + if (stats?.hasErrors()) + return reject(info.errors?.join('\n')) resolve(info) }) }) @@ -81,13 +83,13 @@ export function compile(entry, configDecorator) { // invoke webpack via compile(), applying Beasties to inline CSS and injecting `html` and `document` properties into the webpack build info. export async function compileToHtml( - fixture, - configDecorator, - beastiesOptions = {}, + fixture: string, + configDecorator: (config: webpack.Configuration) => webpack.Configuration | void, + beastiesOptions: Options = {}, ) { const info = await compile(`fixtures/${fixture}/index.js`, (config) => { config = configDecorator(config) || config - config.plugins.push( + config.plugins!.push( new BeastiesWebpackPlugin({ pruneSource: true, compress: false, @@ -96,7 +98,9 @@ export async function compileToHtml( }), ) }) - info.html = await readFile(`fixtures/${fixture}/dist/index.html`) - info.document = parseDom(info.html) - return info + const html = await readFile(`fixtures/${fixture}/dist/index.html`) + return Object.assign(info, { + html, + document: parseDom(html), + }) } diff --git a/packages/beasties-webpack-plugin/test/index.test.js b/packages/beasties-webpack-plugin/test/index.test.ts similarity index 81% rename from packages/beasties-webpack-plugin/test/index.test.js rename to packages/beasties-webpack-plugin/test/index.test.ts index bee0a21..6dc1870 100644 --- a/packages/beasties-webpack-plugin/test/index.test.js +++ b/packages/beasties-webpack-plugin/test/index.test.ts @@ -14,18 +14,21 @@ * the License. */ +import type { Configuration } from 'webpack' import HtmlWebpackPlugin from 'html-webpack-plugin' +// @ts-expect-error missing types will provide when upgrading to webpack v5 import MiniCssExtractPlugin from 'mini-css-extract-plugin' import { beforeAll, describe, expect, it } from 'vitest' -import { compile, compileToHtml, readFile } from './_helpers.js' -function configure(config) { - config.module.rules.push({ +import { compile, compileToHtml, readFile } from './helpers' + +function configure(config: Configuration) { + config.module!.rules!.push({ test: /\.css$/, use: [MiniCssExtractPlugin.loader, 'css-loader'], }) - config.plugins.push( + config.plugins!.push( new MiniCssExtractPlugin({ filename: '[name].css', chunkFilename: '[name].chunk.css', @@ -62,12 +65,12 @@ describe('inline