Skip to content

Commit c0c9fa7

Browse files
authoredMay 14, 2020
test: transform && transformPath (#470)
1 parent 197b0d8 commit c0c9fa7

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed
 

‎test/cacheTransform-option.test.js

+45
Original file line numberDiff line numberDiff line change
@@ -247,4 +247,49 @@ describe('cache option', () => {
247247
.then(done)
248248
.catch(done);
249249
});
250+
251+
it('should cache when "from" is a file', (done) => {
252+
const from = 'file.txt';
253+
254+
runEmit({
255+
expectedAssetKeys: ['subdir/test.txt'],
256+
expectedAssetContent: {
257+
'subdir/test.txt': 'newchanged!',
258+
},
259+
patterns: [
260+
{
261+
from,
262+
cacheTransform: true,
263+
transform: function transform(content) {
264+
return new Promise((resolve) => {
265+
resolve(`${content}changed!`);
266+
});
267+
},
268+
transformPath(targetPath, absoluteFrom) {
269+
expect(absoluteFrom).toBe(path.join(FIXTURES_DIR, 'file.txt'));
270+
271+
return targetPath.replace('file.txt', 'subdir/test.txt');
272+
},
273+
},
274+
],
275+
})
276+
.then(() =>
277+
cacache.ls(cacheDir).then((cacheEntries) => {
278+
const cacheKeys = Object.keys(cacheEntries);
279+
280+
expect(cacheKeys).toHaveLength(1);
281+
282+
cacheKeys.forEach((cacheKey) => {
283+
// eslint-disable-next-line no-new-func
284+
const cacheEntry = new Function(
285+
`'use strict'\nreturn ${cacheKey}`
286+
)();
287+
288+
expect(cacheEntry.pattern.from).toBe(from);
289+
});
290+
})
291+
)
292+
.then(done)
293+
.catch(done);
294+
});
250295
});

‎test/transform-option.test.js

+26
Original file line numberDiff line numberDiff line change
@@ -205,4 +205,30 @@ describe('transform option', () => {
205205
.then(done)
206206
.catch(done);
207207
});
208+
209+
it('should transform file when "from" is a file', (done) => {
210+
runEmit({
211+
expectedAssetKeys: ['subdir/test.txt'],
212+
expectedAssetContent: {
213+
'subdir/test.txt': 'newchanged',
214+
},
215+
patterns: [
216+
{
217+
from: 'file.txt',
218+
transform(content, absoluteFrom) {
219+
expect(absoluteFrom.includes(FIXTURES_DIR)).toBe(true);
220+
221+
return `${content}changed`;
222+
},
223+
transformPath(targetPath, absoluteFrom) {
224+
expect(absoluteFrom).toBe(path.join(FIXTURES_DIR, 'file.txt'));
225+
226+
return targetPath.replace('file.txt', 'subdir/test.txt');
227+
},
228+
},
229+
],
230+
})
231+
.then(done)
232+
.catch(done);
233+
});
208234
});

0 commit comments

Comments
 (0)
Please sign in to comment.