Skip to content

Commit

Permalink
test: transform && transformPath (#470)
Browse files Browse the repository at this point in the history
  • Loading branch information
cap-Bernardito committed May 14, 2020
1 parent 197b0d8 commit c0c9fa7
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
45 changes: 45 additions & 0 deletions test/cacheTransform-option.test.js
Expand Up @@ -247,4 +247,49 @@ describe('cache option', () => {
.then(done)
.catch(done);
});

it('should cache when "from" is a file', (done) => {
const from = 'file.txt';

runEmit({
expectedAssetKeys: ['subdir/test.txt'],
expectedAssetContent: {
'subdir/test.txt': 'newchanged!',
},
patterns: [
{
from,
cacheTransform: true,
transform: function transform(content) {
return new Promise((resolve) => {
resolve(`${content}changed!`);
});
},
transformPath(targetPath, absoluteFrom) {
expect(absoluteFrom).toBe(path.join(FIXTURES_DIR, 'file.txt'));

return targetPath.replace('file.txt', 'subdir/test.txt');
},
},
],
})
.then(() =>
cacache.ls(cacheDir).then((cacheEntries) => {
const cacheKeys = Object.keys(cacheEntries);

expect(cacheKeys).toHaveLength(1);

cacheKeys.forEach((cacheKey) => {
// eslint-disable-next-line no-new-func
const cacheEntry = new Function(
`'use strict'\nreturn ${cacheKey}`
)();

expect(cacheEntry.pattern.from).toBe(from);
});
})
)
.then(done)
.catch(done);
});
});
26 changes: 26 additions & 0 deletions test/transform-option.test.js
Expand Up @@ -205,4 +205,30 @@ describe('transform option', () => {
.then(done)
.catch(done);
});

it('should transform file when "from" is a file', (done) => {
runEmit({
expectedAssetKeys: ['subdir/test.txt'],
expectedAssetContent: {
'subdir/test.txt': 'newchanged',
},
patterns: [
{
from: 'file.txt',
transform(content, absoluteFrom) {
expect(absoluteFrom.includes(FIXTURES_DIR)).toBe(true);

return `${content}changed`;
},
transformPath(targetPath, absoluteFrom) {
expect(absoluteFrom).toBe(path.join(FIXTURES_DIR, 'file.txt'));

return targetPath.replace('file.txt', 'subdir/test.txt');
},
},
],
})
.then(done)
.catch(done);
});
});

0 comments on commit c0c9fa7

Please sign in to comment.