From e86ac1ebf3d3b748bc798b74fa6dc22899f167e4 Mon Sep 17 00:00:00 2001 From: Douglas Duteil Date: Fri, 23 Jan 2015 16:56:53 +0100 Subject: [PATCH] test: remove setTimeout functions --- test/custom-loader.spec.js | 105 +++++------- test/system.spec.js | 337 ++++++++++++++++++------------------- 2 files changed, 210 insertions(+), 232 deletions(-) diff --git a/test/custom-loader.spec.js b/test/custom-loader.spec.js index 157f84f..38aa040 100644 --- a/test/custom-loader.spec.js +++ b/test/custom-loader.spec.js @@ -8,105 +8,92 @@ describe('Custom Loader', function () { it('should support ES6 scripts', function (done) { customLoader.import('test/loader/test') .then(function (m) { - setTimeout(function () { - expect(m.loader).to.be.equal('custom'); - done(); - }); - }, done) + expect(m.loader).to.be.equal('custom'); + }) + .then(done, done) }); it('should support AMD scripts', function (done) { customLoader.import('test/loader/amd') .then(function (m) { - setTimeout(function () { - expect(m.format).to.be.equal('amd'); - done(); - }); - }, done) + expect(m.format).to.be.equal('amd'); + }) + .then(done, done); }); }); - describe('special #locate path rule', function () { + describe('special #locate path rule', function a() { it('should support special loading rules', function (done) { customLoader.import('path/custom') .then(function (m) { - setTimeout(function () { - expect(m.path).to.be.ok(); - done(); - }); - }, done) - }); + expect(m.path).to.be.ok(); + }) + .then(done, done); + }) }); describe('errors', function () { - function dummyLoad(file, cb) { - System.import(file) - .then(function () { - setTimeout(function () { - expect(false, 'should not be successful').to.be.ok(); - cb(); - }); - }) - .catch(function (e) { - setTimeout(function () { - cb(e); - }); - }) - ; + function supposeToFail() { + expect(false, 'should not be successful').to.be.ok(); } it('should make the normalize throw', function (done) { - dummyLoad('test/loader/error1-parent', function (e) { - expect(e).to.be.match(/Error loading "error1" at \S+error1\.js\nError loading "error1" from "test\/loader\/error1-parent"/); - done(); - }); + customLoader.import('test/loader/error1-parent') + .then(supposeToFail, function (e) { + expect(e).to.be.match(/Error loading "test\/loader\/error1-parent" at \S+error1-parent\.js/); + }) + .then(done, done); }); it('should make the locate throw', function (done) { - dummyLoad('test/loader/error2', function (e) { - expect(e).to.be.match(/Error loading "test\/loader\/error2" at \S+test\/loader\/error2\.js/); - done(); - }); + customLoader.import('test/loader/error2') + .then(supposeToFail, function (e) { + expect(e).to.be.match(/Error loading "test\/loader\/error2" at \S+test\/loader\/error2\.js/); + }) + .then(done, done); }); it('should make the fetch throw', function (done) { - dummyLoad('test/loader/error3', function (e) { - expect(e).to.be.match(/Error loading "test\/loader\/error3" at \S+test\/loader\/error3\.js/); - done(); - }); + customLoader.import('test/loader/error3') + .then(supposeToFail, function (e) { + expect(e).to.be.match(/Error loading "test\/loader\/error3" at \S+test\/loader\/error3\.js/); + }) + .then(done, done); }); it('should make the translate throw', function (done) { - dummyLoad('test/loader/error4', function (e) { - expect(e).to.be.match(/Error loading "test\/loader\/error4" at \S+test\/loader\/error4\.js/); - done(); - }); + customLoader.import('test/loader/error4') + .then(supposeToFail, function (e) { + expect(e).to.be.match(/Error loading "test\/loader\/error4" at \S+test\/loader\/error4\.js/); + }) + .then(done, done); }); it('should make the instantiate throw', function (done) { - dummyLoad('test/loader/error5', function (e) { - expect(e).to.be.match(/Error loading "test\/loader\/error5" at \S+test\/loader\/error5\.js/); - done(); - }); + customLoader.import('test/loader/error5') + .then(supposeToFail, function (e) { + expect(e).to.be.match(/Error loading "test\/loader\/error5" at \S+test\/loader\/error5\.js/); + }) + .then(done, done); }); + }); }); describe('#normalize', function () { it('should support async normalization', function (done) { - customLoader.normalize('asdfasdf').then(function (normalized) { - return customLoader.import(normalized) - }).then(function (m) { - setTimeout(function () { + customLoader.normalize('asdfasdf') + .then(function (normalized) { + return customLoader.import(normalized); + }) + .then(function (m) { expect(m.n).to.be.equal('n'); - done(); - }); - }) - .catch(done); + }) + .then(done, done); }); }); }); diff --git a/test/system.spec.js b/test/system.spec.js index c6b8c6a..86a1929 100644 --- a/test/system.spec.js +++ b/test/system.spec.js @@ -23,13 +23,16 @@ describe('System', function () { }); it('should resolve paths', function () { - expect(System.locate({name: '@abc/def'})).to.equal('http://example.org/a/@abc/def.js'); - expect(System.locate({name: ' abc/def'})).to.equal('http://example.org/a/abc/def.js'); + expect(System.locate({name: '@abc/def'})) + .to.equal('http://example.org/a/@abc/def.js'); + expect(System.locate({name: ' abc/def'})) + .to.equal('http://example.org/a/abc/def.js'); }); it('should resolve paths with the existing config', function () { System.paths['path/*'] = '/test/*.js'; - expect(System.locate({name: 'path/test'})).to.equal('http://example.org/test/test.js'); + expect(System.locate({name: 'path/test'})) + .to.equal('http://example.org/test/test.js'); }); }); @@ -39,23 +42,22 @@ describe('System', function () { describe('an ES5 script', function () { it('should import a ES5 script', function (done) { - System.import('test/syntax/script').then(function (m) { - setTimeout(function () { + System.import('test/syntax/script') + .then(function (m) { expect(!!m).to.be.ok(); - done(); - }, 0); - }, done); + }) + .then(done, done); }); it('should import a ES5 script once loaded', function (done) { - System.import('test/syntax/script').then(function () { - System.import('test/syntax/script').then(function (m) { - setTimeout(function () { - expect(!!m).to.be.ok(); - done(); - }, 0); - }, done); - }, done); + System.import('test/syntax/script') + .then(function () { + return System.import('test/syntax/script'). + then(function (m) { + expect(!!m).to.be.ok(); + }); + }) + .then(done, done); }); }); @@ -63,44 +65,40 @@ describe('System', function () { describe('an ES6 script', function () { it('should import an ES6 script', function (done) { - System.import('test/syntax/es6').then(function (m) { - setTimeout(function () { + System.import('test/syntax/es6') + .then(function (m) { expect(m.p).to.equal('p'); - done(); - }, 0); - }, done); + }) + .then(done, done); }); it('should import an ES6 script with its dependencies', function (done) { - System.import('test/syntax/es6-withdep').then(function (m) { - setTimeout(function () { + System.import('test/syntax/es6-withdep') + .then(function (m) { expect(m.p).to.equal('p'); - done(); - }, 0); - }, done); + }) + .then(done, done); }); it('should import an ES6 script with a generator', function (done) { - System.import('test/syntax/es6-generator').then(function (m) { - setTimeout(function () { + System.import('test/syntax/es6-generator') + .then(function (m) { expect(!!m.generator).to.be.ok(); - done(); - }, 0); - }, done); + }) + .then(done, done); }); it('should import without bindings', function (done) { - System.import('test/syntax/direct').then(function (m) { - setTimeout(function () { + System.import('test/syntax/direct') + .then(function (m) { expect(!!m).to.be.ok(); - done(); - }, 0); - }, done); + }) + .then(done, done); }); it('should support es6 various syntax', function (done) { - System.import('test/syntax/es6-file').then(function (m) { - setTimeout(function () { + System.import('test/syntax/es6-file') + .then(function (m) { expect(m.q).to.be.a('function'); @@ -109,9 +107,8 @@ describe('System', function () { expect(e).to.equal('g'); }); - done(); - }); - }, done); + }) + .then(done, done); }); }); @@ -119,34 +116,37 @@ describe('System', function () { describe('with circular dependencies', function () { it('should resolve circular dependencies', function (done) { - System.import('test/syntax/circular1').then(function (m1) { - System.import('test/syntax/circular2').then(function (m2) { - setTimeout(function () { + System.import('test/syntax/circular1') + .then(function (m1) { + return System.import('test/syntax/circular2').then(function (m2) { expect(m1.variable1).to.equal('test circular 1'); expect(m2.variable2).to.equal('test circular 2'); - expect(m2.output, 'The module 2 output is the module 1 variable').to.equal('test circular 1'); - expect(m1.output, 'The module 1 output is the module 2 variable').to.equal('test circular 2'); - expect(m2.output1, 'The module 2 output1 is the module 1 output').to.equal('test circular 2'); - expect(m1.output2, 'The module 1 output2 is the module 2 output').to.equal('test circular 1'); - done(); - }, 0); - }, done); - }, done); + expect(m2.output, 'The module 2 output is the module 1 variable') + .to.equal('test circular 1'); + expect(m1.output, 'The module 1 output is the module 2 variable') + .to.equal('test circular 2'); + expect(m2.output1, 'The module 2 output1 is the module 1 output') + .to.equal('test circular 2'); + expect(m1.output2, 'The module 1 output2 is the module 2 output') + .to.equal('test circular 1'); + }); + }) + .then(done, done); }); it('should update circular dependencies', function (done) { - System.import('test/syntax/even').then(function (m) { - setTimeout(function () { + System.import('test/syntax/even') + .then(function (m) { expect(m.counter, 'Counter initially at 1').to.be.equal(1); expect(m.even(10), 'Must be an even number').to.be.ok(); expect(m.counter, 'Counter sould now be at 7').to.be.equal(7); expect(m.even(15), 'Must be an odd number').to.not.be.ok(); expect(m.counter, 'Counter sould now be at 15').to.be.equal(15); - done(); - }, 0); - }, done); + }) + .then(done, done); + }); }); @@ -154,16 +154,15 @@ describe('System', function () { describe('loading order', function () { function expectedOrder(file, order, done) { - System.import('test/loads/' + file).then(function (m) { - setTimeout(function () { - + System.import('test/loads/' + file) + .then(function (m) { order.forEach(function (letter) { - expect(m[letter], 'The "' + letter + '" file wasn\'t loaded').to.equal(letter); + expect(m[letter], 'The "' + letter + '" file wasn\'t loaded') + .to.equal(letter); }); - done(); - }, 0); - }, done); + }) + .then(done, done); } it('should load in order (a)', function (done) { @@ -200,44 +199,37 @@ describe('System', function () { describe('errors', function () { - function supposeToFail(done) { - return function shouldFail() { - setTimeout(function () { - expect(false, 'should not be successful').to.be.ok(); - done(); - }, 0); - } + function supposeToFail() { + expect(false, 'should not be successful').to.be.ok(); } it('should throw if on syntax error', function (done) { - System.import('test/loads/main').then(supposeToFail) + System.import('test/loads/main') + .then(supposeToFail) .catch(function (e) { - setTimeout(function () { - expect(e).to.be.equal('Error evaluating test/loads/deperror\ndep error'); - done(); - }, 0); - }); + expect(e) + .to.be.equal('Error evaluating test/loads/deperror\ndep error'); + }) + .then(done, done); }); it.skip('should throw what the script throws', function (done) { - System.import('test/loads/deperror').then(supposeToFail) - .catch(function (e) { - setTimeout(function () { - expect(false, 'should be successful').to.be.ok(); - done(); - }, 0); - }); + System.import('test/loads/deperror') + .then(supposeToFail) + .catch(function () { + expect(false, 'should be successful ??').to.be.ok(); + }) + .then(done, done); }); it('Unhandled rejection test', function (done) { - System.import('test/loads/load-non-existet').then(supposeToFail) + System.import('test/loads/load-non-existet') + .then(supposeToFail) .catch(function (e) { - setTimeout(function () { - expect(e).to.be.match(/Error loading "\S+" at \S+/); - done(); - }); - }); + expect(e).to.be.match(/Error loading "\S+" at \S+/); + }) + .then(done, done); }); }); @@ -246,81 +238,81 @@ describe('System', function () { describe('es6 export syntax overview', function () { it('should resolve different export syntax', function (done) { - System.import('test/syntax/export').then(function (m) { - setTimeout(function () { + System.import('test/syntax/export') + .then(function (m) { expect(m.p, 'should export a number').to.be.equal(5); expect(m.foo, 'should export a function').to.be.a('function'); expect(m.q, 'should export an object').to.be.an('object'); - expect(m.default, 'should export a default function').to.be.a('function'); + expect(m.default, 'should export a default function') + .to.be.a('function'); expect(m.s, 'should export a set of variable').to.be.equal(4); expect(m.t, 'should export a specifier number').to.be.equal(4); expect(m.m, 'should export a specifier object ').to.be.an('object'); - done(); - }); - }, done); + }) + .then(done, done); }); }); describe('es6 export default syntax', function () { it('should resolve "export default"', function (done) { - System.import('test/syntax/export-default').then(function (m) { - setTimeout(function () { + System.import('test/syntax/export-default') + .then(function (m) { expect(m.default()).to.be.equal('test'); - done(); - }); - }, done); + }) + .then(done, done); }); }); describe('es6 export re-exporting', function () { it('should support simple re-exporting', function (done) { - System.import('test/syntax/reexport1').then(function (m) { - setTimeout(function () { + System.import('test/syntax/reexport1') + .then(function (m) { expect(m.p, 'should export 5 from the "./export"').to.be.equal(5); - done(); - }); - }, done); + }) + .then(done, done); }); it('should support re-exporting binding', function (done) { - System.import('test/syntax/reexport-binding').then(function () { - System.import('test/syntax/rebinding').then(function (m) { - setTimeout(function () { - expect(m.p, 'should export "p" from the "./rebinding"').to.be.equal(4); - done(); + System.import('test/syntax/reexport-binding') + .then(function () { + return System.import('test/syntax/rebinding').then(function (m) { + expect(m.p, 'should export "p" from the "./rebinding"') + .to.be.equal(4); }); - }, done); - }, done); + }) + .then(done, done); }); it('should support re-exporting with a new name', function (done) { - System.import('test/syntax/reexport2').then(function (m) { - setTimeout(function () { - expect(m.q, 'should export "t" as "q" from the "./export"').to.be.equal(4); - expect(m.z, 'should export "q" as "z" from the "./export"').to.be.equal(5); - done(); - }); - }, done); + System.import('test/syntax/reexport2') + .then(function (m) { + expect(m.q, 'should export "t" as "q" from the "./export"') + .to.be.equal(4); + expect(m.z, 'should export "q" as "z" from the "./export"') + .to.be.equal(5); + }) + .then(done, done); }); it('should support re-exporting', function (done) { - System.import('test/syntax/export-star').then(function (m) { - setTimeout(function () { + System.import('test/syntax/export-star') + .then(function (m) { expect(m.foo, 'should export a function').to.be.equal('foo'); - expect(m.bar, 'should re-export export-star bar variable').to.be.equal('bar'); - done(); - }); - }, done); + expect(m.bar, 'should re-export export-star bar variable') + .to.be.equal('bar'); + }) + .then(done, done); }); it('should support re-exporting overwriting', function (done) { - System.import('test/syntax/export-star2').then(function (m) { - setTimeout(function () { - expect(m.bar, 'should re-export "./export-star" bar variable').to.be.equal('bar'); - expect(m.foo, 'should overwrite "./star-dep" foo variable with a function').to.be.a('function'); - done(); - }); - }, done); + System.import('test/syntax/export-star2') + .then(function (m) { + expect(m.bar, 'should re-export "./export-star" bar variable') + .to.be.equal('bar'); + expect(m.foo, 'should overwrite "./star-dep" foo variable with a function') + .to.be.a('function'); + }) + .then(done, done); }); }); @@ -328,17 +320,22 @@ describe('System', function () { describe('es6 import syntax overview', function () { it('should resolve different import syntax', function (done) { - System.import('test/syntax/import').then(function (m) { - setTimeout(function () { - expect(m.a, 'should export "d" as "a" from the "./export"').to.be.a('function'); - expect(m.b, 'should export "p" as "b" for "s" as "p" from "./reexport1"').to.be.equal(4); - expect(m.c, 'should export "z" as "c" with "z" from "./reexport2"').to.be.equal(5); - expect(m.d, 'should export "r" as "d" for "q" as "r" from the "./reexport2"').to.be.equal(4); - expect(m.q, 'should export "q" as "*" from the "./reexport1"').to.be.an('object'); - expect(m.q.foo, 'should access the "foo" function of "./reexport1" through "q" ad "*" ').to.be.a('function'); - done(); - }); - }, done); + System.import('test/syntax/import') + .then(function (m) { + expect(m.a, 'should export "d" as "a" from the "./export"') + .to.be.a('function'); + expect(m.b, 'should export "p" as "b" for "s" as "p" from "./reexport1"') + .to.be.equal(4); + expect(m.c, 'should export "z" as "c" with "z" from "./reexport2"') + .to.be.equal(5); + expect(m.d, 'should export "r" as "d" for "q" as "r" from the "./reexport2"') + .to.be.equal(4); + expect(m.q, 'should export "q" as "*" from the "./reexport1"') + .to.be.an('object'); + expect(m.q.foo, 'should access the "foo" function of "./reexport1" through "q" ad "*" ') + .to.be.a('function'); + }) + .then(done, done); }); }); @@ -346,15 +343,13 @@ describe('System', function () { describe('a script with metas', function () { it('should support module name meta', function (done) { - System.import('test/loader/moduleName').then(function (m) { - setTimeout(function () { - + System.import('test/loader/moduleName') + .then(function (m) { expect(m.name).to.be.equal('test/loader/moduleName'); - expect(m.address).to.be.equal(System.baseURL + 'test/loader/moduleName.js'); - - done(); - }); - }, done); + expect(m.address) + .to.be.equal(System.baseURL + 'test/loader/moduleName.js'); + }) + .then(done, done); }); }); @@ -364,54 +359,50 @@ describe('System', function () { it('should support custom paths', function (done) { System.paths['bar'] = 'test/loader/custom-path.js'; - System.import('bar').then(function (m) { - setTimeout(function () { + System.import('bar') + .then(function (m) { expect(m.bar).to.be.equal('bar'); delete System.paths['bar']; - done(); - }, 0); - }, done) + }) + .then(done, done); }); it('should support path wildcard', function (done) { System.paths['bar/*'] = 'test/loader/custom-folder/*.js'; - System.import('bar/path').then(function (m) { - setTimeout(function () { + System.import('bar/path') + .then(function (m) { expect(m.bar).to.be.equal('baa'); delete System.paths['bar/*']; - done(); - }, 0); - }, done) + }) + .then(done, done); }); it('should support most specific paths', function (done) { System.paths['bar/bar'] = 'test/loader/specific-path.js'; System.paths['bar/*'] = 'test/loader/custom-folder/*.js'; - System.import('bar/bar').then(function (m) { - setTimeout(function () { + System.import('bar/bar') + .then(function (m) { expect(m.path).to.be.ok(); delete System.paths['bar/bar']; delete System.paths['bar/*']; - done(); - }, 0); - }, done) + }) + .then(done, done); }); }); describeIf( typeof window != 'undefined' && window.Worker, - 'with Web Worker', function (done) { - it('should loading inside of a Web Worker', function () { + 'with Web Worker', function () { + it('should loading inside of a Web Worker', function (done) { var worker = new Worker(System.baseURL + 'test/worker/worker.js'); worker.onmessage = function (e) { - setTimeout(function () { - expect(e.data).to.be.equal('p'); - done(); - }, 0); + expect(e.data).to.be.equal('p'); + done(); }; + }); });