1- import { strict as test } from "assert " ;
1+ import { test , expect } from "vitest " ;
22import { hash , replaceHash } from "../plugin" ;
33import fs from "fs" ;
44import path from "path" ;
@@ -13,38 +13,37 @@ const buffer = fs.readFileSync(path.resolve(__dirname, "__fixtures__/original/bu
1313const DEFAULT_HASH_LENGTH = 20 ;
1414const DEFAULT_PATTERN = new RegExp ( / \[ h a s h .* ] / g) ;
1515
16- test . strictEqual ( replaceHash ( "[hash].js" , buffer , DEFAULT_PATTERN , DEFAULT_HASH_LENGTH ) , "b0dcc67ffc1fd562f212.js" ) ;
17- test . strictEqual (
18- replaceHash ( "script.[hash].js" , buffer , DEFAULT_PATTERN , DEFAULT_HASH_LENGTH ) ,
19- "script.b0dcc67ffc1fd562f212.js"
20- ) ;
21- test . strictEqual (
22- replaceHash ( "script.[hash:20].js" , buffer , DEFAULT_PATTERN , DEFAULT_HASH_LENGTH ) ,
23- "script.b0dcc67ffc1fd562f212.js"
24- ) ;
25- test . strictEqual ( replaceHash ( "script.[hash:8].js" , buffer , DEFAULT_PATTERN , DEFAULT_HASH_LENGTH ) , "script.b0dcc67f.js" ) ;
26- test . throws ( ( ) => replaceHash ( "script.js" , buffer , DEFAULT_PATTERN , DEFAULT_HASH_LENGTH ) ) ;
27- test . throws ( ( ) => replaceHash ( "script[].js" , buffer , DEFAULT_PATTERN , DEFAULT_HASH_LENGTH ) ) ;
28- test . throws ( ( ) => replaceHash ( "script.[has:8].js" , buffer , DEFAULT_PATTERN , DEFAULT_HASH_LENGTH ) ) ;
29- test . throws ( ( ) => replaceHash ( "script.js" , buffer , DEFAULT_PATTERN , DEFAULT_HASH_LENGTH ) ) ;
16+ test ( "posthtml-hash" , ( ) => {
17+ expect ( replaceHash ( "[hash].js" , buffer , DEFAULT_PATTERN , DEFAULT_HASH_LENGTH ) ) . toEqual ( "b0dcc67ffc1fd562f212.js" ) ;
18+ expect ( replaceHash ( "script.[hash].js" , buffer , DEFAULT_PATTERN , DEFAULT_HASH_LENGTH ) ) . toEqual (
19+ "script.b0dcc67ffc1fd562f212.js"
20+ ) ;
21+ expect ( replaceHash ( "script.[hash:20].js" , buffer , DEFAULT_PATTERN , DEFAULT_HASH_LENGTH ) ) . toEqual (
22+ "script.b0dcc67ffc1fd562f212.js"
23+ ) ;
24+ expect ( replaceHash ( "script.[hash:8].js" , buffer , DEFAULT_PATTERN , DEFAULT_HASH_LENGTH ) ) . toEqual ( "script.b0dcc67f.js" ) ;
25+ expect ( ( ) => replaceHash ( "script[].js" , buffer , DEFAULT_PATTERN , DEFAULT_HASH_LENGTH ) ) . toThrow ( ) ;
26+ expect ( ( ) => replaceHash ( "script.[has:8].js" , buffer , DEFAULT_PATTERN , DEFAULT_HASH_LENGTH ) ) . toThrow ( ) ;
27+ expect ( ( ) => replaceHash ( "script.js" , buffer , DEFAULT_PATTERN , DEFAULT_HASH_LENGTH ) ) . toThrow ( ) ;
28+ expect ( ( ) => replaceHash ( "script.js" , buffer , DEFAULT_PATTERN , DEFAULT_HASH_LENGTH ) ) . toThrow ( ) ;
3029
31- const CUSTOM_EXP = new RegExp ( / \[ o h - m y - h a s h .* ] / g) ;
30+ const CUSTOM_EXP = new RegExp ( / \[ o h - m y - h a s h .* ] / g) ;
3231
33- test . strictEqual ( replaceHash ( "[oh-my-hash].js" , buffer , CUSTOM_EXP , DEFAULT_HASH_LENGTH ) , "b0dcc67ffc1fd562f212.js" ) ;
34- test . strictEqual ( replaceHash ( "script.[oh-my-hash].js" , buffer , CUSTOM_EXP , 8 ) , "script.b0dcc67f.js" ) ;
32+ expect ( replaceHash ( "[oh-my-hash].js" , buffer , CUSTOM_EXP , DEFAULT_HASH_LENGTH ) , "b0dcc67ffc1fd562f212.js" ) ;
33+ expect ( replaceHash ( "script.[oh-my-hash].js" , buffer , CUSTOM_EXP , 8 ) , "script.b0dcc67f.js" ) ;
3534
36- const CUSTOM_EXP_2 = new RegExp ( / \o h - m y - h a s h / ) ;
35+ const CUSTOM_EXP_2 = new RegExp ( / \o h - m y - h a s h / ) ;
3736
38- test . strictEqual ( replaceHash ( "oh-my-hash.js" , buffer , CUSTOM_EXP_2 , DEFAULT_HASH_LENGTH ) , "b0dcc67ffc1fd562f212.js" ) ;
39- test . strictEqual ( replaceHash ( "script.oh-my-hash.js" , buffer , CUSTOM_EXP_2 , 8 ) , "script.b0dcc67f.js" ) ;
37+ expect ( replaceHash ( "oh-my-hash.js" , buffer , CUSTOM_EXP_2 , DEFAULT_HASH_LENGTH ) , "b0dcc67ffc1fd562f212.js" ) ;
38+ expect ( replaceHash ( "script.oh-my-hash.js" , buffer , CUSTOM_EXP_2 , 8 ) , "script.b0dcc67f.js" ) ;
4039
41- const CUSTOM_EXP_3 = new RegExp ( / c u s t o m - h a s h / ) ;
40+ const CUSTOM_EXP_3 = new RegExp ( / c u s t o m - h a s h / ) ;
4241
43- test . strictEqual (
44- replaceHash ( "script.custom-hash .js" , buffer , CUSTOM_EXP_3 , DEFAULT_HASH_LENGTH ) ,
45- "script.b0dcc67ffc1fd562f212.js"
46- ) ;
47- test . strictEqual ( replaceHash ( "script.custom-hash.js" , buffer , CUSTOM_EXP_3 , 4 ) , "script.b0dc.js" ) ;
42+ expect ( replaceHash ( "script.custom-hash.js" , buffer , CUSTOM_EXP_3 , DEFAULT_HASH_LENGTH ) ) . toEqual (
43+ "script.b0dcc67ffc1fd562f212 .js"
44+ ) ;
45+ expect ( replaceHash ( "script.custom-hash.js" , buffer , CUSTOM_EXP_3 , 4 ) ) . toEqual ( "script.b0dc.js" ) ;
46+ } ) ;
4847
4948function copyFixture ( fileName : string ) {
5049 const file = path . join ( __dirname , "__fixtures__/original" , fileName ) ;
@@ -69,7 +68,7 @@ async function fixture() {
6968 </html>`
7069 ) ;
7170
72- test . strictEqual (
71+ expect (
7372 result . html . replace ( / \n | \s + / g, "" ) ,
7473 '<html><head><linkrel="stylesheet"href="https://fonts.googleapis.com/css?family=Open+Sans:400,400i,600,600i,700,700i"><linkrel="stylesheet"href="bundle.min.9a6cf95c41e87b9dc102.css"></head><body><scriptsrc="bundle.min.b0dcc67ffc1fd562f212.js"></script></body></html>'
7574 ) ;
@@ -96,7 +95,7 @@ async function fixture() {
9695 </html>`
9796 ) ;
9897
99- test . strictEqual (
98+ expect (
10099 result2 . html . replace ( / \n | \s + / g, "" ) ,
101100 '<html><body><scriptsrc="script.b0dcc67f.js"></script><scriptsrc="script.b0dcc67f.js"></script></body></html>'
102101 ) ;
0 commit comments