Skip to content
This repository was archived by the owner on Mar 24, 2025. It is now read-only.

Commit d42c132

Browse files
Cleanup of esbuild-sass-modules-plugin.class.test
1 parent a3e50c2 commit d42c132

2 files changed

Lines changed: 114 additions & 158 deletions

File tree

test/esbuild-sass-modules-plugin.class.test.js

Lines changed: 47 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import p from 'path';
2-
import fsp from 'fs/promises';
32

43
import ESBuildSASSModulesPlugin, {
54
defaultConfig,
@@ -20,6 +19,10 @@ import
2019
PATH_SAMPLE_FILE_JS,
2120
PATH_SAMPLE_DYNAMIC_SIMPLE_JS
2221
} from './constants.js';
22+
import {
23+
chainTestSASSbuild,
24+
expectImportResolverToMatch
25+
} from './utils.js';
2326

2427
test(
2528
'Loads default config',
@@ -35,27 +38,27 @@ test(
3538
async function testSourcesFilter() {
3639
await expect(new Promise((ok, fail) => {
3740
const fakeEsb =
38-
{ onResolve(filter, fn) {
39-
const checkOkScss = filter.filter.test('file.scss');
40-
const checkOkSass = filter.filter.test('file.sass');
41+
{ async onResolve({ filter }, fn) {
42+
const checkOkScss = filter.test('file.scss');
43+
const checkOkSass = filter.test('file.sass');
4144

4245
if(!checkOkScss) fail('.scss file not resolved');
4346
if(!checkOkSass) fail('.sass file not resolved');
4447

45-
const checkFail = filter.filter.test('file.txt');
48+
const checkFail = filter.test('file.txt');
4649

4750
if(checkFail) fail('Resolves non-SASS file');
4851
}
49-
, onLoad(filter, fn) {
50-
const checkOkScss = filter.filter.test('file.scss');
51-
const checkOkSass = filter.filter.test('file.sass');
52+
, async onLoad({ filter }, fn) {
53+
const checkOkScss = filter.test('file.scss');
54+
const checkOkSass = filter.test('file.sass');
5255

5356
if(!checkOkScss) fail('.scss file not loaded');
5457
if(!checkOkSass) fail('.sass file not loaded');
5558

56-
const checkFail = filter.filter.test('file.txt');
59+
const checkFail = filter.test('file.txt');
5760

58-
if(checkFail) fail('Loaded non-SASS file');
61+
if(checkFail) fail('Loads non-SASS file');
5962
}
6063
};
6164

@@ -66,7 +69,7 @@ test(
6669

6770
ok();
6871
}).not.toThrow();
69-
})).resolves.toEqual(undefined);
72+
})).resolves.toBeUndefined();
7073
}
7174
);
7275

@@ -78,7 +81,8 @@ test(
7881

7982
const fakeEsb =
8083
{ async onResolve(filter, fn) {
81-
const dir = p.dirname(p.resolve(PATH_SAMPLE_SIMPLE_SCSS, '../../'));
84+
const dir =
85+
p.dirname(p.resolve(PATH_SAMPLE_SIMPLE_SCSS, '../../'));
8286

8387
fn(
8488
{ path: PATH_SAMPLE_SIMPLE_SCSS
@@ -88,54 +92,18 @@ test(
8892
}
8993
).catch(fail).then(ok);
9094
}
91-
, onLoad(filter, fn) {
95+
, async onLoad(filter, fn) {
9296
}
9397
, async resolve() {
9498
return { path: p.resolve(PATH_SAMPLE_SIMPLE_SCSS) };
9599
}
96100
};
97101

98-
plugin.setup(fakeEsb);
102+
expect(() => plugin.setup(fakeEsb)).not.toThrow();
99103
})).rejects.toBe('Unsupported kind of import `unsupported\'');
100104
}
101105
);
102106

103-
async function checkImportResolverType(type, scssPath, importer) {
104-
return new Promise((ok, fail) => {
105-
const plugin = new ESBuildSASSModulesPlugin();
106-
107-
const fakeEsb =
108-
{ async onResolve(filter, fn) {
109-
const
110-
{ path: pathSCSS
111-
, namespace: namespaceSCSS
112-
, pluginData
113-
} = await fn(
114-
{ path: scssPath
115-
, kind: 'import-statement'
116-
, importer
117-
, resolveDir:
118-
p.dirname(scssPath)
119-
}
120-
);
121-
122-
expect(pluginData)
123-
.toMatchObject(
124-
{ importResolver: type }
125-
);
126-
}
127-
, onLoad(filter, fn) {
128-
ok();
129-
}
130-
, async resolve() {
131-
return { path: scssPath };
132-
}
133-
};
134-
135-
plugin.setup(fakeEsb);
136-
});
137-
}
138-
139107
test(
140108
'Bundles sass sources from import statements',
141109
async function testSASSImport() {
@@ -191,45 +159,14 @@ test(
191159

192160
expect(() => plugin.setup(fakeEsb)).not.toThrow();
193161
})
194-
.then(async loadFn => {
195-
const scssTestCompiled =
196-
await fsp.readFile(PATH_SAMPLE_SIMPLE_SCSS_COMPILED)
197-
.then(b => b.toString('utf8'));
198-
199-
await expect(
200-
loadFn(
201-
{ path: PATH_SAMPLE_SIMPLE_SCSS
202-
, pluginData:
203-
{ importResolver: ImportResolver.BUNDLE
204-
, loader: 'css'
205-
}
206-
}
207-
)
208-
).resolves.toMatchObject(
209-
{ contents: scssTestCompiled
210-
, loader: 'css'
211-
}
212-
);
213-
214-
const sassTestCompiled =
215-
await fsp.readFile(PATH_SAMPLE_SIMPLE_SASS_COMPILED)
216-
.then(b => b.toString('utf8'));
217-
218-
await expect(
219-
loadFn(
220-
{ path: PATH_SAMPLE_SIMPLE_SASS
221-
, pluginData:
222-
{ importResolver: ImportResolver.BUNDLE
223-
, loader: 'css'
224-
}
225-
}
226-
)
227-
).resolves.toMatchObject(
228-
{ contents: sassTestCompiled
229-
, loader: 'css'
230-
}
231-
);
232-
})).resolves.toEqual(undefined);
162+
.then(chainTestSASSbuild(
163+
PATH_SAMPLE_SIMPLE_SCSS,
164+
PATH_SAMPLE_SIMPLE_SCSS_COMPILED
165+
))
166+
.then(chainTestSASSbuild(
167+
PATH_SAMPLE_SIMPLE_SASS,
168+
PATH_SAMPLE_SIMPLE_SASS_COMPILED
169+
))).resolves.toBeTruthy();
233170
}
234171
);
235172

@@ -269,49 +206,33 @@ test(
269206

270207
expect(() => plugin.setup(fakeEsb)).not.toThrow();
271208
})
272-
.then(async loadFn => {
273-
const scssTestCompiled =
274-
await fsp.readFile(PATH_SAMPLE_SIMPLE_SCSS_COMPILED)
275-
.then(b => b.toString('utf8'));
276-
277-
await expect(
278-
loadFn(
279-
{ path: PATH_SAMPLE_SIMPLE_SCSS
280-
, pluginData:
281-
{ importResolver: ImportResolver.BUNDLE
282-
, loader: 'css'
283-
}
284-
}
285-
)
286-
).resolves.toMatchObject(
287-
{ contents: scssTestCompiled
288-
, loader: 'css'
289-
}
290-
);
291-
})).resolves.toEqual(undefined);
209+
.then(chainTestSASSbuild(
210+
PATH_SAMPLE_SIMPLE_SCSS,
211+
PATH_SAMPLE_SIMPLE_SCSS_COMPILED
212+
))).resolves.toBeTruthy();
292213
}
293-
)
214+
);
294215

295216
test(
296217
'Detects the correct import resolver',
297218
async function testImportResolverType() {
298-
await expect(checkImportResolverType(
219+
await expectImportResolverToMatch(
299220
ImportResolver.BUNDLE,
300221
p.basename(PATH_SAMPLE_SIMPLE_SCSS),
301222
PATH_SAMPLE_SIMPLE_JS_IMPORT_SCSS
302-
)).resolves.toEqual(undefined);
223+
);
303224

304-
await expect(checkImportResolverType(
225+
await expectImportResolverToMatch(
305226
ImportResolver.INLINE,
306227
'inline:' + PATH_SAMPLE_SIMPLE_SCSS,
307228
PATH_SAMPLE_INLINE_JS
308-
)).resolves.toEqual(undefined);
229+
);
309230

310-
await expect(checkImportResolverType(
231+
await expectImportResolverToMatch(
311232
ImportResolver.FILE,
312233
'file:' + PATH_SAMPLE_SIMPLE_SCSS,
313234
PATH_SAMPLE_FILE_JS
314-
)).resolves.toEqual(undefined);
235+
);
315236
}
316237
);
317238

@@ -357,27 +278,12 @@ test(
357278
const plugin =
358279
new ESBuildSASSModulesPlugin(sourceMapDisabled);
359280

360-
plugin.setup(fakeEsb);
281+
expect(() => plugin.setup(fakeEsb)).not.toThrow();
361282
})
362-
.then(async loadFn => {
363-
const compiled = await fsp.readFile(PATH_SAMPLE_POSTCSS_COMPILED)
364-
.then(b => b.toString('utf8'));
365-
366-
await expect(
367-
loadFn(
368-
{ path: PATH_SAMPLE_POSTCSS
369-
, pluginData:
370-
{ importResolver: ImportResolver.BUNDLE
371-
, loader: 'css'
372-
}
373-
}
374-
)
375-
).resolves.toMatchObject(
376-
{ contents: compiled
377-
, loader: 'css'
378-
}
379-
);
380-
})).resolves.toEqual(undefined);
283+
.then(chainTestSASSbuild(
284+
PATH_SAMPLE_POSTCSS,
285+
PATH_SAMPLE_POSTCSS_COMPILED
286+
))).resolves.toBeTruthy();
381287

382288
await expect(new Promise((ok, fail) => {
383289
const fakeEsb =
@@ -402,28 +308,11 @@ test(
402308
const plugin =
403309
new ESBuildSASSModulesPlugin(sourceMapEnabled);
404310

405-
plugin.setup(fakeEsb);
311+
expect(() => plugin.setup(fakeEsb)).not.toThrow();
406312
})
407-
.then(async loadFn => {
408-
const compiled = await fsp.readFile(
409-
PATH_SAMPLE_POSTCSS_SOURCEMAP_COMPILED
410-
)
411-
.then(b => b.toString('utf8'));
412-
413-
await expect(
414-
loadFn(
415-
{ path: PATH_SAMPLE_POSTCSS
416-
, pluginData:
417-
{ importResolver: ImportResolver.BUNDLE
418-
, loader: 'css'
419-
}
420-
}
421-
)
422-
).resolves.toMatchObject(
423-
{ contents: compiled
424-
, loader: 'css'
425-
}
426-
);
427-
})).resolves.toEqual(undefined);
313+
.then(chainTestSASSbuild(
314+
PATH_SAMPLE_POSTCSS,
315+
PATH_SAMPLE_POSTCSS_SOURCEMAP_COMPILED
316+
))).resolves.toBeTruthy();
428317
}
429318
);

test/utils.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import {
1313
PATH_SAMPLE_SIMPLE_JS_IMPORT_SCSS,
1414
PATH_SAMPLES
1515
} from './constants.js';
16+
import ESBuildSASSModulesPlugin, { ImportResolver } from '../src/esbuild-sass-modules-plugin.class.js';
17+
import p from 'path';
1618

1719
const COMMON_ESBUILD_OPTIONS =
1820
{ bundle: true
@@ -81,6 +83,68 @@ async function testBuild(esbResult, compiledPath) {
8183
await expectOutfileToMatch(compiledPath);
8284
}
8385

86+
async function testSASSbuild(loadFn, sourcePath, compiledPath) {
87+
const scssTestCompiled =
88+
await fsp.readFile(compiledPath)
89+
.then(b => b.toString('utf8'));
90+
91+
await expect(
92+
loadFn(
93+
{ path: sourcePath
94+
, pluginData:
95+
{ importResolver: ImportResolver.BUNDLE
96+
, loader: 'css'
97+
}
98+
}
99+
)
100+
).resolves.toMatchObject(
101+
{ contents: scssTestCompiled
102+
, loader: 'css'
103+
}
104+
);
105+
}
106+
107+
function chainTestSASSbuild(sourcePath, compiledPath) {
108+
return async loadFn => testSASSbuild(loadFn, sourcePath, compiledPath)
109+
.then(() => loadFn);
110+
}
111+
112+
async function expectImportResolverToMatch(type, scssPath, importer) {
113+
await expect(new Promise((ok, fail) => {
114+
const plugin = new ESBuildSASSModulesPlugin();
115+
116+
const fakeEsb =
117+
{ async onResolve(filter, fn) {
118+
const
119+
{ path: pathSCSS
120+
, namespace: namespaceSCSS
121+
, pluginData
122+
} = await fn(
123+
{ path: scssPath
124+
, kind: 'import-statement'
125+
, importer
126+
, resolveDir:
127+
p.dirname(scssPath)
128+
}
129+
);
130+
131+
expect(pluginData)
132+
.toMatchObject(
133+
{ importResolver: type }
134+
);
135+
}
136+
, async onLoad(filter, fn) {
137+
ok();
138+
}
139+
, async resolve() {
140+
return { path: scssPath };
141+
}
142+
};
143+
144+
expect(() => plugin.setup(fakeEsb)).not.toThrow();
145+
})).resolves.toBeUndefined();
146+
}
147+
84148
export
85149
{ COMMON_ESBUILD_OPTIONS
86150
, commonESBuildTest
@@ -95,4 +159,7 @@ export
95159
, expectNotToBuild
96160
, expectOutfileToMatch
97161
, testBuild
162+
, testSASSbuild
163+
, chainTestSASSbuild
164+
, expectImportResolverToMatch
98165
};

0 commit comments

Comments
 (0)