1- import test from "ava" ;
2- import fs from "fs" ;
3- import path from "path" ;
1+ import test from "node:test" ;
2+ import fs from "node:fs" ;
3+ import path from "node:path" ;
4+ import assert from "node:assert/strict" ;
45import { webpackAsync } from "./helpers/webpackAsync.js" ;
56import createTestDirectory from "./helpers/createTestDirectory.js" ;
7+ import { fileURLToPath } from "node:url" ;
8+
9+ const __dirname = path . dirname ( fileURLToPath ( import . meta. url ) ) ;
610
711const defaultCacheDir = path . join (
812 __dirname ,
@@ -33,24 +37,26 @@ const CACHE_FILE_REGEX = /^[0-9a-f]{32}(?:[0-9a-f]{32})?\.json\.gz$/;
3337// Create a separate directory for each test so that the tests
3438// can run in parallel
3539
40+ const context = { directory : undefined , cacheDirectory : undefined } ;
41+
3642test . beforeEach ( async t => {
37- const directory = await createTestDirectory ( outputDir , t . title ) ;
38- t . context . directory = directory ;
39- const cacheDirectory = await createTestDirectory ( cacheDir , t . title ) ;
40- t . context . cacheDirectory = cacheDirectory ;
43+ const directory = await createTestDirectory ( outputDir , t . name ) ;
44+ context . directory = directory ;
45+ const cacheDirectory = await createTestDirectory ( cacheDir , t . name ) ;
46+ context . cacheDirectory = cacheDirectory ;
4147} ) ;
4248test . beforeEach ( ( ) =>
4349 fs . rmSync ( defaultCacheDir , { recursive : true , force : true } ) ,
4450) ;
45- test . afterEach ( t => {
46- fs . rmSync ( t . context . directory , { recursive : true , force : true } ) ;
47- fs . rmSync ( t . context . cacheDirectory , { recursive : true , force : true } ) ;
51+ test . afterEach ( ( ) => {
52+ fs . rmSync ( context . directory , { recursive : true , force : true } ) ;
53+ fs . rmSync ( context . cacheDirectory , { recursive : true , force : true } ) ;
4854} ) ;
4955
50- test ( "should output files to cache directory" , async t => {
56+ test ( "should output files to cache directory" , async ( ) => {
5157 const config = Object . assign ( { } , globalConfig , {
5258 output : {
53- path : t . context . directory ,
59+ path : context . directory ,
5460 } ,
5561 module : {
5662 rules : [
@@ -59,7 +65,7 @@ test("should output files to cache directory", async t => {
5965 loader : babelLoader ,
6066 exclude : / n o d e _ m o d u l e s / ,
6167 options : {
62- cacheDirectory : t . context . cacheDirectory ,
68+ cacheDirectory : context . cacheDirectory ,
6369 presets : [ "@babel/preset-env" ] ,
6470 } ,
6571 } ,
@@ -68,17 +74,17 @@ test("should output files to cache directory", async t => {
6874 } ) ;
6975
7076 const stats = await webpackAsync ( config ) ;
71- t . deepEqual ( stats . compilation . errors , [ ] ) ;
72- t . deepEqual ( stats . compilation . warnings , [ ] ) ;
77+ assert . deepEqual ( stats . compilation . errors , [ ] ) ;
78+ assert . deepEqual ( stats . compilation . warnings , [ ] ) ;
7379
74- const files = fs . readdirSync ( t . context . cacheDirectory ) ;
75- t . true ( files . length > 0 ) ;
80+ const files = fs . readdirSync ( context . cacheDirectory ) ;
81+ assert . ok ( files . length > 0 ) ;
7682} ) ;
7783
78- test ( "should output json.gz files to standard cache dir by default" , async t => {
84+ test ( "should output json.gz files to standard cache dir by default" , async ( ) => {
7985 const config = Object . assign ( { } , globalConfig , {
8086 output : {
81- path : t . context . directory ,
87+ path : context . directory ,
8288 } ,
8389 module : {
8490 rules : [
@@ -96,18 +102,18 @@ test("should output json.gz files to standard cache dir by default", async t =>
96102 } ) ;
97103
98104 const stats = await webpackAsync ( config ) ;
99- t . deepEqual ( stats . compilation . errors , [ ] ) ;
100- t . deepEqual ( stats . compilation . warnings , [ ] ) ;
105+ assert . deepEqual ( stats . compilation . errors , [ ] ) ;
106+ assert . deepEqual ( stats . compilation . warnings , [ ] ) ;
101107
102108 let files = fs . readdirSync ( defaultCacheDir ) ;
103109 files = files . filter ( file => CACHE_FILE_REGEX . test ( file ) ) ;
104- t . true ( files . length > 0 ) ;
110+ assert . ok ( files . length > 0 ) ;
105111} ) ;
106112
107- test ( "should output non-compressed files to standard cache dir when cacheCompression is set to false" , async t => {
113+ test ( "should output non-compressed files to standard cache dir when cacheCompression is set to false" , async ( ) => {
108114 const config = Object . assign ( { } , globalConfig , {
109115 output : {
110- path : t . context . directory ,
116+ path : context . directory ,
111117 } ,
112118 module : {
113119 rules : [
@@ -128,13 +134,13 @@ test("should output non-compressed files to standard cache dir when cacheCompres
128134 await webpackAsync ( config ) ;
129135 let files = fs . readdirSync ( defaultCacheDir ) ;
130136 files = files . filter ( file => UNCOMPRESSED_CACHE_FILE_REGEX . test ( file ) ) ;
131- t . true ( files . length > 0 ) ;
137+ assert . ok ( files . length > 0 ) ;
132138} ) ;
133139
134- test ( "should output files to standard cache dir if set to true in query" , async t => {
140+ test ( "should output files to standard cache dir if set to true in query" , async ( ) => {
135141 const config = Object . assign ( { } , globalConfig , {
136142 output : {
137- path : t . context . directory ,
143+ path : context . directory ,
138144 } ,
139145 module : {
140146 rules : [
@@ -152,18 +158,18 @@ test("should output files to standard cache dir if set to true in query", async
152158 } ) ;
153159
154160 const stats = await webpackAsync ( config ) ;
155- t . deepEqual ( stats . compilation . errors , [ ] ) ;
156- t . deepEqual ( stats . compilation . warnings , [ ] ) ;
161+ assert . deepEqual ( stats . compilation . errors , [ ] ) ;
162+ assert . deepEqual ( stats . compilation . warnings , [ ] ) ;
157163
158164 let files = fs . readdirSync ( defaultCacheDir ) ;
159165 files = files . filter ( file => CACHE_FILE_REGEX . test ( file ) ) ;
160- t . true ( files . length > 0 ) ;
166+ assert . ok ( files . length > 0 ) ;
161167} ) ;
162168
163- test ( "should read from cache directory if cached file exists" , async t => {
169+ test ( "should read from cache directory if cached file exists" , async ( ) => {
164170 const config = Object . assign ( { } , globalConfig , {
165171 output : {
166- path : t . context . directory ,
172+ path : context . directory ,
167173 } ,
168174 module : {
169175 rules : [
@@ -172,7 +178,7 @@ test("should read from cache directory if cached file exists", async t => {
172178 loader : babelLoader ,
173179 exclude : / n o d e _ m o d u l e s / ,
174180 options : {
175- cacheDirectory : t . context . cacheDirectory ,
181+ cacheDirectory : context . cacheDirectory ,
176182 presets : [ "@babel/preset-env" ] ,
177183 } ,
178184 } ,
@@ -183,18 +189,18 @@ test("should read from cache directory if cached file exists", async t => {
183189 // @TODO Find a way to know if the file as correctly read without relying on
184190 // Istanbul for coverage.
185191 const stats = await webpackAsync ( config ) ;
186- t . deepEqual ( stats . compilation . errors , [ ] ) ;
187- t . deepEqual ( stats . compilation . warnings , [ ] ) ;
192+ assert . deepEqual ( stats . compilation . errors , [ ] ) ;
193+ assert . deepEqual ( stats . compilation . warnings , [ ] ) ;
188194
189195 await webpackAsync ( config ) ;
190- const files = fs . readdirSync ( t . context . cacheDirectory ) ;
191- t . true ( files . length > 0 ) ;
196+ const files = fs . readdirSync ( context . cacheDirectory ) ;
197+ assert . ok ( files . length > 0 ) ;
192198} ) ;
193199
194- test ( "should have one file per module" , async t => {
200+ test ( "should have one file per module" , async ( ) => {
195201 const config = Object . assign ( { } , globalConfig , {
196202 output : {
197- path : t . context . directory ,
203+ path : context . directory ,
198204 } ,
199205 module : {
200206 rules : [
@@ -203,7 +209,7 @@ test("should have one file per module", async t => {
203209 loader : babelLoader ,
204210 exclude : / n o d e _ m o d u l e s / ,
205211 options : {
206- cacheDirectory : t . context . cacheDirectory ,
212+ cacheDirectory : context . cacheDirectory ,
207213 presets : [ "@babel/preset-env" ] ,
208214 } ,
209215 } ,
@@ -212,18 +218,18 @@ test("should have one file per module", async t => {
212218 } ) ;
213219
214220 const stats = await webpackAsync ( config ) ;
215- t . deepEqual ( stats . compilation . errors , [ ] ) ;
216- t . deepEqual ( stats . compilation . warnings , [ ] ) ;
221+ assert . deepEqual ( stats . compilation . errors , [ ] ) ;
222+ assert . deepEqual ( stats . compilation . warnings , [ ] ) ;
217223
218- const files = fs . readdirSync ( t . context . cacheDirectory ) ;
219- t . true ( files . length === 3 ) ;
224+ const files = fs . readdirSync ( context . cacheDirectory ) ;
225+ assert . ok ( files . length === 3 ) ;
220226} ) ;
221227
222- test ( "should generate a new file if the identifier changes" , async t => {
228+ test ( "should generate a new file if the identifier changes" , async ( ) => {
223229 const configs = [
224230 Object . assign ( { } , globalConfig , {
225231 output : {
226- path : t . context . directory ,
232+ path : context . directory ,
227233 } ,
228234 module : {
229235 rules : [
@@ -232,7 +238,7 @@ test("should generate a new file if the identifier changes", async t => {
232238 loader : babelLoader ,
233239 exclude : / n o d e _ m o d u l e s / ,
234240 options : {
235- cacheDirectory : t . context . cacheDirectory ,
241+ cacheDirectory : context . cacheDirectory ,
236242 cacheIdentifier : "a" ,
237243 presets : [ "@babel/preset-env" ] ,
238244 } ,
@@ -242,7 +248,7 @@ test("should generate a new file if the identifier changes", async t => {
242248 } ) ,
243249 Object . assign ( { } , globalConfig , {
244250 output : {
245- path : t . context . directory ,
251+ path : context . directory ,
246252 } ,
247253 module : {
248254 rules : [
@@ -251,7 +257,7 @@ test("should generate a new file if the identifier changes", async t => {
251257 loader : babelLoader ,
252258 exclude : / n o d e _ m o d u l e s / ,
253259 options : {
254- cacheDirectory : t . context . cacheDirectory ,
260+ cacheDirectory : context . cacheDirectory ,
255261 cacheIdentifier : "b" ,
256262 presets : [ "@babel/preset-env" ] ,
257263 } ,
@@ -264,21 +270,21 @@ test("should generate a new file if the identifier changes", async t => {
264270 await Promise . allSettled (
265271 configs . map ( async config => {
266272 const stats = await webpackAsync ( config ) ;
267- t . deepEqual ( stats . compilation . errors , [ ] ) ;
268- t . deepEqual ( stats . compilation . warnings , [ ] ) ;
273+ assert . deepEqual ( stats . compilation . errors , [ ] ) ;
274+ assert . deepEqual ( stats . compilation . warnings , [ ] ) ;
269275 } ) ,
270276 ) ;
271277
272- const files = fs . readdirSync ( t . context . cacheDirectory ) ;
273- t . true ( files . length === 6 ) ;
278+ const files = fs . readdirSync ( context . cacheDirectory ) ;
279+ assert . ok ( files . length === 6 ) ;
274280} ) ;
275281
276- test ( "should allow to specify the .babelrc file" , async t => {
282+ test ( "should allow to specify the .babelrc file" , async ( ) => {
277283 const config = [
278284 Object . assign ( { } , globalConfig , {
279285 entry : path . join ( __dirname , "fixtures/constant.js" ) ,
280286 output : {
281- path : t . context . directory ,
287+ path : context . directory ,
282288 } ,
283289 module : {
284290 rules : [
@@ -287,7 +293,7 @@ test("should allow to specify the .babelrc file", async t => {
287293 loader : babelLoader ,
288294 exclude : / n o d e _ m o d u l e s / ,
289295 options : {
290- cacheDirectory : t . context . cacheDirectory ,
296+ cacheDirectory : context . cacheDirectory ,
291297 extends : path . join ( __dirname , "fixtures/babelrc" ) ,
292298 babelrc : false ,
293299 presets : [ "@babel/preset-env" ] ,
@@ -299,7 +305,7 @@ test("should allow to specify the .babelrc file", async t => {
299305 Object . assign ( { } , globalConfig , {
300306 entry : path . join ( __dirname , "fixtures/constant.js" ) ,
301307 output : {
302- path : t . context . directory ,
308+ path : context . directory ,
303309 } ,
304310 module : {
305311 rules : [
@@ -308,7 +314,7 @@ test("should allow to specify the .babelrc file", async t => {
308314 loader : babelLoader ,
309315 exclude : / n o d e _ m o d u l e s / ,
310316 options : {
311- cacheDirectory : t . context . cacheDirectory ,
317+ cacheDirectory : context . cacheDirectory ,
312318 presets : [ "@babel/preset-env" ] ,
313319 } ,
314320 } ,
@@ -317,13 +323,13 @@ test("should allow to specify the .babelrc file", async t => {
317323 } ) ,
318324 ] ;
319325 const multiStats = await webpackAsync ( config ) ;
320- t . deepEqual ( multiStats . stats [ 0 ] . compilation . errors , [ ] ) ;
321- t . deepEqual ( multiStats . stats [ 0 ] . compilation . warnings , [ ] ) ;
322- t . deepEqual ( multiStats . stats [ 1 ] . compilation . errors , [ ] ) ;
323- t . deepEqual ( multiStats . stats [ 1 ] . compilation . warnings , [ ] ) ;
326+ assert . deepEqual ( multiStats . stats [ 0 ] . compilation . errors , [ ] ) ;
327+ assert . deepEqual ( multiStats . stats [ 0 ] . compilation . warnings , [ ] ) ;
328+ assert . deepEqual ( multiStats . stats [ 1 ] . compilation . errors , [ ] ) ;
329+ assert . deepEqual ( multiStats . stats [ 1 ] . compilation . warnings , [ ] ) ;
324330
325- const files = fs . readdirSync ( t . context . cacheDirectory ) ;
331+ const files = fs . readdirSync ( context . cacheDirectory ) ;
326332 // The two configs resolved to same Babel config because "fixtures/babelrc"
327333 // is { "presets": ["@babel/preset-env"] }
328- t . true ( files . length === 1 ) ;
334+ assert . ok ( files . length === 1 ) ;
329335} ) ;
0 commit comments