From ad9bee051a564e7fe6b1dd2e4f2a644198d8ae7c Mon Sep 17 00:00:00 2001 From: bdwenxi Date: Mon, 9 Nov 2020 16:53:56 +0800 Subject: [PATCH] refactor: add unit test configuration file and unit test code --- .gitignore | 1 + jest.config.js | 16 ++++++++ package.json | 51 ++++++++++++------------ src/__tests__/index.test.ts | 55 ++++++++++++++++++++++++++ src/index.ts | 19 ++++++--- tsconfig.json | 6 ++- yarn.lock | 78 +++++++++++++++++++++++++++++++++++++ 7 files changed, 196 insertions(+), 30 deletions(-) create mode 100644 jest.config.js diff --git a/.gitignore b/.gitignore index 3c5c71c..c090a24 100644 --- a/.gitignore +++ b/.gitignore @@ -76,3 +76,4 @@ Thumbs.db es/ cjs/ packages/doc/.docz +dist diff --git a/jest.config.js b/jest.config.js new file mode 100644 index 0000000..eb5c1f6 --- /dev/null +++ b/jest.config.js @@ -0,0 +1,16 @@ +const path = require('path'); + +module.exports = { + moduleDirectories: ['src', 'node_modules'], + moduleFileExtensions: ['js', 'ts', 'jsx', 'tsx', 'd.ts'], + transform: { + '^.+\\.(js|jsx|ts|tsx)$': path.resolve( + './node_modules/reskript/dist/config/jest/transformer' + ) + }, + coverageReporters: ['json-summary', 'lcov', 'text', 'clover'], + testMatch: ['**/__tests__/**/*.test.{js,jsx,ts,tsx}'], + collectCoverageFrom: ['/src/**/*.{js,jsx,ts,tsx}'], + coveragePathIgnorePatterns: ['/node_modules/', '/__tests__/'], + testEnvironment: 'jsdom' +}; diff --git a/package.json b/package.json index 16bc16f..83e6368 100644 --- a/package.json +++ b/package.json @@ -2,11 +2,9 @@ "name": "file2md5", "version": "1.0.0", "description": "file2md5 is a browser-side library based on SparkMD5, which supports typescript friendly.", - "main": "lib/index.js", - "module": "es/index.js", - "types": "es/index.d.ts", - "repository": "git@github.com:bdwenxi/file2md5.git", "homepage": "https://github.com/bdwenxi/file2md5", + "repository": "git@github.com:bdwenxi/file2md5.git", + "license": "MIT", "author": "bdwenxi ", "contributors": [ { @@ -14,38 +12,43 @@ "email": "bdwenxi@gmail.com" } ], - "scripts": { - "build": "rm -rf es cjs && tsc & tsc --module esnext --outDir ./es", - "build:check": "tsc", - "test": "skr test --coverage" - }, + "main": "lib/index.js", + "module": "es/index.js", + "types": "es/index.d.ts", "files": [ "src", "cjs", "es" ], - "publishConfig": { - "registry": "https://npm.pkg.github.com/" + "scripts": { + "build": "rm -rf es cjs && tsc & tsc --module esnext --outDir ./es", + "build:check": "tsc", + "test": "skr test --coverage" + }, + "husky": { + "hooks": { + "prepare-commit-msg": "exec < /dev/tty && git cz --hook || true" + } + }, + "config": { + "commitizen": { + "path": "./node_modules/cz-conventional-changelog" + } + }, + "dependencies": { + "lodash.noop": "^3.0.1", + "spark-md5": "^3.0.1" }, - "license": "MIT", "devDependencies": { + "@types/jest": "^26.0.15", + "@types/lodash.noop": "^3.0.6", "@types/spark-md5": "^3.0.2", "cz-conventional-changelog": "3.3.0", "husky": "^4.3.0", "reskript": "^0.26.7", "typescript": "^4.0.5" }, - "dependencies": { - "spark-md5": "^3.0.1" - }, - "config": { - "commitizen": { - "path": "./node_modules/cz-conventional-changelog" - } - }, - "husky": { - "hooks": { - "prepare-commit-msg": "exec < /dev/tty && git cz --hook || true" - } + "publishConfig": { + "registry": "https://npm.pkg.github.com/" } } diff --git a/src/__tests__/index.test.ts b/src/__tests__/index.test.ts index e69de29..1f72186 100644 --- a/src/__tests__/index.test.ts +++ b/src/__tests__/index.test.ts @@ -0,0 +1,55 @@ +import file2md5 from '../index'; + +jest.setTimeout(300000); + +describe('file2md5', () => { + test('test basic usage', async () => { + const file = new window.File([new ArrayBuffer(1024)], 'test.txt'); + + const md5 = await file2md5(file, {chunkSize: 3 * 1024 * 1024}); + expect(file).not.toBeNull(); + expect(typeof md5).toBe('string'); + expect(md5.length).toBeGreaterThan(0); + }); + + test('test "onProgress" callback', async () => { + const file = new window.File([new ArrayBuffer(1024)], 'test.txt'); + const onProgress = jest.fn(); + + await file2md5(file, {chunkSize: 3 * 1024 * 1024, onProgress}); + + expect(onProgress).toHaveBeenCalled(); + }); + + test('test "raw" property', async () => { + const file = new window.File([new ArrayBuffer(1024)], 'test.txt'); + const md5 = await file2md5(file, {chunkSize: 3 * 1024 * 1024, raw: true}); + + expect(typeof md5).toBe('string'); + expect(md5.length).toBeGreaterThan(0); + }); + + test('test huge file', async () => { + const file = new window.File([new ArrayBuffer(300 * 1024 * 1024)], 'test.txt'); + const md5 = await file2md5(file, {chunkSize: 3 * 1024 * 1024}); + + expect(typeof md5).toBe('string'); + expect(md5.length).toBeGreaterThan(0); + }); + + test('test abort method', async () => { + const file = new window.File([new ArrayBuffer(700 * 1024 * 1024)], 'test.txt'); + const abort = jest.fn().mockImplementation(() => file2md5.abort()); + + setTimeout( + () => abort(), + 1500 + ); + + const md5 = await file2md5(file, {chunkSize: 3 * 1024 * 1024}); + + expect(abort).toHaveBeenCalled(); + expect(typeof md5).toBe('string'); + expect(md5).toBe(''); + }); +}); diff --git a/src/index.ts b/src/index.ts index fbb7656..6c70d56 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,5 @@ import SparkMD5 from 'spark-md5'; +import noop from 'lodash.noop'; export interface IOptions { chunkSize?: number; @@ -6,9 +7,12 @@ export interface IOptions { onProgress?: (progress: number) => unknown; } -type IResolvedResult = [md5: string, abort: () => void]; +export interface IFile2Md5 { + (file: File, options: IOptions): Promise; + abort(): void; +} -const file2md5 = function (file: File, options: IOptions = {}): Promise { +const file2md5: IFile2Md5 = function (file: File, options: IOptions = {}): Promise { const {chunkSize = 2 * 1024 * 1024, raw = false, onProgress} = options; const spark = new SparkMD5.ArrayBuffer(); const fileReader = new FileReader(); @@ -28,7 +32,7 @@ const file2md5 = function (file: File, options: IOptions = {}): Promise void, reject: (err: DOMException | null) => void): void { + const execute = function (resolve: (md5: string) => void, reject: (err: DOMException | null) => void): void { fileReader.addEventListener( 'load', e => { @@ -43,7 +47,7 @@ const file2md5 = function (file: File, options: IOptions = {}): Promise { // Resets the internal state of the computation spark.reset(); + resolve(''); } ); @@ -67,7 +72,11 @@ const file2md5 = function (file: File, options: IOptions = {}): Promise(execute); + file2md5.abort = abort; + + return new Promise(execute); }; +file2md5.abort = noop; + export default file2md5; diff --git a/tsconfig.json b/tsconfig.json index f6af72f..b8bfea4 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -16,7 +16,11 @@ "moduleResolution": "node", "noUnusedParameters": true, "noUnusedLocals": true, - "noFallthroughCasesInSwitch": true + "noFallthroughCasesInSwitch": true, + "typeRoots": [ + "./node_modules/@types", + "./types" + ] }, "include": ["src"] } diff --git a/yarn.lock b/yarn.lock index f821705..b3296af 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1263,6 +1263,17 @@ "@types/yargs" "^15.0.0" chalk "^3.0.0" +"@jest/types@^26.6.2": + version "26.6.2" + resolved "http://registry.npm.baidu-int.com/@jest%2ftypes/-/types-26.6.2.tgz#bef5a532030e1d88a2f5a6d933f84e97226ed48e" + integrity sha1-vvWlMgMOHYii9abZM/hOlyJu1I4= + dependencies: + "@types/istanbul-lib-coverage" "^2.0.0" + "@types/istanbul-reports" "^3.0.0" + "@types/node" "*" + "@types/yargs" "^15.0.0" + chalk "^4.0.0" + "@nodelib/fs.scandir@2.1.3": version "2.1.3" resolved "http://registry.npm.baidu-int.com/@nodelib%2ffs.scandir/-/fs.scandir-2.1.3.tgz#3a582bdb53804c6ba6d146579c46e52130cf4a3b" @@ -1568,11 +1579,38 @@ "@types/istanbul-lib-coverage" "*" "@types/istanbul-lib-report" "*" +"@types/istanbul-reports@^3.0.0": + version "3.0.0" + resolved "http://registry.npm.baidu-int.com/@types%2fistanbul-reports/-/istanbul-reports-3.0.0.tgz#508b13aa344fa4976234e75dddcc34925737d821" + integrity sha1-UIsTqjRPpJdiNOdd3cw0klc32CE= + dependencies: + "@types/istanbul-lib-report" "*" + +"@types/jest@^26.0.15": + version "26.0.15" + resolved "http://registry.npm.baidu-int.com/@types%2fjest/-/jest-26.0.15.tgz#12e02c0372ad0548e07b9f4e19132b834cb1effe" + integrity sha1-EuAsA3KtBUjge59OGRMrg0yx7/4= + dependencies: + jest-diff "^26.0.0" + pretty-format "^26.0.0" + "@types/json-schema@^7.0.3", "@types/json-schema@^7.0.5", "@types/json-schema@^7.0.6": version "7.0.6" resolved "http://registry.npm.baidu-int.com/@types%2fjson-schema/-/json-schema-7.0.6.tgz#f4c7ec43e81b319a9815115031709f26987891f0" integrity sha1-9MfsQ+gbMZqYFRFQMXCfJph4kfA= +"@types/lodash.noop@^3.0.6": + version "3.0.6" + resolved "http://registry.npm.baidu-int.com/@types%2flodash.noop/-/lodash.noop-3.0.6.tgz#2be7ceeabe21af841c76b57b6ff12bb04b7ad21f" + integrity sha1-K+fO6r4hr4QcdrV7b/ErsEt60h8= + dependencies: + "@types/lodash" "*" + +"@types/lodash@*": + version "4.14.165" + resolved "http://registry.npm.baidu-int.com/@types%2flodash/-/lodash-4.14.165.tgz#74d55d947452e2de0742bad65270433b63a8c30f" + integrity sha1-dNVdlHRS4t4HQrrWUnBDO2Ooww8= + "@types/minimatch@*": version "3.0.3" resolved "http://registry.npm.baidu-int.com/@types%2fminimatch/-/minimatch-3.0.3.tgz#3dca0e3f33b200fc7d1139c0cd96c1268cadfd9d" @@ -3943,6 +3981,11 @@ diff-sequences@^25.2.6: resolved "http://registry.npm.baidu-int.com/diff-sequences/-/diff-sequences-25.2.6.tgz#5f467c00edd35352b7bca46d7927d60e687a76dd" integrity sha1-X0Z8AO3TU1K3vKRteSfWDmh6dt0= +diff-sequences@^26.6.2: + version "26.6.2" + resolved "http://registry.npm.baidu-int.com/diff-sequences/-/diff-sequences-26.6.2.tgz#48ba99157de1923412eed41db6b6d4aa9ca7c0b1" + integrity sha1-SLqZFX3hkjQS7tQdtrbUqpynwLE= + dir-glob@^2.0.0: version "2.2.2" resolved "http://registry.npm.baidu-int.com/dir-glob/-/dir-glob-2.2.2.tgz#fa09f0694153c8918b18ba0deafae94769fc50c4" @@ -6833,6 +6876,16 @@ jest-diff@^25.5.0: jest-get-type "^25.2.6" pretty-format "^25.5.0" +jest-diff@^26.0.0: + version "26.6.2" + resolved "http://registry.npm.baidu-int.com/jest-diff/-/jest-diff-26.6.2.tgz#1aa7468b52c3a68d7d5c5fdcdfcd5e49bd164394" + integrity sha1-GqdGi1LDpo19XF/c381eSb0WQ5Q= + dependencies: + chalk "^4.0.0" + diff-sequences "^26.6.2" + jest-get-type "^26.3.0" + pretty-format "^26.6.2" + jest-docblock@^25.3.0: version "25.3.0" resolved "http://registry.npm.baidu-int.com/jest-docblock/-/jest-docblock-25.3.0.tgz#8b777a27e3477cd77a168c05290c471a575623ef" @@ -6880,6 +6933,11 @@ jest-get-type@^25.2.6: resolved "http://registry.npm.baidu-int.com/jest-get-type/-/jest-get-type-25.2.6.tgz#0b0a32fab8908b44d508be81681487dbabb8d877" integrity sha1-Cwoy+riQi0TVCL6BaBSH26u42Hc= +jest-get-type@^26.3.0: + version "26.3.0" + resolved "http://registry.npm.baidu-int.com/jest-get-type/-/jest-get-type-26.3.0.tgz#e97dc3c3f53c2b406ca7afaed4493b1d099199e0" + integrity sha1-6X3Dw/U8K0Bsp6+u1Ek7HQmRmeA= + jest-haste-map@^25.5.1: version "25.5.1" resolved "http://registry.npm.baidu-int.com/jest-haste-map/-/jest-haste-map-25.5.1.tgz#1df10f716c1d94e60a1ebf7798c9fb3da2620943" @@ -7554,6 +7612,11 @@ lodash.memoize@^4.1.2: resolved "http://registry.npm.baidu-int.com/lodash.memoize/-/lodash.memoize-4.1.2.tgz#bcc6c49a42a2840ed997f323eada5ecd182e0bfe" integrity sha1-vMbEmkKihA7Zl/Mj6tpezRguC/4= +lodash.noop@^3.0.1: + version "3.0.1" + resolved "http://registry.npm.baidu-int.com/lodash.noop/-/lodash.noop-3.0.1.tgz#38188f4d650a3a474258439b96ec45b32617133c" + integrity sha1-OBiPTWUKOkdCWEObluxFsyYXEzw= + lodash.sortby@^4.7.0: version "4.7.0" resolved "http://registry.npm.baidu-int.com/lodash.sortby/-/lodash.sortby-4.7.0.tgz#edd14c824e2cc9c1e0b0a1b42bb5210516a42438" @@ -9471,6 +9534,16 @@ pretty-format@^25.5.0: ansi-styles "^4.0.0" react-is "^16.12.0" +pretty-format@^26.0.0, pretty-format@^26.6.2: + version "26.6.2" + resolved "http://registry.npm.baidu-int.com/pretty-format/-/pretty-format-26.6.2.tgz#e35c2705f14cb7fe2fe94fa078345b444120fc93" + integrity sha1-41wnBfFMt/4v6U+geDRbREEg/JM= + dependencies: + "@jest/types" "^26.6.2" + ansi-regex "^5.0.0" + ansi-styles "^4.0.0" + react-is "^17.0.1" + pretty-time@^1.1.0: version "1.1.0" resolved "http://registry.npm.baidu-int.com/pretty-time/-/pretty-time-1.1.0.tgz#ffb7429afabb8535c346a34e41873adf3d74dd0e" @@ -9707,6 +9780,11 @@ react-is@^16.12.0, react-is@^16.13.1, react-is@^16.8.1, react-is@^16.8.6: resolved "http://registry.npm.baidu-int.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4" integrity sha1-eJcppNw23imZ3BVt1sHZwYzqVqQ= +react-is@^17.0.1: + version "17.0.1" + resolved "http://registry.npm.baidu-int.com/react-is/-/react-is-17.0.1.tgz#5b3531bd76a645a4c9fb6e693ed36419e3301339" + integrity sha1-WzUxvXamRaTJ+25pPtNkGeMwEzk= + react-monaco-editor@^0.36.0: version "0.36.0" resolved "http://registry.npm.baidu-int.com/react-monaco-editor/-/react-monaco-editor-0.36.0.tgz#ac085c14f25fb072514c925596f6a06a711ee078"