Skip to content

Commit

Permalink
chore: unitTest use ts
Browse files Browse the repository at this point in the history
  • Loading branch information
daolou committed Aug 6, 2020
1 parent 4d07a1d commit 6bf22bd
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 39 deletions.
31 changes: 18 additions & 13 deletions __tests__/basic.test.js → __tests__/basic.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
const { resolve } = require('path');
const webpack = require('webpack');
const rimraf = require('rimraf');
import { resolve } from 'path';
import webpack, { Configuration } from 'webpack';
import pkg from 'webpack/package.json';
import LogFilesizeWebpackPlugin from '../lib/index';
const baseConfig = require('./support/webpack.base.conf');

const webpackMajorVersion = Number(require('webpack/package.json').version.split('.')[0]);
const webpackMajorVersion = Number(pkg.version.split('.')[0]);

if (isNaN(webpackMajorVersion)) {
throw new Error('Cannot parse webpack major version');
}

const LogFilesizeWebpackPlugin = require('../lib/index');
const baseConfig = require('./support/webpack.base.conf');

const OUTPUT_DIR = resolve(__dirname, '../dist/basic-test');

const testLogFilesizePlugin = (webpackConfig, pluginInstance, done) => {
const testLogFilesizePlugin = (
webpackConfig: Configuration,
pluginInstance: any,
done: Function
) => {
// console.log(webpackConfig)
webpack(webpackConfig, (err, stats) => {
expect(err).toBeFalsy();
const compilationErrors = (stats.compilation.errors || []).join('\n');
Expand Down Expand Up @@ -49,17 +52,19 @@ describe('LogFilesizeWebpackPlugin', () => {
});

it('shoule be printfStats', (done) => {
const config = { ...baseConfig };
const config = { ...baseConfig } as Configuration;
// @ts-ignore
config.entry = {
index1: resolve(__dirname, './fixtures/index1.js'),
index2: resolve(__dirname, './fixtures/index2.js')
index1: resolve(__dirname, './fixtures/index1.tsx'),
index2: resolve(__dirname, './fixtures/index2.tsx')
};
// @ts-ignore
config.output = {
path: OUTPUT_DIR,
filename: 'js/[name].[chunkhash:6].js',
chunkFilename: 'js/[name].[chunkhash:6].js'
};
config.plugins.push(log);
config.plugins!.push(log);
testLogFilesizePlugin(config, log, done);
});
});
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { generateRandom } from '@/utils/index';
import { generateRandom } from '@/utils';

console.log('== Index2 ==', generateRandom(10));
24 changes: 24 additions & 0 deletions __tests__/fixtures/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"lib": ["ES2015", "ESNext", "DOM"],
"outDir": "lib",
"strict": true,
"moduleResolution": "node",
"declaration": true,
"baseUrl": ".",
"paths": {
"@/*": ["./*"] // fixtures 目录别名
},
"jsx": "react",
"resolveJsonModule": true,
"pretty": true,
"esModuleInterop": true,
"experimentalDecorators": true,
"skipLibCheck": true,
"removeComments": true
},
"include": ["./**/*", "typings.d.ts"],
"exclude": ["node_modules", "**/test/*", "**/*.spec.ts", "lib", "es", "dist", "build", "example"]
}
7 changes: 7 additions & 0 deletions __tests__/fixtures/typings.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
declare module '*.css';
declare module '*.less';
declare module '*.png';
declare module '*.jpg';
declare module '*.jpeg';
declare module '*.svg';
declare module '*.ttf';
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @param {number} min - The minimum value of the interval, the default is 0
* @param {bool} int - Whether it is an integer, the default is true, which is an integer
*/
export const generateRandom = (max, min = 0, int = true) => {
export const generateRandom = (max: number, min = 0, int = true) => {
min = Math.ceil(min);
max = Math.floor(max);
const res = Math.random() * (max - min + 1) + min;
Expand Down
25 changes: 19 additions & 6 deletions __tests__/support/webpack.base.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin');
const ManifestPlugin = require('webpack-manifest-plugin');

module.exports = {
const config = (module.exports = {
mode: 'development',
stats: 'none',
resolve: {
alias: {
'@': resolve(__dirname, '../fixtures')
}
},
extensions: ['.js', '.jsx', '.ts', '.tsx']
},
module: {
rules: [
{
test: /\.js(x?)$/,
test: /[\\.]js(x?)$/,
include: resolve(__dirname, '../fixtures'),
use: [
{
Expand Down Expand Up @@ -48,7 +49,19 @@ module.exports = {
]
},
{
test: /\.(le|c)ss$/,
test: /[\\.]ts(x?)$/,
include: resolve(__dirname, '../fixtures'),
use: [
{
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env', '@babel/preset-react', '@babel/preset-typescript']
}
}
]
},
{
test: /[\\.](le|c)ss$/,
include: resolve(__dirname, '../fixtures'),
use: [
MiniCssExtractPlugin.loader,
Expand All @@ -73,7 +86,7 @@ module.exports = {
]
},
{
test: /\.(png|jpg|jpeg|gif|svg|svga)$/,
test: /[\\.](png|jpg|jpeg|gif|svg|svga)$/,
include: resolve(__dirname, '../fixtures'),
use: [
{
Expand Down Expand Up @@ -134,4 +147,4 @@ module.exports = {
}),
new ManifestPlugin()
]
};
});
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// https://jestjs.io/docs/en/configuration.html

module.exports = {
preset: 'ts-jest',
displayName: {
name: 'LOG-FILESIZE',
color: 'blue'
Expand Down
27 changes: 9 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,7 @@
"name": "@jsany/log-filesize-webpack-plugin",
"version": "1.2.10",
"description": "log assets size after webpack build(wirte by typescript)",
"keywords": [
"log",
"assets",
"filesize",
"gzip",
"webpack",
"plugin"
],
"keywords": ["log", "assets", "filesize", "gzip", "webpack", "plugin"],
"homepage": "https://github.com/jsany/log-filesize-webpack-plugin#readme",
"bugs": {
"url": "https://github.com/jsany/log-filesize-webpack-plugin/issues"
Expand All @@ -22,9 +15,7 @@
"author": "jiangzhiguo2010",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"files": [
"lib"
],
"files": ["lib"],
"scripts": {
"build": "rimraf lib/* && ttsc",
"coverage": "jest --runInBand --coverage",
Expand All @@ -40,12 +31,8 @@
}
},
"lint-staged": {
"**/*.{js,ts,md,json}": [
"prettier --write"
],
"package.json": [
"sort-package-json"
]
"**/*.{js,ts,md,json}": ["prettier --write"],
"package.json": ["sort-package-json"]
},
"dependencies": {
"chalk": "^4.1.0",
Expand All @@ -61,11 +48,14 @@
"@babel/plugin-transform-runtime": "^7.10.5",
"@babel/preset-env": "^7.10.4",
"@babel/preset-react": "^7.10.4",
"@babel/preset-typescript": "^7.10.4",
"@commitlint/cli": "^9.1.1",
"@commitlint/config-conventional": "^9.1.1",
"@types/jest": "^26.0.4",
"@types/jest": "^26.0.9",
"@types/mime": "^2.0.2",
"@types/node": "^14.0.23",
"@types/react": "^16.9.44",
"@types/react-dom": "^16.9.8",
"@types/rimraf": "^3.0.0",
"@types/webpack": "^4.41.21",
"@typescript-eslint/eslint-plugin": "^3.6.1",
Expand All @@ -91,6 +81,7 @@
"rimraf": "^3.0.2",
"sort-package-json": "^1.44.0",
"standard-version": "^8.0.2",
"ts-jest": "^26.1.4",
"ttypescript": "^1.5.10",
"typescript": "^3.9.6",
"typescript-transform-paths": "^1.1.14",
Expand Down
2 changes: 2 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
"@@/*": ["../*"] // 根目录别名
},
"plugins": [{ "transform": "typescript-transform-paths" }],
"resolveJsonModule": true,
"allowSyntheticDefaultImports": true,
"pretty": true,
"esModuleInterop": true,
"experimentalDecorators": true,
Expand Down

0 comments on commit 6bf22bd

Please sign in to comment.