test('allows babel config from package.json/babel when babelConfig === "inherit"', function (t) { var tempDir = uniqueTempDir(); var precompiler = new CachingPrecompiler(tempDir, 'inherit'); babel.transform.reset(); precompiler.precompileFile(fixture('es2015.js')); t.true(babel.transform.calledOnce); var options = babel.transform.firstCall.args[1]; t.true('filename' in options); t.true(options.sourceMaps); t.false(options.ast); t.true('inputSourceMap' in options); t.true(options.babelrc); t.end(); });
test('uses default babel options when babelConfig === "default"', function (t) { var tempDir = uniqueTempDir(); var precompiler = new CachingPrecompiler(tempDir, 'default'); babel.transform.reset(); precompiler.precompileFile(fixture('es2015.js')); t.true(babel.transform.calledOnce); var options = babel.transform.firstCall.args[1]; t.true('filename' in options); t.true(options.sourceMaps); t.false(options.ast); t.true('inputSourceMap' in options); t.false(options.babelrc); t.true(Array.isArray(options.presets)); t.true(Array.isArray(options.plugins)); t.end(); });
test('source file is an absolute path, after source map correction, even if already absolute', t => { const fixture = sourceMapFixtures.mapFile('throws'); const map = JSON.parse(fs.readFileSync(fixture.file + '.map')); const tmp = uniqueTempDir({create: true}); const sourceRoot = path.join(tmp, 'src'); const expectedSourceFile = path.join(sourceRoot, map.file); const tmpFile = path.join(tmp, path.basename(fixture.file)); fs.writeFileSync(tmpFile, fs.readFileSync(fixture.file)); fs.writeFileSync(tmpFile + '.map', JSON.stringify(Object.assign(map, {sourceRoot}), null, 2)); try { require(tmpFile).run(); t.fail('Fixture should have thrown'); } catch (err) { const serializedErr = serialize(err); t.is(serializedErr.source.file, expectedSourceFile); t.end(); } });
t.throws(function () { var cachingPrecompiler = CachingPrecompiler; cachingPrecompiler(uniqueTempDir(), null); }, {message: 'Class constructor CachingPrecompiler cannot be invoked without \'new\''});
test('creation with new', function (t) { var tempDir = uniqueTempDir(); var precompiler = new CachingPrecompiler(tempDir, null); t.is(precompiler.cacheDirPath, tempDir); t.end(); });
.then(function (files) { if (files.length === 0) { self._handleExceptions({ exception: new AvaError('Couldn\'t find any files to test'), file: undefined }); return []; } var cacheEnabled = self.options.cacheEnabled !== false; var cacheDir = (cacheEnabled && findCacheDir({name: 'ava', files: files})) || uniqueTempDir(); self.options.cacheDir = cacheDir; self.precompiler = new CachingPrecompiler(cacheDir, self.options.babelConfig); self.fileCount = files.length; self.base = path.relative('.', commonPathPrefix(files)) + path.sep; var tests = new Array(self.fileCount); return new Promise(function (resolve) { function run() { if (self.options.match.length > 0 && !self.hasExclusive) { self._handleExceptions({ exception: new AvaError('Couldn\'t find any matching tests'), file: undefined }); resolve([]); return; } self.emit('ready'); var method = self.options.serial ? 'mapSeries' : 'map'; var options = { runOnlyExclusive: self.hasExclusive }; resolve(Promise[method](files, function (file, index) { return tests[index].run(options).catch(function (err) { // The test failed catastrophically. Flag it up as an // exception, then return an empty result. Other tests may // continue to run. self._handleExceptions({ exception: err, file: file }); return { stats: {passCount: 0, skipCount: 0, todoCount: 0, failCount: 0}, tests: [] }; }); })); } // receive test count from all files and then run the tests var unreportedFiles = self.fileCount; var bailed = false; files.every(function (file, index) { var tried = false; function tryRun() { if (!tried && !bailed) { tried = true; unreportedFiles--; if (unreportedFiles === 0) { run(); } } } try { var test = tests[index] = self._runFile(file); test.on('stats', tryRun); test.catch(tryRun); return true; } catch (err) { bailed = true; self._handleExceptions({ exception: err, file: file }); resolve([]); return false; } }); }).then(function (results) { if (results.length === 0) { // No tests ran, make sure to tear down the child processes. tests.forEach(function (test) { test.send('teardown'); }); } return results; }); })
Api.prototype._run = function (files, _options) { var self = this; var runStatus = new TestData({ prefixTitles: this.options.explicitTitles || files.length > 1, runOnlyExclusive: _options && _options.runOnlyExclusive, base: path.relative('.', commonPathPrefix(files)) + path.sep }); if (self.options.timeout) { var timeout = ms(self.options.timeout); runStatus._restartTimer = debounce(function () { self._onTimeout(runStatus); }, timeout); runStatus._restartTimer(); runStatus.on('test', runStatus._restartTimer); } self.emit('test-run', runStatus, files); if (files.length === 0) { runStatus.handleExceptions({ exception: new AvaError('Couldn\'t find any files to test'), file: undefined }); return Promise.resolve([]); } var cacheEnabled = self.options.cacheEnabled !== false; var cacheDir = (cacheEnabled && findCacheDir({name: 'ava', files: files})) || uniqueTempDir(); self.options.cacheDir = cacheDir; self.precompiler = new CachingPrecompiler(cacheDir, self.options.babelConfig); self.fileCount = files.length; var tests = new Array(self.fileCount); // TODO: thid should be cleared at the end of the run runStatus.on('timeout', function () { tests.forEach(function (fork) { fork.exit(); }); }); return new Promise(function (resolve) { function run() { if (self.options.match.length > 0 && !runStatus.hasExclusive) { runStatus.handleExceptions({ exception: new AvaError('Couldn\'t find any matching tests'), file: undefined }); resolve([]); return; } self.emit('ready'); var method = self.options.serial ? 'mapSeries' : 'map'; var options = { runOnlyExclusive: runStatus.hasExclusive }; resolve(Promise[method](files, function (file, index) { return tests[index].run(options).catch(function (err) { // The test failed catastrophically. Flag it up as an // exception, then return an empty result. Other tests may // continue to run. runStatus.handleExceptions({ exception: err, file: file }); return { stats: { passCount: 0, skipCount: 0, todoCount: 0, failCount: 0 }, tests: [] }; }); })); } // receive test count from all files and then run the tests var unreportedFiles = self.fileCount; var bailed = false; files.every(function (file, index) { var tried = false; function tryRun() { if (!tried && !bailed) { tried = true; unreportedFiles--; if (unreportedFiles === 0) { run(); } } } try { var test = tests[index] = self._runFile(file, runStatus); test.on('stats', tryRun); test.catch(tryRun); return true; } catch (err) { bailed = true; runStatus.handleExceptions({ exception: err, file: file }); resolve([]); return false; } }); }).then(function (results) { if (results.length === 0) { // No tests ran, make sure to tear down the child processes. tests.forEach(function (test) { test.send('teardown'); }); } return results; }).then(function (results) { // cancel debounced _onTimeout() from firing if (self.options.timeout) { runStatus._restartTimer.cancel(); } runStatus.processResults(results); return runStatus; }); };
'serial', 'tap' ], default: conf, alias: { r: 'require', s: 'serial' } }); if (cli.input.length !== 1) { throw new Error('Specify a test file'); } var file = path.resolve(cli.input[0]); var cacheDir = findCacheDir({name: 'ava', files: [file]}) || uniqueTempDir(); var opts = { file: file, failFast: cli.flags.failFast, serial: cli.flags.serial, require: arrify(cli.flags.require), tty: false, cacheDir: cacheDir, precompiled: new CachingPrecompiler(cacheDir, conf.babel).generateHashForFile(file) }; var events = new EventEmitter(); // Mock the behavior of a parent process. process.send = function (data) { if (data && data.ava) {
.then(function (files) { if (files.length === 0) { self._handleExceptions({ exception: new AvaError('Couldn\'t find any files to test'), file: undefined }); return []; } var cacheEnabled = self.options.cacheEnabled !== false; var cacheDir = (cacheEnabled && findCacheDir({name: 'ava', files: files})) || uniqueTempDir(); self.options.cacheDir = cacheDir; self.precompiler = new CachingPrecompiler(cacheDir); self.fileCount = files.length; self.base = path.relative('.', commonPathPrefix(files)) + path.sep; var tests = files.map(self._runFile); // receive test count from all files and then run the tests var statsCount = 0; return new Promise(function (resolve) { tests.forEach(function (test) { var counted = false; function tryRun() { if (counted) { return; } if (++statsCount === self.fileCount) { self.emit('ready'); var method = self.options.serial ? 'mapSeries' : 'map'; resolve(Promise[method](files, function (file, index) { return tests[index].run().catch(function (err) { // The test failed catastrophically. Flag it up as an // exception, then return an empty result. Other tests may // continue to run. self._handleExceptions({ exception: err, file: file }); return { stats: {passCount: 0, skipCount: 0, failCount: 0}, tests: [] }; }); })); } } test.on('stats', tryRun); test.catch(tryRun); }); }); })
'verbose', 'serial', 'tap' ], default: conf, alias: { s: 'serial' } }); if (cli.input.length === 0) { throw new Error('Specify a test file'); } const file = path.resolve(cli.input[0]); const cacheDir = conf.cacheEnabled === false ? uniqueTempDir() : path.join(projectDir, 'node_modules', '.cache', 'ava'); const babelConfig = babelPipeline.validate(conf.babel); conf.extensions = normalizeExtensions(conf.extensions || [], babelConfig); const _regexpFullExtensions = new RegExp(`\\.(${conf.extensions.full.map(ext => escapeStringRegexp(ext)).join('|')})$`); const precompileFull = babelPipeline.build(process.cwd(), cacheDir, babelConfig, conf.compileEnhancements === true); let precompileEnhancementsOnly = () => null; if (conf.compileEnhancements) { precompileEnhancementsOnly = babelPipeline.build(projectDir, cacheDir, null, conf.compileEnhancements); } const precompiled = {}; precompiled[file] = _regexpFullExtensions.test(file) ? precompileFull(file) : precompileEnhancementsOnly(file);
default: conf, alias: { r: 'require', s: 'serial' } }); if (cli.input.length !== 1) { throw new Error('Specify a test file'); } var file = path.resolve(cli.input[0]); var cacheDir = findCacheDir({ name: 'ava', files: [file] }) || uniqueTempDir(); var precompiled = {}; precompiled[file] = new CachingPrecompiler(cacheDir, conf.babel).precompileFile(file); var opts = { file: file, failFast: cli.flags.failFast, serial: cli.flags.serial, require: arrify(cli.flags.require), tty: false, cacheDir: cacheDir, precompiled: precompiled }; var events = new EventEmitter(); var uncaughtExceptionCount = 0;