Browse Source

make tests more robust (fs was getting out of sync)

ghi-672
Rich Harris 9 years ago
parent
commit
84d408f9ad
  1. 20
      src/Bundle.js
  2. 5
      test/incremental/not-transform-twice/expected.js
  3. 3
      test/incremental/not-transform-twice/foo.js
  4. 3
      test/incremental/not-transform-twice/main.js
  5. 5
      test/incremental/transform-changed/expected.js
  6. 3
      test/incremental/transform-changed/foo-changed.js
  7. 3
      test/incremental/transform-changed/foo.js
  8. 3
      test/incremental/transform-changed/main.js
  9. 111
      test/test.js

20
src/Bundle.js

@ -19,11 +19,11 @@ import { dirname, isRelative, isAbsolute, relative, resolve } from './utils/path
export default class Bundle {
constructor ( options ) {
if ( typeof options.cache === 'object' ) {
this.cachedModules = options.cache.modules.reduce((modules, module) => {
modules[module.id] = module;
return modules;
}, {});
this.cachedModules = new Map();
if ( options.cache ) {
options.cache.modules.forEach( module => {
this.cachedModules.set( module.id, module );
});
}
this.plugins = ensureArray( options.plugins );
@ -195,13 +195,11 @@ export default class Bundle {
ast: null
};
}
if (this.cachedModules && this.cachedModules[id] && this.cachedModules[id].originalCode === source.code) {
const { code, originalCode } = this.cachedModules[id];
return {
code,
originalCode
};
if ( this.cachedModules.has( id ) && this.cachedModules.get( id ).originalCode === source.code ) {
return this.cachedModules.get( id );
}
return transform( source, id, this.transformers );
})
.then( source => {

5
test/incremental/not-transform-twice/expected.js

@ -1,5 +0,0 @@
function foo () {
console.log('foo');
};
foo();

3
test/incremental/not-transform-twice/foo.js

@ -1,3 +0,0 @@
export default function () {
console.log('foo');
};

3
test/incremental/not-transform-twice/main.js

@ -1,3 +0,0 @@
import foo from './foo.js';
foo();

5
test/incremental/transform-changed/expected.js

@ -1,5 +0,0 @@
function foo () {
console.log('foo-changed');
};
foo();

3
test/incremental/transform-changed/foo-changed.js

@ -1,3 +0,0 @@
export default function () {
console.log('foo-changed');
};

3
test/incremental/transform-changed/foo.js

@ -1,3 +0,0 @@
export default function () {
console.log('foo');
};

3
test/incremental/transform-changed/main.js

@ -1,3 +0,0 @@
import foo from './foo.js';
foo();

111
test/test.js

@ -448,76 +448,79 @@ describe( 'rollup', function () {
});
describe.only('incremental', function () {
it('does not transforms in the second time', function () {
var calls = 0;
var counter = {
transform: function ( code ) {
calls += 1;
return code;
}
function executeBundle ( bundle ) {
const cjs = bundle.generate({ format: 'cjs' });
const m = new Function( 'module', 'exports', cjs.code );
let module = { exports: {} };
m( module, module.exports );
return module.exports;
}
var calls;
var modules;
var plugin = {
resolveId: id => id,
load: id => {
return modules[ id ];
},
transform: function ( code ) {
calls += 1;
return code;
}
};
beforeEach( () => {
calls = 0;
modules = {
entry: `import foo from 'foo'; export default foo;`,
foo: `export default 42`
};
});
it('does not transforms in the second time', function () {
return rollup.rollup({
entry: path.join( INCREMENTAL, 'not-transform-twice', 'main.js' ),
plugins: [counter]
}).then( function ( bundle ) {
entry: 'entry',
plugins: [ plugin ]
}).then( bundle => {
assert.equal( calls, 2 );
return rollup.rollup({
entry: path.join( INCREMENTAL, 'not-transform-twice', 'main.js' ),
plugins: [counter],
entry: 'entry',
plugins: [ plugin ],
cache: bundle
});
}).then( function ( bundle ) {
}).then( bundle => {
assert.equal( calls, 2 );
var result = bundle.generate({
format: 'es6'
});
return sander.readFile( path.join( INCREMENTAL, 'not-transform-twice', 'expected.js' ) ).then( function (expected) {
assert.equal( normaliseOutput( expected ), result.code );
});
assert.equal( executeBundle( bundle ), 42 );
});
});
it('transforms modified sources', function () {
var calls = 0;
var counter = {
transform: function ( code ) {
calls += 1;
return code;
}
};
var bundle;
var foo;
var changed;
var expected;
return Promise.all([
rollup.rollup({
entry: path.join( INCREMENTAL, 'transform-changed', 'main.js' ),
plugins: [counter]
}),
sander.readFile( path.join( INCREMENTAL, 'transform-changed', 'foo.js' ) ),
sander.readFile( path.join( INCREMENTAL, 'transform-changed', 'foo-changed.js' ) ),
sander.readFile( path.join( INCREMENTAL, 'transform-changed', 'expected.js' ) )
]).then( function ( [_bundle, _foo, _changed, _expected] ) {
bundle = _bundle;
foo = normaliseOutput( _foo );
changed = normaliseOutput( _changed );
expected = normaliseOutput( _expected );
let cache;
return rollup.rollup({
entry: 'entry',
plugins: [ plugin ]
}).then( bundle => {
assert.equal( calls, 2 );
assert.equal( executeBundle( bundle ), 42 );
return sander.writeFile( INCREMENTAL, 'transform-changed', 'foo.js', changed );
}).then(function () {
modules.foo = `export default 43`;
cache = bundle;
}).then( () => {
return rollup.rollup({
entry: path.join( INCREMENTAL, 'transform-changed', 'main.js' ),
plugins: [counter],
cache: bundle
entry: 'entry',
plugins: [ plugin ],
cache
});
}).then( function ( bundle ) {
}).then( bundle => {
assert.equal( calls, 3 );
var result = bundle.generate({
format: 'es6'
});
assert.equal(expected, result.code);
return sander.writeFile( INCREMENTAL, 'transform-changed', 'foo.js', foo );
assert.equal( executeBundle( bundle ), 43 );
});
});
});

Loading…
Cancel
Save