Skip to content

Commit

Permalink
Create hash test type and red test
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Feb 15, 2019
1 parent d6a865e commit bf59bfd
Show file tree
Hide file tree
Showing 27 changed files with 195 additions and 0 deletions.
69 changes: 69 additions & 0 deletions test/file-hashes/index.js
@@ -0,0 +1,69 @@
const path = require('path');
const rollup = require('../../dist/rollup');
const { extend, runTestSuiteWithSamples } = require('../utils.js');
const assert = require('assert');

runTestSuiteWithSamples('file hashes', path.resolve(__dirname, 'samples'), (dir, config) => {
(config.skip ? describe.skip : config.solo ? describe.only : describe)(
path.basename(dir) + ': ' + config.description,
() => {
it(`generates correct hashes`, () => {
process.chdir(dir);
return Promise.all(
[config.options1, config.options2].map(options =>
rollup
.rollup(options)
.then(bundle =>
bundle.generate(
extend(
{ format: 'esm', chunkFileNames: '[hash]', entryFileNames: '[hash]' },
options.output
)
)
)
.then(mapGeneratedToHashMap)
)
).then(([hashMap1, hashMap2]) => {
for (const name of config.expectedEqualHashes || []) {
checkChunkExists(hashMap1, hashMap2, name);
assert.equal(
hashMap1[name],
hashMap2[name],
`Expected hashes for chunk containing "${name}" to be equal but they were different.`
);
}
for (const name of config.expectedDifferentHashes || []) {
checkChunkExists(hashMap1, hashMap2, name);
assert.notEqual(
hashMap1[name],
hashMap2[name],
`Expected hashes for chunk containing "${name}" to be different but they were equal.`
);
}
});
});
}
);
});

function mapGeneratedToHashMap(generated) {
const hashMap = {};
const dirLength = process.cwd().length + 1;
for (const chunk of generated.output) {
for (const moduleName of Object.keys(chunk.modules)) {
hashMap[moduleName.slice(dirLength)] = chunk.fileName;
}
}
return hashMap;
}

function checkChunkExists(hashMap1, hashMap2, name) {
[hashMap1, hashMap2].forEach((hashMap, index) =>
assert.ok(
hashMap[name],
`Bundle ${index} did not contain a chunk containing module "${name}", found chunks for modules: ${Object.keys(
hashMap
).join(', ')}`
)
);
}
11 changes: 11 additions & 0 deletions test/file-hashes/samples/respects-exports/_config.js
@@ -0,0 +1,11 @@
module.exports = {
solo: true,
description: 'creates different hashes if the content is equal but the generated exports differ',
options1: {
input: ['main1', 'other']
},
options2: {
input: ['main2', 'other']
},
expectedDifferentHashes: ['dep.js']
};
@@ -0,0 +1,5 @@
define(function () { 'use strict';

console.log('main4');

});
@@ -0,0 +1,5 @@
define(function () { 'use strict';

console.log('main5');

});
@@ -0,0 +1,7 @@
define(['require'], function (require) { 'use strict';

console.log('main1');
new Promise(function (resolve, reject) { require(['./generated-chunk.js'], resolve, reject) });
new Promise(function (resolve, reject) { require(['./generated-chunk2.js'], resolve, reject) });

});
@@ -0,0 +1,5 @@
define(function () { 'use strict';

console.log('main2');

});
@@ -0,0 +1,5 @@
define(function () { 'use strict';

console.log('main3');

});
@@ -0,0 +1,3 @@
'use strict';

console.log('main4');
@@ -0,0 +1,3 @@
'use strict';

console.log('main5');
@@ -0,0 +1,5 @@
'use strict';

console.log('main1');
Promise.resolve(require('./generated-chunk.js'));
Promise.resolve(require('./generated-chunk2.js'));
@@ -0,0 +1,3 @@
'use strict';

console.log('main2');
@@ -0,0 +1,3 @@
'use strict';

console.log('main3');
@@ -0,0 +1 @@
console.log('main4');
@@ -0,0 +1 @@
console.log('main5');
@@ -0,0 +1,3 @@
console.log('main1');
import('./generated-chunk.js');
import('./generated-chunk2.js');
@@ -0,0 +1 @@
console.log('main2');
@@ -0,0 +1 @@
console.log('main3');
@@ -0,0 +1,10 @@
System.register([], function (exports, module) {
'use strict';
return {
execute: function () {

console.log('main4');

}
};
});
@@ -0,0 +1,10 @@
System.register([], function (exports, module) {
'use strict';
return {
execute: function () {

console.log('main5');

}
};
});
@@ -0,0 +1,12 @@
System.register([], function (exports, module) {
'use strict';
return {
execute: function () {

console.log('main1');
module.import('./generated-chunk.js');
module.import('./generated-chunk2.js');

}
};
});
@@ -0,0 +1,10 @@
System.register([], function (exports, module) {
'use strict';
return {
execute: function () {

console.log('main2');

}
};
});
@@ -0,0 +1,10 @@
System.register([], function (exports, module) {
'use strict';
return {
execute: function () {

console.log('main3');

}
};
});
2 changes: 2 additions & 0 deletions test/file-hashes/samples/respects-exports/dep.js
@@ -0,0 +1,2 @@
export const mightNotBeImported = 42;
console.log(mightNotBeImported);
3 changes: 3 additions & 0 deletions test/file-hashes/samples/respects-exports/main1.js
@@ -0,0 +1,3 @@
import {mightNotBeImported} from './dep';

console.log('main1', mightNotBeImported);
3 changes: 3 additions & 0 deletions test/file-hashes/samples/respects-exports/main2.js
@@ -0,0 +1,3 @@
import './dep';

console.log('main2');
3 changes: 3 additions & 0 deletions test/file-hashes/samples/respects-exports/other.js
@@ -0,0 +1,3 @@
import './dep';

console.log('other');
1 change: 1 addition & 0 deletions test/test.js
Expand Up @@ -12,6 +12,7 @@ describe('rollup', function() {
require('./function/index.js');
require('./form/index.js');
require('./chunking-form/index.js');
require('./file-hashes/index.js');
require('./sourcemaps/index.js');
require('./incremental/index.js');
require('./hooks/index.js');
Expand Down

0 comments on commit bf59bfd

Please sign in to comment.